summaryrefslogtreecommitdiff
path: root/main.go
diff options
context:
space:
mode:
authorJeff Carr <[email protected]>2024-12-13 16:16:18 -0600
committerJeff Carr <[email protected]>2024-12-13 16:16:18 -0600
commit8c68cff7624dd797533122509a7d86ed316090c5 (patch)
tree5cae23d1f096a619180533c24f19380970cca121 /main.go
parent02bbd75ce1d80f845673c30a004e35f9ce491ab6 (diff)
don't work on non-master branches
Diffstat (limited to 'main.go')
-rw-r--r--main.go166
1 files changed, 97 insertions, 69 deletions
diff --git a/main.go b/main.go
index 5371daf..8febccd 100644
--- a/main.go
+++ b/main.go
@@ -17,9 +17,12 @@ var BUILDTIME string
var pp *arg.Parser
var forge *forgepb.Forge
-var check *gitpb.Repo
+
+// var check *gitpb.Repo
+var configSave bool
func main() {
+ var check *gitpb.Repo
log.Info("go-mod-clean version", VERSION, "built on", BUILDTIME)
pp = arg.MustParse(&argv)
@@ -27,66 +30,35 @@ func main() {
// this lets you configure repos you have read/write access too
forge = forgepb.Init()
- // figure out what directory we are running in
- check = findPwdRepo()
- if check == nil {
- log.Info("this directory isn't in a golang project (not in ~/go/src nor a go.work file)")
- os.Exit(-1)
- }
-
- if err := check.ValidGoSum(); err == nil {
- okExit("go.mod and go.sum are already valid")
- }
-
- // skip restore if --force
- if !argv.Force {
- cname := check.GetCurrentBranchName()
- // try to restore from the git metadata
- if err := check.AutogenRestore(cname); err != nil {
- // ignore errors here
+ if argv.All {
+ // run this on every single repo
+ // do this before publishing new golang versions
+ all := forge.Repos.SortByGoPath()
+ for all.Scan() {
+ check = all.Next()
+ if err := doMain(check); err != nil {
+ badExit(check, err)
+ }
}
- if err := check.ValidGoSum(); err == nil {
- okExit("go.mod and go.sum were restored ok")
+ } else {
+ // figure out what directory we are running in
+ check := findPwdRepo()
+ if check == nil {
+ log.Info("this directory isn't in a golang project (not in ~/go/src nor a go.work file)")
+ os.Exit(-1)
}
- }
-
- if check.GetMasterBranchName() != check.GetCurrentBranchName() {
- log.Info("")
- log.Info("You can only run go-mod-clean on a git master branch.")
- log.Info("Publishing go.mod & go.sum files must come from from git version tag")
- log.Info("Anything else doesn't make sense.")
- log.Info("")
- badExit(errors.New("not git master branch"))
- }
- // re-create go.sum and go.mod
- if _, err := redoGoMod(check); err != nil {
- badExit(err)
- }
-
- if argv.Trim {
- // try to trim junk
- if err := trimGoSum(check); err != nil {
- badExit(err)
+ if err := doMain(check); err != nil {
+ badExit(check, err)
}
}
- // check go.sum file
- if err := cleanGoDepsCheckOk(check); err != nil {
- log.Info("forge.FinalGoDepsCheck() failed. boo. :", check.GoPath)
- badExit(err)
- }
-
- // put the files in the notes section in git
- // this way, git commits are not messed up
- // with this autogenerated code
- if err := saveAsMetadata(check); err != nil {
- log.Info("save go.mod as git metadata failed", check.GoPath, err)
- badExit(err)
+ if configSave {
+ forge.ConfigSave()
}
log.Info("forge.FinalGoDepsCheck() worked :", check.GoPath)
- okExit(check.GoPath + " go.sum seems clean")
+ okExit(check, "go.sum seems clean")
}
func findPwdRepo() *gitpb.Repo {
@@ -107,29 +79,85 @@ func findPwdRepo() *gitpb.Repo {
return nil
}
-func okExit(msg string) {
- log.Info("exit() go-mod-clean on", check.GetGoPath(), "ok")
- log.DaemonMode(true)
- log.Info(msg)
- os.Exit(0)
-}
-
-func badExit(err error) {
- log.DaemonMode(true)
- log.Info("go-mod-clean failed: ", err, forge.GetGoSrc())
- os.Exit(-1)
-}
-
func saveAsMetadata(repo *gitpb.Repo) error {
- cname := check.GetCurrentBranchName()
- if check.GoPrimitive {
- if err := check.AutogenSave([]string{"go.mod"}, cname, true); err != nil {
+ cname := repo.GetCurrentBranchName()
+ if repo.GoPrimitive {
+ if err := repo.AutogenSave([]string{"go.mod"}, cname, true); err != nil {
return err
}
} else {
- if err := check.AutogenSave([]string{"go.mod", "go.sum"}, cname, true); err != nil {
+ if err := repo.AutogenSave([]string{"go.mod", "go.sum"}, cname, true); err != nil {
return err
}
}
return nil
}
+
+func doMain(repo *gitpb.Repo) error {
+ if !repo.IsValid() {
+ log.Info(repo.GoPath, "is invalid. fix your repos.pb file with 'forge' first")
+ log.Info("")
+ log.Info("go install go.wit.com/apps/forge@latest")
+ log.Info("")
+ return errors.New(repo.GoPath + " is invalid. fix your repository list with 'forge' first")
+ } else {
+ log.Info(repo.GoPath, "is valid according to forge")
+ }
+ if err := repo.ValidGoSum(); err == nil {
+ log.Info(repo.GoPath, "go.mod and go.sum are already valid")
+ return nil
+ }
+
+ // skip restore if --force
+ if !argv.Force {
+ cname := repo.GetCurrentBranchName()
+ // try to restore from the git metadata
+ if err := repo.AutogenRestore(cname); err != nil {
+ // ignore errors here
+ }
+ if err := repo.ValidGoSum(); err == nil {
+ log.Info(repo.GoPath, "go.mod and go.sum were restored ok")
+ configSave = true
+ return nil
+ }
+ }
+
+ if repo.GetMasterBranchName() != repo.GetCurrentBranchName() {
+ log.Info("")
+ log.Info("You can only run go-mod-clean on a git master branch.")
+ log.Info("Publishing go.mod & go.sum files must come from from git version tag")
+ log.Info("Anything else doesn't make sense.")
+ log.Info("")
+ return errors.New(repo.GoPath + " not in the git master branch")
+ }
+
+ // re-create go.sum and go.mod
+ if err := redoGoMod(repo); err != nil {
+ return err
+ }
+
+ if argv.Trim {
+ // try to trim junk
+ if err := trimGoSum(repo); err != nil {
+ return err
+ }
+ }
+
+ // check go.sum file
+ if err := cleanGoDepsCheckOk(repo); err != nil {
+ log.Info("forge.FinalGoDepsCheck() failed. boo. :", repo.GoPath)
+ return err
+ }
+
+ // put the files in the notes section in git
+ // this way, git commits are not messed up
+ // with this autogenerated code
+ if err := saveAsMetadata(repo); err != nil {
+ log.Info("save go.mod as git metadata failed", repo.GoPath, err)
+ return err
+ }
+
+ // everything worked!
+ configSave = true
+ return nil
+}