use go-cmd/cmd

This commit is contained in:
Jeff Carr 2024-11-08 06:45:25 -06:00
parent 4cee81ca2d
commit 9878b8fe6c
3 changed files with 58 additions and 11 deletions

View File

@ -62,3 +62,12 @@ git-clone:
debian:
go-deb --no-gui --repo go.wit.com/apps/go-clone
pull: build
./go-clone --dry-run --pull
pullreal: build
./go-clone --pull
fetch: build
./go-clone --dry-run --fetch

View File

@ -14,6 +14,8 @@ type args struct {
GoSrc bool `arg:"--go-src" default:"true" help:"only work in ~/go/src"`
DryRun bool `arg:"--dry-run" help:"show what would be run"`
Recursive bool `arg:"--recursive" default:"false" help:"resursively clone all dependencies"`
Pull bool `arg:"--pull" default:"false" help:"run 'git pull' on all your repos"`
// Fetch bool `arg:"--fetch" default:"false" help:"run 'git fetch' on all your repos"`
}
func (a args) Description() string {

58
main.go
View File

@ -22,11 +22,6 @@ var rv *repolist.RepoList
func main() {
pp := arg.MustParse(&argv)
if argv.Repo == "" {
pp.WriteHelp(os.Stdout)
os.Exit(0)
}
// 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)
@ -47,6 +42,38 @@ func main() {
b := gui.RawBox()
rv = repolist.AutotypistView(b)
log.Info("got here")
if argv.Pull {
count := scanForRepos(wdir)
log.Info("Total repositories:", count)
log.Info("Going to run git pull in each one")
log.Sleep(1)
pull := []string{"git", "pull"}
loop := rv.ReposSortByName()
var trycount, errcount int
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 err := shell.PwdRun(repo.Status.Path(), pull); err != nil {
log.Info("git pull error:", err)
errcount += 1
}
}
}
log.Info("Total repositories:", count, "Total attempted:", trycount, "Errors:", errcount)
os.Exit(0)
}
// if the user didn't provide a repo, stop here
if argv.Repo == "" {
pp.WriteHelp(os.Stdout)
os.Exit(0)
}
os.Setenv("REPO_AUTO_CLONE", "true")
newr, err := rv.NewRepo(argv.Repo)
if err != nil {
@ -98,12 +125,6 @@ func main() {
rv.MakeGoWork()
shell.RunPath(wdir, []string{"go", "work", "use"})
}
/*
for _, repo := range rv.AllRepos() {
log.Info("found repo", repo.GoPath(), repo.Status.Path())
}
*/
}
// look for or make a go.work file
@ -174,3 +195,18 @@ func digup(path string) (string, error) {
return "", fmt.Errorf("no go.work file found")
}
func scanForRepos(wdir string) int {
var count int
log.Info("scanning for repo in:", filepath.Join(wdir, argv.Repo))
// rv.NewRepo("go.wit.com/apps/helloworld")
for _, path := range repostatus.ScanGitDirectories(wdir) {
count += 1
gopath := strings.TrimPrefix(path, wdir)
gopath = strings.Trim(gopath, "/")
// log.Info("Also should add:", gopath)
rv.NewRepo(gopath)
}
return count
}