go-clone/main.go

263 lines
6.2 KiB
Go
Raw Normal View History

2024-03-07 00:50:58 -06:00
package main
import (
2024-11-30 12:44:29 -06:00
"errors"
2024-03-07 19:30:09 -06:00
"os"
"path/filepath"
2024-03-07 16:45:49 -06:00
"go.wit.com/dev/alexflint/arg"
2024-03-07 00:50:58 -06:00
"go.wit.com/lib/gui/shell"
"go.wit.com/lib/protobuf/forgepb"
2024-12-01 10:42:32 -06:00
"go.wit.com/lib/protobuf/gitpb"
2024-03-07 00:50:58 -06:00
"go.wit.com/log"
)
2024-11-07 16:53:53 -06:00
// sent via -ldflags
var VERSION string
2024-11-15 10:52:00 -06:00
var BUILDTIME string
2024-03-07 00:50:58 -06:00
2024-11-30 12:44:29 -06:00
var pp *arg.Parser
var forge *forgepb.Forge
2024-12-01 10:42:32 -06:00
var argvRepo *gitpb.Repo
2024-03-07 00:50:58 -06:00
func main() {
2024-12-07 16:46:59 -06:00
log.Info("go-clone version", VERSION, "built on", BUILDTIME)
2024-11-30 12:44:29 -06:00
pp = arg.MustParse(&argv)
2024-03-07 16:45:49 -06:00
// for very new users or users unfamilar with the command line, this may help them
2024-11-30 12:44:29 -06:00
if argv.Repo == "help" || argv.Repo == "?" {
pp.WriteHelp(os.Stdout)
os.Exit(0)
}
2024-11-30 12:44:29 -06:00
if argv.Repo == "version" {
log.Info(argv.Version())
os.Exit(0)
}
2024-11-30 01:35:56 -06:00
// load the ~/.config/forge/ config
2024-11-30 12:44:29 -06:00
// this lets you configure repos you have read/write access too
forge = forgepb.Init()
2024-12-01 10:42:32 -06:00
argvRepo = forge.Repos.FindByGoPath(argv.Repo)
if argvRepo == nil {
2024-12-01 00:47:04 -06:00
log.Info("yep, need to clone", argv.Repo)
} else {
log.Info("already have", argv.Repo)
2024-12-03 13:23:35 -06:00
if argv.Recursive {
2024-12-07 16:46:59 -06:00
clone()
2024-12-03 13:23:35 -06:00
recursiveClone()
}
2024-12-07 16:46:59 -06:00
autoWork()
2024-12-01 00:47:04 -06:00
build()
okExit(argv.Repo)
}
2024-11-30 12:44:29 -06:00
// gui is in testing
2024-12-07 16:46:59 -06:00
// myGui := gui.New()
2024-11-30 01:35:56 -06:00
// myGui.Default()
2024-11-30 12:44:29 -06:00
// run 'git pull' if argv --git-pull
if argv.Pull {
gitPull()
okExit("git pull")
}
// remake all the go.sum & go.mod in every repo
// todo: make go.sum and go.mod git commit metadata
if argv.RedoGoMod {
2024-12-07 16:46:59 -06:00
// redoGoModAll()
2024-11-30 12:44:29 -06:00
}
// this works sometimes
if argv.Recursive {
2024-12-07 16:46:59 -06:00
clone()
2024-11-30 12:44:29 -06:00
recursiveClone()
autoWork()
build()
okExit("--recursive")
}
clone()
autoWork()
2024-12-01 22:21:34 -06:00
if argv.Install {
if err := forge.Install(argvRepo, nil); err == nil {
okExit("install worked")
} else {
badExit(err)
}
2024-11-30 12:44:29 -06:00
}
2024-12-01 22:21:34 -06:00
if argv.Build {
if err := forge.Build(argvRepo, nil); err == nil {
okExit("build worked")
2024-12-10 01:47:13 -06:00
argvRepo.RunEcho([]string{"ls", "-l"})
2024-12-01 22:21:34 -06:00
} else {
2024-12-10 01:47:13 -06:00
argvRepo.RunEcho([]string{"ls", "-l"})
2024-12-01 22:21:34 -06:00
badExit(err)
}
}
okExit("skipping build of " + argv.Repo)
2024-11-30 12:44:29 -06:00
}
func okExit(thing string) {
2024-12-01 22:21:34 -06:00
log.Info(thing, "ok")
log.Info("Finished clone on", argvRepo.GetGoPath(), "ok")
2024-11-30 12:44:29 -06:00
os.Exit(0)
}
func badExit(err error) {
log.Info("Total repositories:", forge.Repos.Len())
2024-12-07 16:46:59 -06:00
log.Info("Finished go-clone with error", err, forge.GetGoSrc())
2024-11-30 12:44:29 -06:00
os.Exit(-1)
}
func clone() {
// if the user defined a repo, attempt to download it now
if argv.Repo != "" {
os.Setenv("REPO_AUTO_CLONE", "true")
2024-11-30 01:35:56 -06:00
// pb, _ := forge.NewGoPath(argv.Repo)
2024-12-07 16:46:59 -06:00
check := forge.Repos.FindByGoPath(argv.Repo)
if check != nil {
return
}
2024-12-07 16:46:59 -06:00
pb, err := forge.Clone(argv.Repo)
2024-11-30 01:35:56 -06:00
if err != nil {
2024-12-07 16:46:59 -06:00
log.Info("clone() could not download err:", err)
2024-11-30 12:44:29 -06:00
badExit(err)
2024-11-17 16:04:08 -06:00
}
2024-12-13 12:34:31 -06:00
if err := pb.ValidGoSum(); err != nil {
// update go.sum and go.mod
if err := pb.RunStrict([]string{"go-mod-clean"}); err != nil {
log.Info("")
log.Info("Do you have go-mod-clean? Otherwise:")
log.Info(" go install go.wit.com/apps/go-mod-clean@latest")
log.Info("")
badExit(err)
}
}
if err := pb.ValidGoSum(); err != nil {
log.Info("could not generate valid go.sum file")
badExit(err)
}
// double check it actually downloaded
2024-11-30 12:44:29 -06:00
fullgitdir := filepath.Join(forge.GetGoSrc(), argv.Repo, ".git")
if !shell.IsDir(fullgitdir) {
2024-11-30 12:44:29 -06:00
log.Info("repo cloned failed", filepath.Join(forge.GetGoSrc(), argv.Repo))
badExit(errors.New(fullgitdir + " was not created"))
}
2024-11-30 12:44:29 -06:00
build()
// exit unless other --argv options are set
if !(argv.Recursive || argv.Pull || argv.RedoGoMod) {
log.Info("repo cloned worked to", filepath.Join(forge.GetGoSrc(), argv.Repo))
okExit(argv.Repo)
}
log.Info("onward and upward")
}
2024-11-30 12:44:29 -06:00
}
2024-11-30 12:44:29 -06:00
func gitPull() {
log.Info("Total repositories:", forge.Repos.Len())
log.Info("Going to run git pull in each one. TODO: use rill here")
log.Sleep(1)
pull := []string{"git", "pull"}
var trycount, errcount int
2024-11-30 15:32:42 -06:00
repos := forge.Repos.SortByGoPath()
2024-11-30 12:44:29 -06:00
for repos.Scan() {
repo := repos.Next()
if argv.DryRun {
log.Info("git pull --dry-run", repo.GoPath)
continue
}
log.Info("git pull:", repo.FullPath)
trycount += 1
log.Info("actually run: git pull:", repo.GoPath)
if result := shell.PathRunRealtime(repo.FullPath, pull); result.Error != nil {
log.Info("git pull error:", result.Error)
errcount += 1
2024-11-08 06:45:25 -06:00
}
}
2024-11-30 12:44:29 -06:00
log.Info("Total repositories:", forge.Repos.Len(), "Total attempted:", trycount, "Errors:", errcount)
}
2024-11-08 06:45:25 -06:00
2024-12-03 13:23:35 -06:00
// really only does go.sum things
// so not 'really' recursive
// but that is because go.sum is supposed
// to have everything required in it
2024-11-30 12:44:29 -06:00
func recursiveClone() {
2024-12-03 13:23:35 -06:00
var good int
var bad int
2024-11-30 12:44:29 -06:00
// this works sometimes
if argv.Recursive {
2024-12-03 13:23:35 -06:00
check := forge.Repos.FindByGoPath(argv.Repo)
log.Info("download deps for:", check.GoPath)
deps := check.GoDeps.SortByGoPath()
for deps.Scan() {
depRepo := deps.Next()
log.Info("download:", depRepo.GoPath)
_, err := forge.Clone(depRepo.GoPath)
2024-03-21 19:43:26 -05:00
if err != nil {
2024-12-07 16:46:59 -06:00
log.Info("recursiveClone() could not download", depRepo.GoPath)
2024-12-03 13:23:35 -06:00
log.Info("err:", err)
bad += 1
} else {
log.Info("downloaded", depRepo.GoPath)
good += 1
2024-03-21 19:43:26 -05:00
}
2024-03-09 22:00:10 -06:00
}
}
2024-12-03 13:23:35 -06:00
log.Info("got", good, "repos", "failed on", bad, "repos")
2024-11-30 12:44:29 -06:00
}
2024-03-09 22:00:10 -06:00
2024-12-01 22:21:34 -06:00
func build() error {
2024-12-07 16:46:59 -06:00
forge.RillRedoGoMod()
repos := forge.Repos.SortByGoPath()
for repos.Scan() {
repo := repos.Next()
log.Info("go.work repo (hopefully):", repo.GoPath, repo.FullPath, repo.RepoType())
}
2024-12-01 22:21:34 -06:00
if argv.Install {
if err := forge.Install(argvRepo, nil); err == nil {
okExit("install worked")
} else {
badExit(err)
}
}
2024-11-30 12:44:29 -06:00
if argv.Build {
2024-12-10 01:47:13 -06:00
err := forge.Build(argvRepo, nil)
pwd, _ := os.Getwd()
if err == nil {
log.Info("this totally worked", pwd)
shell.RunEcho([]string{"ls", "-l"})
log.Info("ran ls")
} else {
log.Info("this totally did not work", pwd)
shell.RunEcho([]string{"ls", "-l"})
log.Info("ran ls")
}
return err
2024-11-30 12:44:29 -06:00
}
2024-12-01 22:21:34 -06:00
log.Info("skipping build")
return nil
2024-11-30 12:44:29 -06:00
}
func autoWork() {
// remake the go.work file
if argv.AutoWork {
2024-11-30 12:44:29 -06:00
log.Info("About to re-create", forge.GetGoSrc()+"/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-30 12:44:29 -06:00
shell.PathRun(forge.GetGoSrc(), []string{"mv", "go.work", "go.work.last"})
2024-12-07 16:46:59 -06:00
forge.MakeGoWork()
2024-11-30 12:44:29 -06:00
shell.PathRun(forge.GetGoSrc(), []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-11-30 12:44:29 -06:00
okExit("go.work create")
2024-03-07 16:45:49 -06:00
}
2024-11-08 06:45:25 -06:00
}