diff options
Diffstat (limited to 'cmd')
-rw-r--r-- | cmd/fix.go | 154 | ||||
-rw-r--r-- | cmd/serve.go | 83 | ||||
-rw-r--r-- | cmd/update.go | 24 | ||||
-rw-r--r-- | cmd/web.go | 36 |
4 files changed, 208 insertions, 89 deletions
diff --git a/cmd/fix.go b/cmd/fix.go index 3c2278ba90..95ab3ae66f 100644 --- a/cmd/fix.go +++ b/cmd/fix.go @@ -5,10 +5,16 @@ package cmd import ( + "bufio" "fmt" + "io" + "io/ioutil" "os" + "path" + "strings" "github.com/codegangsta/cli" + "github.com/gogits/gogs/models" "github.com/gogits/gogs/modules/setting" ) @@ -16,28 +22,152 @@ import ( var CmdFix = cli.Command{ Name: "fix", Usage: "This command for upgrade from old version", - Description: `Fix provide upgrade from old version`, Action: runFix, + Subcommands: fixCommands, Flags: []cli.Flag{}, } -func runFix(k *cli.Context) { - workDir, _ := setting.WorkDir() - newLogger(workDir) +func runFix(ctx *cli.Context) { +} - setting.NewConfigContext() - models.LoadModelsConfig() +var fixCommands = []cli.Command{ + { + Name: "location", + Usage: "Change Gogs app location", + Description: `Command location fixes location change of Gogs + +gogs fix location <old Gogs path> +`, + Action: runFixLocation, + }, +} + +// rewriteAuthorizedKeys replaces old Gogs path to the new one. +func rewriteAuthorizedKeys(sshPath, oldPath, newPath string) error { + fr, err := os.Open(sshPath) + if err != nil { + return err + } + defer fr.Close() + + tmpPath := sshPath + ".tmp" + fw, err := os.Create(tmpPath) + if err != nil { + return err + } + defer fw.Close() + + oldPath = "command=\"" + oldPath + " serv" + newPath = "command=\"" + newPath + " serv" + buf := bufio.NewReader(fr) + for { + line, errRead := buf.ReadString('\n') + line = strings.TrimSpace(line) + + if errRead != nil { + if errRead != io.EOF { + return errRead + } + + // Reached end of file, if nothing to read then break, + // otherwise handle the last line. + if len(line) == 0 { + break + } + } + + // Still finding the line, copy the line that currently read. + if _, err = fw.WriteString(strings.Replace(line, oldPath, newPath, 1) + "\n"); err != nil { + return err + } - if models.UseSQLite3 { - os.Chdir(workDir) + if errRead == io.EOF { + break + } } - models.SetEngine() + if err = os.Remove(sshPath); err != nil { + return err + } + return os.Rename(tmpPath, sshPath) +} + +func rewriteUpdateHook(path, appPath string) error { + rp := strings.NewReplacer("\\", "/", " ", "\\ ") + if err := ioutil.WriteFile(path, []byte(fmt.Sprintf(models.TPL_UPDATE_HOOK, + setting.ScriptType, rp.Replace(appPath))), os.ModePerm); err != nil { + return err + } + return nil +} - err := models.Fix() +func walkDir(rootPath, recPath, appPath string, depth int) error { + depth++ + if depth > 3 { + return nil + } else if depth == 3 { + if err := rewriteUpdateHook(path.Join(rootPath, "hooks/update"), appPath); err != nil { + return err + } + } + + dir, err := os.Open(rootPath) + if err != nil { + return err + } + defer dir.Close() + + fis, err := dir.Readdir(0) if err != nil { + return err + } + + for _, fi := range fis { + if strings.Contains(fi.Name(), ".DS_Store") { + continue + } + + relPath := path.Join(recPath, fi.Name()) + curPath := path.Join(rootPath, fi.Name()) + if fi.IsDir() { + if err = walkDir(curPath, relPath, appPath, depth); err != nil { + return err + } + } + } + return nil +} + +func runFixLocation(ctx *cli.Context) { + if len(ctx.Args()) != 1 { + fmt.Println("Incorrect arguments number, expect 1") + os.Exit(2) + } + + execPath, _ := setting.ExecPath() + + oldPath := ctx.Args().First() + fmt.Printf("Old location: %s\n", oldPath) + fmt.Println("This command should be executed in the new Gogs path") + fmt.Printf("Do you want to change Gogs app path from old location to:\n") + fmt.Printf("-> %s?\n", execPath) + fmt.Print("Press <enter> to continue, use <Ctrl+c> to exit.") + fmt.Scanln() + + // Fix in authorized_keys file. + sshPath := path.Join(models.SshPath, "authorized_keys") + fmt.Printf("Fixing pathes in file: %s\n", sshPath) + if err := rewriteAuthorizedKeys(sshPath, oldPath, execPath); err != nil { + fmt.Println(err) + os.Exit(1) + } + + // Fix position in gogs-repositories. + setting.NewConfigContext() + fmt.Printf("Fixing pathes in repositories: %s\n", setting.RepoRootPath) + if err := walkDir(setting.RepoRootPath, "", execPath, 0); err != nil { fmt.Println(err) - } else { - fmt.Println("Fix successfully!") + os.Exit(1) } + fmt.Println("Fix position finished!") } diff --git a/cmd/serve.go b/cmd/serve.go index 302f8568e1..2a76da7937 100644 --- a/cmd/serve.go +++ b/cmd/serve.go @@ -13,9 +13,9 @@ import ( "strings" "github.com/codegangsta/cli" - qlog "github.com/qiniu/log" "github.com/gogits/gogs/models" + "github.com/gogits/gogs/modules/log" "github.com/gogits/gogs/modules/setting" ) @@ -27,27 +27,13 @@ var CmdServ = cli.Command{ Flags: []cli.Flag{}, } -func newLogger(logPath string) { - os.MkdirAll(path.Dir(logPath), os.ModePerm) - - f, err := os.OpenFile(logPath, os.O_WRONLY|os.O_APPEND|os.O_CREATE, os.ModePerm) - if err != nil { - qlog.Fatal(err) - } - - qlog.SetOutput(f) - //qlog.SetOutputLevel(qlog.Ldebug) - qlog.Info("Start logging serv...") -} - func setup(logPath string) { - workDir, _ := setting.WorkDir() - newLogger(path.Join(workDir, logPath)) - setting.NewConfigContext() + log.NewGitLogger(path.Join(setting.LogRootPath, logPath)) models.LoadModelsConfig() if models.UseSQLite3 { + workDir, _ := setting.WorkDir() os.Chdir(workDir) } @@ -70,45 +56,45 @@ func parseCmd(cmd string) (string, string) { } var ( - COMMANDS_READONLY = map[string]int{ - "git-upload-pack": models.AU_WRITABLE, - "git upload-pack": models.AU_WRITABLE, - "git-upload-archive": models.AU_WRITABLE, + COMMANDS_READONLY = map[string]models.AccessType{ + "git-upload-pack": models.WRITABLE, + "git upload-pack": models.WRITABLE, + "git-upload-archive": models.WRITABLE, } - COMMANDS_WRITE = map[string]int{ - "git-receive-pack": models.AU_READABLE, - "git receive-pack": models.AU_READABLE, + COMMANDS_WRITE = map[string]models.AccessType{ + "git-receive-pack": models.READABLE, + "git receive-pack": models.READABLE, } ) -func In(b string, sl map[string]int) bool { +func In(b string, sl map[string]models.AccessType) bool { _, e := sl[b] return e } func runServ(k *cli.Context) { - setup(path.Join(setting.LogRootPath, "serv.log")) + setup("serv.log") keys := strings.Split(os.Args[2], "-") if len(keys) != 2 { println("Gogs: auth file format error") - qlog.Fatal("Invalid auth file format: %s", os.Args[2]) + log.GitLogger.Fatal("Invalid auth file format: %s", os.Args[2]) } keyId, err := strconv.ParseInt(keys[1], 10, 64) if err != nil { println("Gogs: auth file format error") - qlog.Fatalf("Invalid auth file format: %v", err) + log.GitLogger.Fatal("Invalid auth file format: %v", err) } user, err := models.GetUserByKeyId(keyId) if err != nil { if err == models.ErrUserNotKeyOwner { println("Gogs: you are not the owner of SSH key") - qlog.Fatalf("Invalid owner of SSH key: %d", keyId) + log.GitLogger.Fatal("Invalid owner of SSH key: %d", keyId) } println("Gogs: internal error:", err) - qlog.Fatalf("Fail to get user by key ID(%d): %v", keyId, err) + log.GitLogger.Fatal("Fail to get user by key ID(%d): %v", keyId, err) } cmd := os.Getenv("SSH_ORIGINAL_COMMAND") @@ -122,7 +108,7 @@ func runServ(k *cli.Context) { rr := strings.SplitN(repoPath, "/", 2) if len(rr) != 2 { println("Gogs: unavailable repository", args) - qlog.Fatalf("Unavailable repository: %v", args) + log.GitLogger.Fatal("Unavailable repository: %v", args) } repoUserName := rr[0] repoName := strings.TrimSuffix(rr[1], ".git") @@ -134,45 +120,45 @@ func runServ(k *cli.Context) { if err != nil { if err == models.ErrUserNotExist { println("Gogs: given repository owner are not registered") - qlog.Fatalf("Unregistered owner: %s", repoUserName) + log.GitLogger.Fatal("Unregistered owner: %s", repoUserName) } println("Gogs: internal error:", err) - qlog.Fatalf("Fail to get repository owner(%s): %v", repoUserName, err) + log.GitLogger.Fatal("Fail to get repository owner(%s): %v", repoUserName, err) } // Access check. switch { case isWrite: - has, err := models.HasAccess(user.Name, path.Join(repoUserName, repoName), models.AU_WRITABLE) + has, err := models.HasAccess(user.Name, path.Join(repoUserName, repoName), models.WRITABLE) if err != nil { println("Gogs: internal error:", err) - qlog.Fatal("Fail to check write access:", err) + log.GitLogger.Fatal("Fail to check write access:", err) } else if !has { println("You have no right to write this repository") - qlog.Fatalf("User %s has no right to write repository %s", user.Name, repoPath) + log.GitLogger.Fatal("User %s has no right to write repository %s", user.Name, repoPath) } case isRead: repo, err := models.GetRepositoryByName(repoUser.Id, repoName) if err != nil { if err == models.ErrRepoNotExist { println("Gogs: given repository does not exist") - qlog.Fatalf("Repository does not exist: %s/%s", repoUser.Name, repoName) + log.GitLogger.Fatal("Repository does not exist: %s/%s", repoUser.Name, repoName) } println("Gogs: internal error:", err) - qlog.Fatalf("Fail to get repository: %v", err) + log.GitLogger.Fatal("Fail to get repository: %v", err) } if !repo.IsPrivate { break } - has, err := models.HasAccess(user.Name, path.Join(repoUserName, repoName), models.AU_READABLE) + has, err := models.HasAccess(user.Name, path.Join(repoUserName, repoName), models.READABLE) if err != nil { println("Gogs: internal error:", err) - qlog.Fatal("Fail to check read access:", err) + log.GitLogger.Fatal("Fail to check read access:", err) } else if !has { println("You have no right to access this repository") - qlog.Fatalf("User %s has no right to read repository %s", user.Name, repoPath) + log.GitLogger.Fatal("User %s has no right to read repository %s", user.Name, repoPath) } default: println("Unknown command") @@ -186,18 +172,9 @@ func runServ(k *cli.Context) { gitcmd.Stdout = os.Stdout gitcmd.Stdin = os.Stdin gitcmd.Stderr = os.Stderr - - if err = gitcmd.Run(); err != nil { + err = gitcmd.Run() + if err != nil { println("Gogs: internal error:", err) - qlog.Fatalf("Fail to execute git command: %v", err) + log.GitLogger.Fatal("Fail to execute git command: %v", err) } - - //refName := os.Getenv("refName") - //oldCommitId := os.Getenv("oldCommitId") - //newCommitId := os.Getenv("newCommitId") - - //qlog.Error("get envs:", refName, oldCommitId, newCommitId) - - // update - //models.Update(refName, oldCommitId, newCommitId, repoUserName, repoName, user.Id) } diff --git a/cmd/update.go b/cmd/update.go index b8686a3e6e..c030b6cfb2 100644 --- a/cmd/update.go +++ b/cmd/update.go @@ -6,14 +6,12 @@ package cmd import ( "os" - "path" "strconv" "github.com/codegangsta/cli" - qlog "github.com/qiniu/log" "github.com/gogits/gogs/models" - "github.com/gogits/gogs/modules/setting" + "github.com/gogits/gogs/modules/log" ) var CmdUpdate = cli.Command{ @@ -24,35 +22,27 @@ var CmdUpdate = cli.Command{ Flags: []cli.Flag{}, } -func updateEnv(refName, oldCommitId, newCommitId string) { - os.Setenv("refName", refName) - os.Setenv("oldCommitId", oldCommitId) - os.Setenv("newCommitId", newCommitId) - qlog.Info("set envs:", refName, oldCommitId, newCommitId) -} - func runUpdate(c *cli.Context) { cmd := os.Getenv("SSH_ORIGINAL_COMMAND") if cmd == "" { return } - setup(path.Join(setting.LogRootPath, "update.log")) + setup("update.log") args := c.Args() if len(args) != 3 { - qlog.Fatal("received less 3 parameters") + log.GitLogger.Fatal("received less 3 parameters") } else if args[0] == "" { - qlog.Fatal("refName is empty, shouldn't use") + log.GitLogger.Fatal("refName is empty, shouldn't use") } - //updateEnv(args[0], args[1], args[2]) - userName := os.Getenv("userName") userId, _ := strconv.ParseInt(os.Getenv("userId"), 10, 64) - //repoId := os.Getenv("repoId") repoUserName := os.Getenv("repoUserName") repoName := os.Getenv("repoName") - models.Update(args[0], args[1], args[2], userName, repoUserName, repoName, userId) + if err := models.Update(args[0], args[1], args[2], userName, repoUserName, repoName, userId); err != nil { + log.GitLogger.Fatal(err.Error()) + } } diff --git a/cmd/web.go b/cmd/web.go index dc4daca085..7387d445e4 100644 --- a/cmd/web.go +++ b/cmd/web.go @@ -27,6 +27,7 @@ import ( "github.com/gogits/gogs/routers/admin" "github.com/gogits/gogs/routers/api/v1" "github.com/gogits/gogs/routers/dev" + "github.com/gogits/gogs/routers/org" "github.com/gogits/gogs/routers/repo" "github.com/gogits/gogs/routers/user" ) @@ -89,11 +90,13 @@ func runWeb(*cli.Context) { m.Get("/", ignSignIn, routers.Home) m.Get("/install", bindIgnErr(auth.InstallForm{}), routers.Install) m.Post("/install", bindIgnErr(auth.InstallForm{}), routers.InstallPost) - m.Get("/issues", reqSignIn, user.Issues) - m.Get("/pulls", reqSignIn, user.Pulls) - m.Get("/stars", reqSignIn, user.Stars) + m.Group("", func(r martini.Router) { + r.Get("/issues", user.Issues) + r.Get("/pulls", user.Pulls) + r.Get("/stars", user.Stars) + }, reqSignIn) - m.Group("/api", func(r martini.Router) { + m.Group("/api", func(_ martini.Router) { m.Group("/v1", func(r martini.Router) { // Miscellaneous. r.Post("/markdown", bindIgnErr(apiv1.MarkdownForm{}), v1.Markdown) @@ -159,8 +162,9 @@ func runWeb(*cli.Context) { m.Group("/admin", func(r martini.Router) { r.Get("/users", admin.Users) r.Get("/repos", admin.Repositories) - r.Get("/config", admin.Config) r.Get("/auths", admin.Auths) + r.Get("/config", admin.Config) + r.Get("/monitor", admin.Monitor) }, adminReq) m.Group("/admin/users", func(r martini.Router) { r.Get("/new", admin.NewUser) @@ -184,6 +188,22 @@ func runWeb(*cli.Context) { reqOwner := middleware.RequireOwner() + m.Group("/org", func(r martini.Router) { + r.Get("/create", org.New) + r.Post("/create", bindIgnErr(auth.CreateOrgForm{}), org.NewPost) + r.Get("/:org", org.Organization) + r.Get("/:org/dashboard", org.Dashboard) + r.Get("/:org/members", org.Members) + + r.Get("/:org/teams/:team/edit", org.EditTeam) + r.Get("/:org/teams/new", org.NewTeam) + r.Get("/:org/teams", org.Teams) + + r.Get("/:org/settings", org.Settings) + r.Post("/:org/settings", bindIgnErr(auth.OrgSettingForm{}), org.SettingsPost) + r.Post("/:org/settings/delete", org.DeletePost) + }, reqSignIn) + m.Group("/:username/:reponame", func(r martini.Router) { r.Get("/settings", repo.Setting) r.Post("/settings", bindIgnErr(auth.RepoSettingForm{}), repo.SettingPost) @@ -221,11 +241,13 @@ func runWeb(*cli.Context) { }) r.Post("/comment/:action", repo.Comment) - r.Get("/releases/new", repo.ReleasesNew) + r.Get("/releases/new", repo.NewRelease) + r.Get("/releases/edit/:tagname", repo.EditRelease) }, reqSignIn, middleware.RepoAssignment(true)) m.Group("/:username/:reponame", func(r martini.Router) { - r.Post("/releases/new", bindIgnErr(auth.NewReleaseForm{}), repo.ReleasesNewPost) + r.Post("/releases/new", bindIgnErr(auth.NewReleaseForm{}), repo.NewReleasePost) + r.Post("/releases/edit/:tagname", bindIgnErr(auth.EditReleaseForm{}), repo.EditReleasePost) }, reqSignIn, middleware.RepoAssignment(true, true)) m.Group("/:username/:reponame", func(r martini.Router) { |