go-clone/main.go

144 lines
3.3 KiB
Go
Raw Normal View History

2024-03-07 00:50:58 -06:00
package main
import (
2024-03-07 19:30:09 -06:00
"os"
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
var workingRepo *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)
}
// this package helps scan git repos
2024-11-30 12:44:29 -06:00
forge = forgepb.Init()
var err error
// attempt to clone, returns *gitpb.Repo
workingRepo, err = clone(argv.Repo)
if err != nil {
badExit(err)
2024-11-30 12:44:29 -06:00
}
if argv.Recursive {
log.Info("STARTING RECURSIVE CLONE", workingRepo.GoPath)
if err := recursiveClone(workingRepo); err != nil {
badExit(err)
}
2024-11-30 12:44:29 -06:00
}
autoWork()
if argv.Build {
if err := build(); err != nil {
2024-12-01 22:21:34 -06:00
badExit(err)
}
2024-11-30 12:44:29 -06:00
}
if argv.Install {
// can only install binary or plugin go packages
if workingRepo.RepoType() == "binary" || workingRepo.RepoType() == "plugin" {
log.Info("install will probably fail", workingRepo.GoPath, "is", workingRepo.RepoType())
}
if err := forge.Install(workingRepo, nil); err != nil {
log.Warn("INSTALL FAILED", workingRepo.GoPath, err)
2024-12-01 22:21:34 -06:00
badExit(err)
}
}
if argv.Pull {
// run 'git pull' if argv --git-pull
gitPull()
}
okExit("")
2024-11-30 12:44:29 -06:00
}
func okExit(thing string) {
if thing != "" {
log.Info(thing, "ok")
}
log.Info("Finished clone on", workingRepo.GetGoPath(), "ok")
forge.ConfigSave()
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 gitPull() {
log.Info("Total repositories:", forge.Repos.Len())
log.Info("Going to run git pull in each one. TODO: use rill here")
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-01 22:21:34 -06:00
func build() error {
err := forge.Build(workingRepo, 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")
badExit(err)
2024-11-30 12:44:29 -06:00
}
return err
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")
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
}