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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
|
package main
import (
"os"
"path/filepath"
"go.wit.com/dev/alexflint/arg"
"go.wit.com/gui"
"go.wit.com/lib/gui/repolist"
"go.wit.com/lib/gui/shell"
"go.wit.com/lib/protobuf/forgepb"
"go.wit.com/log"
)
// sent via -ldflags
var VERSION string
var BUILDTIME string
var rv *repolist.RepoList
var goSrcPath string
func main() {
pp := arg.MustParse(&argv)
// for very new users or users unfamilar with the command line, this may help them
if argv.Repo == "version" || argv.Repo == "help" || argv.Repo == "?" {
pp.WriteHelp(os.Stdout)
os.Exit(0)
}
// load the ~/.config/forge/ config
forge := forgepb.Init()
forge.ConfigPrintTable()
os.Setenv("REPO_WORK_PATH", forge.GetGoSrc())
if argv.Repo == "" {
// if there isn't anything else, just exit here
// if --git-pull, continue
if argv.Pull || argv.RedoGoMod {
// there is more to do
log.Info("onward and upward")
} else {
// user needs to pick something to do
pp.WriteHelp(os.Stdout)
log.Info("give me something to do!")
os.Exit(0)
}
} else {
// the user specified a repo, check if it's already cloned
fullgitdir := filepath.Join(goSrcPath, argv.Repo, ".git")
if shell.IsDir(fullgitdir) {
// if --recursive, continue
// if --git-pull, continue
if argv.Recursive || argv.Pull || argv.RedoGoMod {
log.Info("repo already cloned", filepath.Join(goSrcPath, argv.Repo))
// there is more to do
log.Info("argv.Recursive is", argv.Recursive)
log.Info("argv.Pull is", argv.Pull)
log.Info("argv.RedoGoMod is", argv.RedoGoMod)
log.Info("onward and upward")
} else {
log.Info("repo already cloned", filepath.Join(goSrcPath, argv.Repo))
os.Exit(0)
}
} else {
// need to download this new repo!
log.Info("repo is new. going to clone it to:", filepath.Join(goSrcPath, argv.Repo))
}
}
// testing gui idea
myGui := gui.New()
// myGui.Default()
rv := repolist.Init(forge, myGui)
rv.Enable()
rv.ScanRepositories()
// if the user defined a repo, attempt to download it now
if argv.Repo != "" {
os.Setenv("REPO_AUTO_CLONE", "true")
// pb, _ := forge.NewGoPath(argv.Repo)
pb, err := forge.Clone(argv.Repo)
if err != nil {
log.Info("could not download:", err)
os.Exit(-1)
}
newr, err := rv.AddRepo(pb)
if err != nil {
log.Info("AddRepo() failed", err)
os.Exit(-1)
}
if argv.Recursive || argv.Pull || argv.RedoGoMod {
log.Info("repo already cloned", filepath.Join(goSrcPath, argv.Repo))
// there is more to do
log.Info("argv.Recursive is", argv.Recursive)
log.Info("argv.Pull is", argv.Pull)
log.Info("argv.RedoGoMod is", argv.RedoGoMod)
log.Info("onward and upward")
} else {
log.Info("repo cloned worked to", filepath.Join(goSrcPath, argv.Repo))
os.Exit(0)
}
// update go.sum and go.mod
// todo: only do this if they don't exist?
// todo: make these git commit metadata
newr.Status.MakeRedomod()
// double check it actually downloaded
fullgitdir := filepath.Join(goSrcPath, argv.Repo, ".git")
if !shell.IsDir(fullgitdir) {
log.Info("repo cloned failed", filepath.Join(goSrcPath, argv.Repo))
os.Exit(-1)
}
}
os.Setenv("REPO_AUTO_CLONE", "false")
// look recursively in your working directory for git repos
totalcount := forge.Repos.Len()
// if --git-pull, run git pull on everything here
if argv.Pull {
log.Info("Total repositories:", totalcount)
log.Info("Going to run git pull in each one")
log.Sleep(1)
pull := []string{"git", "pull"}
var trycount, errcount int
loop := rv.ReposSortByName()
for loop.Scan() {
repo := loop.Repo()
if argv.DryRun {
log.Info("git pull --dry-run", repo.Status.Path())
} else {
trycount += 1
log.Info("actually run: git pull:", repo.Status.Path())
if result := shell.PathRunRealtime(repo.Status.Path(), pull); result.Error != nil {
log.Info("git pull error:", result.Error)
errcount += 1
}
}
}
log.Info("Total repositories:", totalcount, "Total attempted:", trycount, "Errors:", errcount)
os.Exit(0)
}
// this is experiemental but works for me
if argv.Recursive {
newr := rv.FindByName(argv.Repo)
if newr == nil {
log.Info("how did this repo still not exist?", argv.Repo)
os.Exit(-1)
}
os.Setenv("REPO_AUTO_CLONE", "true")
godep := newr.Status.GetGoDeps()
for gopath, version := range godep {
pb, err := forge.Clone(gopath)
if err != nil {
log.Info("could not download:", err)
os.Exit(-1)
}
repo, err := rv.AddRepo(pb)
if err != nil {
log.Info("git clone failed for", gopath, version)
continue
}
// always do this for now. probably always forever
repo.Status.MakeRedomod()
}
}
// remake all the go.sum & go.mod in every repo
// todo: make go.sum and go.mod git commit metadata
if argv.RedoGoMod {
loop := rv.ReposSortByName()
for loop.Scan() {
repo := loop.Repo()
repo.Status.MakeRedomod()
}
}
// remake the go.work file
if argv.AutoWork {
log.Info("About to re-create", goSrcPath+"/go.work")
log.Info("Sleep 3. original go.work saved as go.work.last (hit ctrl-c to cancel)")
log.Sleep(3)
shell.PathRun(goSrcPath, []string{"mv", "go.work", "go.work.last"})
rv.MakeGoWork()
shell.PathRun(goSrcPath, []string{"go", "work", "use"})
log.Info("")
log.Info("original go.work file saved as go.work.last")
log.Info("")
}
log.Info("Total repositories:", totalcount)
log.Info("Finished go-clone for", argv.Repo)
}
/*
func scanForRepos(goSrcPath string) int {
var count int
log.Info("scanning for repo in:", filepath.Join(goSrcPath, argv.Repo))
// rv.NewRepo("go.wit.com/apps/helloworld")
for _, path := range repostatus.ScanGitDirectories(goSrcPath) {
count += 1
gopath := strings.TrimPrefix(path, goSrcPath)
gopath = strings.Trim(gopath, "/")
// log.Info("Also should add:", gopath)
pb, err := forge.Clone(gopath)
if err != nil {
log.Info("could not download:", err)
os.Exit(-1)
}
repo, err := rv.AddRepo(pb)
}
return count
}
*/
|