go-clone/main.go

213 lines
5.0 KiB
Go

package main
import (
"fmt"
"os"
"path/filepath"
"strings"
"go.wit.com/dev/alexflint/arg"
"go.wit.com/gui"
"go.wit.com/lib/gui/repolist"
"go.wit.com/lib/gui/repostatus"
"go.wit.com/lib/gui/shell"
"go.wit.com/log"
)
// sent via -ldflags
var VERSION string
var rv *repolist.RepoList
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)
}
// figures out where you're go.work file is
wdir, err := findWorkFile()
if err != nil {
log.Info(err)
os.Exit(-1)
}
log.Info("scanning directory:", wdir)
os.Setenv("REPO_WORK_PATH", wdir)
// readControlFile()
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 {
log.Info("could not download:", err)
os.Exit(-1)
}
newr.Status.MakeRedomod()
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))
// rv.NewRepo("go.wit.com/apps/helloworld")
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()
if argv.Recursive {
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()
}
}
var count int
for _, repo := range rv.AllRepos() {
count += 1
if !repo.Status.Exists("go.mod") {
repo.Status.MakeRedomod()
}
}
log.Info("Total repositories:", count)
log.Info("Finished go-clone for", argv.Repo)
if !argv.NoWork {
log.Info("Creating", wdir+"/go.work")
rv.MakeGoWork()
shell.RunPath(wdir, []string{"go", "work", "use"})
}
}
// look for or make a go.work file
// otherwise use ~/go/src
func findWorkFile() (string, error) {
if argv.GoSrc {
// user put --go-src on the command line so use ~/go/src
return useGoSrc()
}
pwd, err := os.Getwd()
if err == nil {
// Check for go.work in the current directory and then move up until root
if pwd, err := digup(pwd); err == nil {
// found an existing go.work file
os.Chdir(pwd)
return pwd, nil
}
// if the user added '--work' on the cmdline, make a work directory and init the go.work file
if !argv.NoWork {
pwd, err = os.Getwd()
newpwd := filepath.Join(pwd, "work")
shell.Mkdir(newpwd)
os.Chdir(newpwd)
if _, err := os.Stat("go.work"); err == nil {
return newpwd, nil
}
shell.RunPath(newpwd, []string{"go", "work", "init"})
if shell.Exists("go.work") {
return newpwd, nil
}
}
}
// there are no go.work files, resume the ~/go/src behavior from prior to golang 1.22
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) {
homeDir, err := os.UserHomeDir()
if err != nil {
return "", err
}
pwd := filepath.Join(homeDir, "go/src")
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
}
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
}