summaryrefslogtreecommitdiff
path: root/main.go
diff options
context:
space:
mode:
authorJeff Carr <[email protected]>2024-11-30 12:44:29 -0600
committerJeff Carr <[email protected]>2024-11-30 12:44:29 -0600
commitfa98d97dfba46bac5590ee98d0493640b000d883 (patch)
treeacf31c5b4f79838945db12c5b18b4127b8076866 /main.go
parentdd258155dfedcbfb17b4c9903e967fc394c83e6a (diff)
prepare to use go-clone to buildv0.6.6
Diffstat (limited to 'main.go')
-rw-r--r--main.go239
1 files changed, 115 insertions, 124 deletions
diff --git a/main.go b/main.go
index 1d2768f..bcfc075 100644
--- a/main.go
+++ b/main.go
@@ -1,6 +1,7 @@
package main
import (
+ "errors"
"os"
"path/filepath"
@@ -16,91 +17,94 @@ import (
var VERSION string
var BUILDTIME string
+var pp *arg.Parser
+var forge *forgepb.Forge
var rv *repolist.RepoList
-var goSrcPath string
func main() {
- pp := arg.MustParse(&argv)
+ 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 == "?" {
+ if argv.Repo == "help" || argv.Repo == "?" {
pp.WriteHelp(os.Stdout)
os.Exit(0)
}
+ if argv.Repo == "version" {
+ log.Info(argv.Version())
+ os.Exit(0)
+ }
// load the ~/.config/forge/ config
- forge := forgepb.Init()
+ // this lets you configure repos you have read/write access too
+ forge = forgepb.Init()
forge.ConfigPrintTable()
os.Setenv("REPO_WORK_PATH", forge.GetGoSrc())
- if argv.Repo == "" {
- // if there isn't anything else, just exit here
- // if --git-pull, continue
- if argv.Pull || argv.RedoGoMod {
- // there is more to do
- log.Info("onward and upward")
- } else {
- // user needs to pick something to do
- pp.WriteHelp(os.Stdout)
- log.Info("give me something to do!")
- os.Exit(0)
- }
- } else {
- // the user specified a repo, check if it's already cloned
- fullgitdir := filepath.Join(goSrcPath, argv.Repo, ".git")
- if shell.IsDir(fullgitdir) {
- // if --recursive, continue
- // if --git-pull, continue
- if argv.Recursive || argv.Pull || argv.RedoGoMod {
- log.Info("repo already cloned", filepath.Join(goSrcPath, argv.Repo))
- // there is more to do
- log.Info("argv.Recursive is", argv.Recursive)
- log.Info("argv.Pull is", argv.Pull)
- log.Info("argv.RedoGoMod is", argv.RedoGoMod)
- log.Info("onward and upward")
- } else {
- log.Info("repo already cloned", filepath.Join(goSrcPath, argv.Repo))
- os.Exit(0)
- }
- } else {
- // need to download this new repo!
- log.Info("repo is new. going to clone it to:", filepath.Join(goSrcPath, argv.Repo))
- }
- }
-
- // testing gui idea
+ // gui is in testing
myGui := gui.New()
// myGui.Default()
+ // find and scan all repos
rv := repolist.Init(forge, myGui)
rv.Enable()
-
rv.ScanRepositories()
+ // 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 {
+ redoGoModAll()
+ }
+
+ // this works sometimes
+ if argv.Recursive {
+ recursiveClone()
+ autoWork()
+ build()
+ okExit("--recursive")
+ }
+
+ clone()
+ autoWork()
+ build()
+ if argv.Repo == "" {
+ pp.WriteHelp(os.Stdout)
+ os.Exit(0)
+ }
+ okExit(argv.Repo)
+}
+
+func okExit(thing string) {
+ log.Info("Total repositories:", forge.Repos.Len())
+ log.Info("Finished go-clone", thing, "ok")
+ os.Exit(0)
+}
+
+func badExit(err error) {
+ log.Info("Total repositories:", forge.Repos.Len())
+ log.Info("Finished go-clone with error", err)
+ 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")
// pb, _ := forge.NewGoPath(argv.Repo)
pb, err := forge.Clone(argv.Repo)
if err != nil {
- log.Info("could not download:", err)
- os.Exit(-1)
+ log.Info("could not download")
+ badExit(err)
}
newr, err := rv.AddRepo(pb)
if err != nil {
- log.Info("AddRepo() failed", err)
- os.Exit(-1)
- }
- if argv.Recursive || argv.Pull || argv.RedoGoMod {
- log.Info("repo already cloned", filepath.Join(goSrcPath, argv.Repo))
- // there is more to do
- log.Info("argv.Recursive is", argv.Recursive)
- log.Info("argv.Pull is", argv.Pull)
- log.Info("argv.RedoGoMod is", argv.RedoGoMod)
- log.Info("onward and upward")
- } else {
- log.Info("repo cloned worked to", filepath.Join(goSrcPath, argv.Repo))
- os.Exit(0)
+ log.Info("repolist.AddRepo() failed")
+ badExit(err)
}
// update go.sum and go.mod
@@ -109,57 +113,61 @@ func main() {
newr.MakeRedoMod()
// double check it actually downloaded
- fullgitdir := filepath.Join(goSrcPath, argv.Repo, ".git")
+ fullgitdir := filepath.Join(forge.GetGoSrc(), argv.Repo, ".git")
if !shell.IsDir(fullgitdir) {
- log.Info("repo cloned failed", filepath.Join(goSrcPath, argv.Repo))
- os.Exit(-1)
+ log.Info("repo cloned failed", filepath.Join(forge.GetGoSrc(), argv.Repo))
+ badExit(errors.New(fullgitdir + " was not created"))
+ }
+ 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")
}
+}
- os.Setenv("REPO_AUTO_CLONE", "false")
- // look recursively in your working directory for git repos
- totalcount := forge.Repos.Len()
-
- // if --git-pull, run git pull on everything here
- if argv.Pull {
- log.Info("Total repositories:", totalcount)
- log.Info("Going to run git pull in each one")
- log.Sleep(1)
- pull := []string{"git", "pull"}
+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
- loop := rv.ReposSortByName()
- 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 result := shell.PathRunRealtime(repo.Status.Path(), pull); result.Error != nil {
- log.Info("git pull error:", result.Error)
- errcount += 1
- }
- }
+ var trycount, errcount int
+ repos := forge.Repos.SortByPath()
+ 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
}
- log.Info("Total repositories:", totalcount, "Total attempted:", trycount, "Errors:", errcount)
- os.Exit(0)
}
+ log.Info("Total repositories:", forge.Repos.Len(), "Total attempted:", trycount, "Errors:", errcount)
+}
- // this is experiemental but works for me
+func recursiveClone() {
+ // this works sometimes
if argv.Recursive {
newr := rv.FindByName(argv.Repo)
if newr == nil {
log.Info("how did this repo still not exist?", argv.Repo)
- os.Exit(-1)
+ badExit(errors.New("failed to clone repo: " + argv.Repo))
}
os.Setenv("REPO_AUTO_CLONE", "true")
godep := newr.Status.GetGoDeps()
for gopath, version := range godep {
pb, err := forge.Clone(gopath)
if err != nil {
- log.Info("could not download:", err)
- os.Exit(-1)
+ log.Info("could not download")
+ badExit(err)
}
repo, err := rv.AddRepo(pb)
if err != nil {
@@ -170,52 +178,35 @@ func main() {
repo.MakeRedoMod()
}
}
+}
- // remake all the go.sum & go.mod in every repo
- // todo: make go.sum and go.mod git commit metadata
- if argv.RedoGoMod {
- loop := rv.ReposSortByName()
- for loop.Scan() {
- repo := loop.Repo()
- repo.MakeRedoMod()
- }
+func redoGoModAll() {
+ loop := rv.ReposSortByName()
+ for loop.Scan() {
+ repo := loop.Repo()
+ repo.MakeRedoMod()
}
+}
+
+func build() {
+ if argv.Build {
+ log.Info("need to try to build here")
+ }
+}
+func autoWork() {
// remake the go.work file
if argv.AutoWork {
- log.Info("About to re-create", goSrcPath+"/go.work")
+ log.Info("About to re-create", forge.GetGoSrc()+"/go.work")
log.Info("Sleep 3. original go.work saved as go.work.last (hit ctrl-c to cancel)")
log.Sleep(3)
- shell.PathRun(goSrcPath, []string{"mv", "go.work", "go.work.last"})
+ shell.PathRun(forge.GetGoSrc(), []string{"mv", "go.work", "go.work.last"})
rv.MakeGoWork()
- shell.PathRun(goSrcPath, []string{"go", "work", "use"})
+ shell.PathRun(forge.GetGoSrc(), []string{"go", "work", "use"})
log.Info("")
log.Info("original go.work file saved as go.work.last")
log.Info("")
+ okExit("go.work create")
}
- log.Info("Total repositories:", totalcount)
- log.Info("Finished go-clone for", argv.Repo)
-}
-
-/*
-func scanForRepos(goSrcPath string) int {
- var count int
- log.Info("scanning for repo in:", filepath.Join(goSrcPath, argv.Repo))
-
- // rv.NewRepo("go.wit.com/apps/helloworld")
- for _, path := range repostatus.ScanGitDirectories(goSrcPath) {
- count += 1
- gopath := strings.TrimPrefix(path, goSrcPath)
- gopath = strings.Trim(gopath, "/")
- // log.Info("Also should add:", gopath)
- pb, err := forge.Clone(gopath)
- if err != nil {
- log.Info("could not download:", err)
- os.Exit(-1)
- }
- repo, err := rv.AddRepo(pb)
- }
- return count
}
-*/