2024-03-07 00:50:58 -06:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-03-07 19:30:09 -06:00
|
|
|
"fmt"
|
|
|
|
"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"
|
|
|
|
"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-03-07 00:50:58 -06:00
|
|
|
|
2024-03-07 16:45:49 -06:00
|
|
|
var rv *repolist.RepoList
|
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-03-21 22:47:17 -05:00
|
|
|
// figures out where you're go.work file is
|
2024-03-07 19:30:09 -06:00
|
|
|
wdir, err := findWorkFile()
|
|
|
|
if err != nil {
|
|
|
|
log.Info(err)
|
|
|
|
os.Exit(-1)
|
2024-03-07 00:50:58 -06:00
|
|
|
}
|
2024-03-09 16:49:27 -06:00
|
|
|
log.Info("scanning directory:", wdir)
|
2024-03-07 19:30:09 -06:00
|
|
|
os.Setenv("REPO_WORK_PATH", wdir)
|
|
|
|
|
|
|
|
// readControlFile()
|
2024-03-07 00:50:58 -06:00
|
|
|
|
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-08 06:45:25 -06:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2024-03-07 22:07:40 -06:00
|
|
|
os.Setenv("REPO_AUTO_CLONE", "true")
|
2024-11-07 07:03:51 -06:00
|
|
|
newr, err := rv.NewRepo(argv.Repo)
|
2024-03-08 14:41:22 -06:00
|
|
|
if err != nil {
|
|
|
|
log.Info("could not download:", err)
|
|
|
|
os.Exit(-1)
|
|
|
|
}
|
|
|
|
newr.Status.MakeRedomod()
|
2024-03-07 16:45:49 -06:00
|
|
|
|
2024-11-07 07:03:51 -06:00
|
|
|
fullgitdir := filepath.Join(wdir, argv.Repo, ".git")
|
|
|
|
if shell.IsDir(fullgitdir) {
|
|
|
|
log.Info("repo already cloned", filepath.Join(wdir, argv.Repo))
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Info("scanning for repo in:", filepath.Join(wdir, argv.Repo))
|
|
|
|
|
2024-03-07 19:30:09 -06:00
|
|
|
// rv.NewRepo("go.wit.com/apps/helloworld")
|
2024-03-09 09:23:04 -06:00
|
|
|
for _, path := range repostatus.ScanGitDirectories(wdir) {
|
|
|
|
gopath := strings.TrimPrefix(path, wdir)
|
|
|
|
gopath = strings.Trim(gopath, "/")
|
|
|
|
// log.Info("Also should add:", gopath)
|
|
|
|
rv.NewRepo(gopath)
|
|
|
|
}
|
|
|
|
|
|
|
|
godep := newr.Status.GetGoDeps()
|
2024-11-07 07:03:51 -06:00
|
|
|
if argv.Recursive {
|
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
|
|
|
|
}
|
|
|
|
repo.Status.MakeRedomod()
|
2024-03-09 22:00:10 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-21 19:43:26 -05:00
|
|
|
var count int
|
2024-03-09 22:00:10 -06:00
|
|
|
for _, repo := range rv.AllRepos() {
|
2024-03-21 19:43:26 -05:00
|
|
|
count += 1
|
|
|
|
if !repo.Status.Exists("go.mod") {
|
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-03-21 19:43:26 -05:00
|
|
|
log.Info("Total repositories:", count)
|
2024-11-07 07:03:51 -06:00
|
|
|
log.Info("Finished go-clone for", argv.Repo)
|
|
|
|
if !argv.NoWork {
|
2024-03-09 22:00:10 -06:00
|
|
|
log.Info("Creating", wdir+"/go.work")
|
2024-03-09 16:49:27 -06:00
|
|
|
rv.MakeGoWork()
|
2024-03-09 22:00:10 -06:00
|
|
|
shell.RunPath(wdir, []string{"go", "work", "use"})
|
2024-03-07 16:45:49 -06:00
|
|
|
}
|
2024-03-07 00:50:58 -06:00
|
|
|
}
|
2024-03-07 16:45:49 -06:00
|
|
|
|
2024-03-07 19:30:09 -06:00
|
|
|
// look for or make a go.work file
|
|
|
|
// otherwise use ~/go/src
|
|
|
|
func findWorkFile() (string, error) {
|
2024-11-07 07:03:51 -06:00
|
|
|
if argv.GoSrc {
|
2024-03-21 22:47:17 -05:00
|
|
|
// user put --go-src on the command line so use ~/go/src
|
|
|
|
return useGoSrc()
|
|
|
|
}
|
|
|
|
|
2024-03-07 19:30:09 -06:00
|
|
|
pwd, err := os.Getwd()
|
|
|
|
if err == nil {
|
|
|
|
// Check for go.work in the current directory and then move up until root
|
2024-03-09 09:23:04 -06:00
|
|
|
if pwd, err := digup(pwd); err == nil {
|
|
|
|
// found an existing go.work file
|
2024-03-07 19:30:09 -06:00
|
|
|
os.Chdir(pwd)
|
|
|
|
return pwd, nil
|
|
|
|
}
|
|
|
|
|
2024-03-09 09:23:04 -06:00
|
|
|
// if the user added '--work' on the cmdline, make a work directory and init the go.work file
|
2024-11-07 07:03:51 -06:00
|
|
|
if !argv.NoWork {
|
2024-03-09 09:23:04 -06:00
|
|
|
pwd, err = os.Getwd()
|
|
|
|
newpwd := filepath.Join(pwd, "work")
|
|
|
|
shell.Mkdir(newpwd)
|
|
|
|
os.Chdir(newpwd)
|
2024-03-07 19:30:09 -06:00
|
|
|
if _, err := os.Stat("go.work"); err == nil {
|
2024-03-09 09:23:04 -06:00
|
|
|
return newpwd, nil
|
2024-03-07 19:30:09 -06:00
|
|
|
}
|
2024-03-09 09:23:04 -06:00
|
|
|
shell.RunPath(newpwd, []string{"go", "work", "init"})
|
2024-03-07 19:30:09 -06:00
|
|
|
if shell.Exists("go.work") {
|
2024-03-09 09:23:04 -06:00
|
|
|
return newpwd, nil
|
2024-03-07 19:30:09 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-09 09:23:04 -06:00
|
|
|
// there are no go.work files, resume the ~/go/src behavior from prior to golang 1.22
|
2024-03-21 22:47:17 -05:00
|
|
|
return useGoSrc()
|
|
|
|
}
|
|
|
|
|
|
|
|
// this is the 'old way" and works fine for me. I use it because I like the ~/go/src directory
|
|
|
|
// because I know exactly what is in it: GO stuff & nothing else
|
|
|
|
func useGoSrc() (string, error) {
|
2024-03-07 19:30:09 -06:00
|
|
|
homeDir, err := os.UserHomeDir()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2024-03-21 22:47:17 -05:00
|
|
|
pwd := filepath.Join(homeDir, "go/src")
|
2024-03-07 19:30:09 -06:00
|
|
|
shell.Mkdir(pwd)
|
|
|
|
os.Chdir(pwd)
|
|
|
|
return pwd, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func digup(path string) (string, error) {
|
|
|
|
for {
|
|
|
|
workFilePath := filepath.Join(path, "go.work")
|
|
|
|
if _, err := os.Stat(workFilePath); err == nil {
|
|
|
|
return path, nil // Found the go.work file
|
|
|
|
} else if !os.IsNotExist(err) {
|
|
|
|
return "", err // An error other than not existing
|
|
|
|
}
|
|
|
|
|
|
|
|
parentPath := filepath.Dir(path)
|
|
|
|
if parentPath == path {
|
|
|
|
break // Reached the filesystem root
|
|
|
|
}
|
|
|
|
path = parentPath
|
2024-03-07 16:45:49 -06:00
|
|
|
}
|
|
|
|
|
2024-03-07 19:30:09 -06:00
|
|
|
return "", fmt.Errorf("no go.work file found")
|
|
|
|
}
|
2024-11-08 06:45:25 -06:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|