aboutsummaryrefslogtreecommitdiffstats
path: root/cmd/fix.go
diff options
context:
space:
mode:
authorUnknown <joe2010xtmf@163.com>2014-06-10 19:11:53 -0400
committerUnknown <joe2010xtmf@163.com>2014-06-10 19:11:53 -0400
commita3e1383cac3dfa2a71b04b47a295e9836fcb0d50 (patch)
tree402818d9b343a921f5c372972fa237a91e40d0f5 /cmd/fix.go
parentf160b4f33ca69df13b071648aad09e561dafec26 (diff)
downloadgitea-a3e1383cac3dfa2a71b04b47a295e9836fcb0d50.tar.gz
gitea-a3e1383cac3dfa2a71b04b47a295e9836fcb0d50.zip
Add gogs fix location command
Diffstat (limited to 'cmd/fix.go')
-rw-r--r--cmd/fix.go154
1 files changed, 142 insertions, 12 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!")
}