-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcommand_doctor.go
83 lines (71 loc) · 2.24 KB
/
command_doctor.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package main
import (
"fmt"
"io/fs"
"os"
"path/filepath"
"strings"
"github.com/Songmu/prompter"
"github.com/urfave/cli"
)
var commandDoctor = cli.Command{
Name: "doctor",
Action: doDoctor,
Flags: []cli.Flag{
cli.BoolFlag{
Name: "fix",
Usage: "automatically fix issues",
},
},
}
func doDoctor(c *cli.Context) error {
fixupIssues := c.Bool("fix")
ghqPath := verifyGhqPath()
reposChannel := searchForRepos(ghqPath)
// Listing repos
for repo := range reposChannel {
remoteOriginURL, _ := GitConfigGet(repo.Path, "remote.origin.url")
trimmedRemote := compileTargetPathFromURL(remoteOriginURL)
trimmedLocal := strings.TrimPrefix(repo.Path, ghqPath+"/")
if remoteOriginURL == "" {
continue
}
if trimmedRemote != trimmedLocal && !strings.Contains(trimmedLocal, "golang.org/x/") {
fmt.Println("===> 'remote.origin.url' has been changed")
fmt.Println("===> local ", trimmedLocal)
fmt.Println("===> remote", trimmedRemote)
if fixupIssues {
fmt.Println("===> Choose the right location for" + trimmedLocal)
fmt.Println("[1] " + trimmedLocal)
fmt.Println("[2] " + trimmedRemote)
choice := prompter.Choose("===>", []string{"1", "2"}, "1")
if choice == "1" {
// Change remote.origin
slp := strings.Split(trimmedLocal, "/")
remotePathFromLocal := fmt.Sprintf("git@%s:%s/%s.git", slp[0], slp[1], slp[2])
err := GitRemoteSetURL(repo.Path, "origin", remotePathFromLocal)
if err != nil {
fmt.Println("===> Failed because of", err)
continue
}
fmt.Println("===> Change remote.origin.url to", remotePathFromLocal)
} else {
// Move directory
localPathFromRemote := filepath.Join(ghqPath, trimmedRemote)
fmt.Println(localPathFromRemote)
if _, err := os.Stat(localPathFromRemote); os.IsExist(err) {
fmt.Println("===> Failed to move repository because", localPathFromRemote, "already exist")
continue
}
os.MkdirAll(filepath.Dir(localPathFromRemote), fs.ModeDir)
if err := os.Rename(repo.Path, localPathFromRemote); err != nil {
fmt.Println("===> Failed to move repository because of", err)
continue
}
fmt.Println("===> Moved repository from", repo.Path, "to", localPathFromRemote)
}
}
}
}
return nil
}