2024-03-07 00:50:58 -06:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-03-07 19:30:09 -06:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2024-03-09 09:23:04 -06:00
|
|
|
"strings"
|
2024-03-07 19:30:09 -06:00
|
|
|
|
2024-03-07 16:45:49 -06:00
|
|
|
"go.wit.com/dev/alexflint/arg"
|
|
|
|
"go.wit.com/gui"
|
|
|
|
"go.wit.com/lib/gui/repolist"
|
2024-03-09 09:23:04 -06:00
|
|
|
"go.wit.com/lib/gui/repostatus"
|
2024-03-07 00:50:58 -06:00
|
|
|
"go.wit.com/lib/gui/shell"
|
2024-11-23 17:24:45 -06:00
|
|
|
"go.wit.com/lib/protobuf/forgepb"
|
2024-03-07 00:50:58 -06:00
|
|
|
"go.wit.com/log"
|
|
|
|
)
|
|
|
|
|
2024-11-07 16:53:53 -06:00
|
|
|
// sent via -ldflags
|
2024-11-07 07:03:51 -06:00
|
|
|
var VERSION string
|
2024-11-15 10:52:00 -06:00
|
|
|
var BUILDTIME string
|
2024-03-07 00:50:58 -06:00
|
|
|
|
2024-03-07 16:45:49 -06:00
|
|
|
var rv *repolist.RepoList
|
2024-11-23 17:24:45 -06:00
|
|
|
var goSrcPath string
|
2024-03-07 00:50:58 -06:00
|
|
|
|
|
|
|
func main() {
|
2024-11-07 07:03:51 -06:00
|
|
|
pp := arg.MustParse(&argv)
|
2024-03-07 16:45:49 -06:00
|
|
|
|
2024-11-07 07:03:51 -06:00
|
|
|
// for very new users or users unfamilar with the command line, this may help them
|
|
|
|
if argv.Repo == "version" || argv.Repo == "help" || argv.Repo == "?" {
|
2024-11-01 23:58:19 -05:00
|
|
|
pp.WriteHelp(os.Stdout)
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
2024-11-15 13:25:52 -06:00
|
|
|
// use ~/go/src unless we find a go.work file in a parent directory
|
2024-11-23 17:24:45 -06:00
|
|
|
goSrcPath, err := forgepb.FindGoSrc()
|
2024-03-07 19:30:09 -06:00
|
|
|
if err != nil {
|
|
|
|
log.Info(err)
|
|
|
|
os.Exit(-1)
|
2024-03-07 00:50:58 -06:00
|
|
|
}
|
2024-11-23 17:24:45 -06:00
|
|
|
if goSrcPath != "" {
|
|
|
|
os.Setenv("REPO_WORK_PATH", goSrcPath)
|
2024-11-15 09:11:11 -06:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-11-15 13:25:52 -06:00
|
|
|
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!")
|
2024-11-15 09:11:11 -06:00
|
|
|
os.Exit(0)
|
|
|
|
}
|
2024-11-15 13:25:52 -06:00
|
|
|
} else {
|
|
|
|
// the user specified a repo, check if it's already cloned
|
2024-11-23 17:24:45 -06:00
|
|
|
fullgitdir := filepath.Join(goSrcPath, argv.Repo, ".git")
|
2024-11-15 13:25:52 -06:00
|
|
|
if shell.IsDir(fullgitdir) {
|
|
|
|
// if --recursive, continue
|
|
|
|
// if --git-pull, continue
|
|
|
|
if argv.Recursive || argv.Pull || argv.RedoGoMod {
|
2024-11-23 17:24:45 -06:00
|
|
|
log.Info("repo already cloned", filepath.Join(goSrcPath, argv.Repo))
|
2024-11-15 13:25:52 -06:00
|
|
|
// there is more to do
|
2024-11-17 16:04:08 -06:00
|
|
|
log.Info("argv.Recursive is", argv.Recursive)
|
|
|
|
log.Info("argv.Pull is", argv.Pull)
|
|
|
|
log.Info("argv.RedoGoMod is", argv.RedoGoMod)
|
2024-11-15 13:25:52 -06:00
|
|
|
log.Info("onward and upward")
|
|
|
|
} else {
|
2024-11-23 17:24:45 -06:00
|
|
|
log.Info("repo already cloned", filepath.Join(goSrcPath, argv.Repo))
|
2024-11-15 13:25:52 -06:00
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// need to download this new repo!
|
2024-11-23 17:24:45 -06:00
|
|
|
log.Info("repo is new. going to clone it to:", filepath.Join(goSrcPath, argv.Repo))
|
2024-11-15 13:25:52 -06:00
|
|
|
}
|
2024-11-15 09:11:11 -06:00
|
|
|
}
|
2024-03-07 19:30:09 -06:00
|
|
|
|
2024-11-15 13:25:52 -06:00
|
|
|
// sets up gui stuff. not working yet
|
2024-03-07 16:45:49 -06:00
|
|
|
b := gui.RawBox()
|
|
|
|
rv = repolist.AutotypistView(b)
|
2024-03-07 00:50:58 -06:00
|
|
|
|
2024-11-15 13:25:52 -06:00
|
|
|
// if the user defined a repo, attempt to download it now
|
|
|
|
if argv.Repo != "" {
|
|
|
|
os.Setenv("REPO_AUTO_CLONE", "true")
|
|
|
|
newr, err := rv.NewRepo(argv.Repo)
|
|
|
|
if err != nil {
|
|
|
|
log.Info("could not download:", err)
|
|
|
|
os.Exit(-1)
|
|
|
|
}
|
2024-11-17 16:04:08 -06:00
|
|
|
if argv.Recursive || argv.Pull || argv.RedoGoMod {
|
2024-11-23 17:24:45 -06:00
|
|
|
log.Info("repo already cloned", filepath.Join(goSrcPath, argv.Repo))
|
2024-11-17 16:04:08 -06:00
|
|
|
// 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 {
|
2024-11-23 17:24:45 -06:00
|
|
|
log.Info("repo cloned worked to", filepath.Join(goSrcPath, argv.Repo))
|
2024-11-17 16:04:08 -06:00
|
|
|
os.Exit(0)
|
|
|
|
}
|
2024-11-15 13:25:52 -06:00
|
|
|
|
|
|
|
// 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
|
2024-11-23 17:24:45 -06:00
|
|
|
fullgitdir := filepath.Join(goSrcPath, argv.Repo, ".git")
|
2024-11-15 13:25:52 -06:00
|
|
|
if !shell.IsDir(fullgitdir) {
|
2024-11-23 17:24:45 -06:00
|
|
|
log.Info("repo cloned failed", filepath.Join(goSrcPath, argv.Repo))
|
2024-11-15 13:25:52 -06:00
|
|
|
os.Exit(-1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-17 16:04:08 -06:00
|
|
|
os.Setenv("REPO_AUTO_CLONE", "false")
|
2024-11-15 13:25:52 -06:00
|
|
|
// look recursively in your working directory for git repos
|
2024-11-23 17:24:45 -06:00
|
|
|
totalcount := scanForRepos(goSrcPath)
|
2024-11-15 13:25:52 -06:00
|
|
|
|
|
|
|
// if --git-pull, run git pull on everything here
|
2024-11-08 06:45:25 -06:00
|
|
|
if argv.Pull {
|
2024-11-15 13:25:52 -06:00
|
|
|
log.Info("Total repositories:", totalcount)
|
2024-11-08 06:45:25 -06:00
|
|
|
log.Info("Going to run git pull in each one")
|
|
|
|
log.Sleep(1)
|
|
|
|
pull := []string{"git", "pull"}
|
2024-11-15 09:11:11 -06:00
|
|
|
|
2024-11-08 06:45:25 -06:00
|
|
|
var trycount, errcount int
|
2024-11-15 09:11:11 -06:00
|
|
|
loop := rv.ReposSortByName()
|
2024-11-08 06:45:25 -06:00
|
|
|
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())
|
2024-11-15 09:29:49 -06:00
|
|
|
if result := shell.PathRunRealtime(repo.Status.Path(), pull); result.Error != nil {
|
2024-11-08 07:17:27 -06:00
|
|
|
log.Info("git pull error:", result.Error)
|
2024-11-08 06:45:25 -06:00
|
|
|
errcount += 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-11-15 13:25:52 -06:00
|
|
|
log.Info("Total repositories:", totalcount, "Total attempted:", trycount, "Errors:", errcount)
|
2024-11-08 06:45:25 -06:00
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
2024-11-15 13:25:52 -06:00
|
|
|
// this is experiemental but works for me
|
2024-11-07 07:03:51 -06:00
|
|
|
if argv.Recursive {
|
2024-11-22 21:44:16 -06:00
|
|
|
newr := rv.FindByName(argv.Repo)
|
2024-11-15 13:25:52 -06:00
|
|
|
if newr == nil {
|
|
|
|
log.Info("how did this repo still not exist?", argv.Repo)
|
|
|
|
os.Exit(-1)
|
|
|
|
}
|
2024-11-17 16:04:08 -06:00
|
|
|
os.Setenv("REPO_AUTO_CLONE", "true")
|
2024-11-15 13:25:52 -06:00
|
|
|
godep := newr.Status.GetGoDeps()
|
2024-03-21 19:43:26 -05:00
|
|
|
for gopath, version := range godep {
|
|
|
|
repo, err := rv.NewRepo(gopath)
|
|
|
|
if err != nil {
|
|
|
|
log.Info("git clone failed for", gopath, version)
|
|
|
|
continue
|
|
|
|
}
|
2024-11-15 13:25:52 -06:00
|
|
|
// always do this for now. probably always forever
|
2024-03-21 19:43:26 -05:00
|
|
|
repo.Status.MakeRedomod()
|
2024-03-09 22:00:10 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-15 13:25:52 -06:00
|
|
|
// 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()
|
2024-03-09 22:00:10 -06:00
|
|
|
repo.Status.MakeRedomod()
|
2024-03-09 16:49:27 -06:00
|
|
|
}
|
2024-03-09 09:23:04 -06:00
|
|
|
}
|
2024-03-07 16:45:49 -06:00
|
|
|
|
2024-11-15 13:25:52 -06:00
|
|
|
// remake the go.work file
|
2024-11-15 09:29:49 -06:00
|
|
|
if argv.AutoWork {
|
2024-11-23 17:24:45 -06:00
|
|
|
log.Info("About to re-create", goSrcPath+"/go.work")
|
2024-11-15 09:44:54 -06:00
|
|
|
log.Info("Sleep 3. original go.work saved as go.work.last (hit ctrl-c to cancel)")
|
|
|
|
log.Sleep(3)
|
2024-11-23 17:24:45 -06:00
|
|
|
shell.PathRun(goSrcPath, []string{"mv", "go.work", "go.work.last"})
|
2024-03-09 16:49:27 -06:00
|
|
|
rv.MakeGoWork()
|
2024-11-23 17:24:45 -06:00
|
|
|
shell.PathRun(goSrcPath, []string{"go", "work", "use"})
|
2024-11-15 09:44:54 -06:00
|
|
|
log.Info("")
|
|
|
|
log.Info("original go.work file saved as go.work.last")
|
|
|
|
log.Info("")
|
2024-03-07 16:45:49 -06:00
|
|
|
}
|
2024-11-15 13:25:52 -06:00
|
|
|
|
|
|
|
log.Info("Total repositories:", totalcount)
|
|
|
|
log.Info("Finished go-clone for", argv.Repo)
|
2024-03-07 00:50:58 -06:00
|
|
|
}
|
2024-03-07 16:45:49 -06:00
|
|
|
|
2024-11-23 17:24:45 -06:00
|
|
|
func scanForRepos(goSrcPath string) int {
|
2024-11-08 06:45:25 -06:00
|
|
|
var count int
|
2024-11-23 17:24:45 -06:00
|
|
|
log.Info("scanning for repo in:", filepath.Join(goSrcPath, argv.Repo))
|
2024-11-08 06:45:25 -06:00
|
|
|
|
|
|
|
// rv.NewRepo("go.wit.com/apps/helloworld")
|
2024-11-23 17:24:45 -06:00
|
|
|
for _, path := range repostatus.ScanGitDirectories(goSrcPath) {
|
2024-11-08 06:45:25 -06:00
|
|
|
count += 1
|
2024-11-23 17:24:45 -06:00
|
|
|
gopath := strings.TrimPrefix(path, goSrcPath)
|
2024-11-08 06:45:25 -06:00
|
|
|
gopath = strings.Trim(gopath, "/")
|
|
|
|
// log.Info("Also should add:", gopath)
|
|
|
|
rv.NewRepo(gopath)
|
|
|
|
}
|
|
|
|
return count
|
|
|
|
}
|