2024-12-10 14:07:14 -06:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-12-13 02:21:25 -06:00
|
|
|
"errors"
|
2024-12-10 14:07:14 -06:00
|
|
|
"os"
|
2024-12-11 00:03:08 -06:00
|
|
|
"strings"
|
2024-12-10 14:07:14 -06:00
|
|
|
|
|
|
|
"go.wit.com/dev/alexflint/arg"
|
|
|
|
"go.wit.com/lib/protobuf/forgepb"
|
|
|
|
"go.wit.com/lib/protobuf/gitpb"
|
|
|
|
"go.wit.com/log"
|
|
|
|
)
|
|
|
|
|
|
|
|
// sent via -ldflags
|
|
|
|
var VERSION string
|
|
|
|
var BUILDTIME string
|
|
|
|
|
|
|
|
var pp *arg.Parser
|
|
|
|
var forge *forgepb.Forge
|
|
|
|
var check *gitpb.Repo
|
|
|
|
|
|
|
|
func main() {
|
2024-12-12 18:59:16 -06:00
|
|
|
log.Info("go-mod-clean version", VERSION, "built on", BUILDTIME)
|
2024-12-10 14:07:14 -06:00
|
|
|
pp = arg.MustParse(&argv)
|
|
|
|
|
|
|
|
// load the ~/.config/forge/ config
|
|
|
|
// this lets you configure repos you have read/write access too
|
|
|
|
forge = forgepb.Init()
|
|
|
|
|
2024-12-11 00:03:08 -06:00
|
|
|
// figure out what directory we are running in
|
|
|
|
check = findPwdRepo()
|
2024-12-10 14:07:14 -06:00
|
|
|
if check == nil {
|
2024-12-11 00:03:08 -06:00
|
|
|
log.Info("this directory isn't in a golang project (not in ~/go/src nor a go.work file)")
|
|
|
|
os.Exit(-1)
|
|
|
|
}
|
2024-12-13 02:21:25 -06:00
|
|
|
|
2024-12-13 10:44:11 -06:00
|
|
|
if err := check.ValidGoSum(); err == nil {
|
|
|
|
okExit("go.mod and go.sum are already valid")
|
|
|
|
}
|
|
|
|
|
2024-12-13 02:21:25 -06:00
|
|
|
// skip restore if --force
|
|
|
|
if !argv.Force {
|
2024-12-13 13:52:35 -06:00
|
|
|
cname := check.GetCurrentBranchName()
|
2024-12-13 02:21:25 -06:00
|
|
|
// try to restore from the git metadata
|
2024-12-13 13:52:35 -06:00
|
|
|
if err := check.AutogenRestore(cname); err != nil {
|
|
|
|
// ignore errors here
|
2024-12-13 12:34:47 -06:00
|
|
|
}
|
|
|
|
if err := check.ValidGoSum(); err == nil {
|
|
|
|
okExit("go.mod and go.sum were restored ok")
|
2024-12-11 00:03:08 -06:00
|
|
|
}
|
2024-12-13 02:21:25 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
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"))
|
2024-12-10 14:07:14 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// re-create go.sum and go.mod
|
2024-12-13 02:21:25 -06:00
|
|
|
if _, err := redoGoMod(check); err != nil {
|
2024-12-11 00:03:08 -06:00
|
|
|
badExit(err)
|
|
|
|
}
|
|
|
|
|
2024-12-13 02:21:25 -06:00
|
|
|
if argv.Trim {
|
|
|
|
// try to trim junk
|
|
|
|
if err := trimGoSum(check); err != nil {
|
|
|
|
badExit(err)
|
|
|
|
}
|
2024-12-11 13:51:06 -06:00
|
|
|
}
|
|
|
|
|
2024-12-11 00:03:08 -06:00
|
|
|
// check go.sum file
|
2024-12-13 02:21:25 -06:00
|
|
|
if err := cleanGoDepsCheckOk(check); err != nil {
|
2024-12-11 01:14:24 -06:00
|
|
|
log.Info("forge.FinalGoDepsCheck() failed. boo. :", check.GoPath)
|
|
|
|
badExit(err)
|
2024-12-11 00:03:08 -06:00
|
|
|
}
|
2024-12-11 13:51:06 -06:00
|
|
|
|
2024-12-13 02:21:25 -06:00
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
2024-12-11 13:51:06 -06:00
|
|
|
log.Info("forge.FinalGoDepsCheck() worked :", check.GoPath)
|
|
|
|
okExit(check.GoPath + " go.sum seems clean")
|
2024-12-11 00:03:08 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func findPwdRepo() *gitpb.Repo {
|
|
|
|
var check *gitpb.Repo
|
|
|
|
// attempt to use the working directory
|
|
|
|
// this is probably what happens most of the time
|
|
|
|
pwd, _ := os.Getwd()
|
|
|
|
if strings.HasPrefix(pwd, forge.GetGoSrc()) {
|
|
|
|
gopath := strings.TrimPrefix(pwd, forge.GetGoSrc())
|
|
|
|
gopath = strings.Trim(gopath, "/")
|
|
|
|
log.Info("findRepo() trying gopath", gopath)
|
|
|
|
check = forge.Repos.FindByGoPath(gopath)
|
|
|
|
if check != nil {
|
|
|
|
log.Info("findRepo() worked", check.GoPath)
|
|
|
|
return check
|
|
|
|
}
|
2024-12-10 14:07:14 -06:00
|
|
|
}
|
2024-12-11 00:03:08 -06:00
|
|
|
return nil
|
2024-12-10 14:07:14 -06:00
|
|
|
}
|
|
|
|
|
2024-12-13 10:44:11 -06:00
|
|
|
func okExit(msg string) {
|
|
|
|
log.Info("exit() go-mod-clean on", check.GetGoPath(), "ok")
|
2024-12-13 02:21:25 -06:00
|
|
|
log.DaemonMode(true)
|
2024-12-13 10:44:11 -06:00
|
|
|
log.Info(msg)
|
2024-12-10 14:07:14 -06:00
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
func badExit(err error) {
|
2024-12-13 02:21:25 -06:00
|
|
|
log.DaemonMode(true)
|
2024-12-12 18:59:16 -06:00
|
|
|
log.Info("go-mod-clean failed: ", err, forge.GetGoSrc())
|
2024-12-10 14:07:14 -06:00
|
|
|
os.Exit(-1)
|
|
|
|
}
|
2024-12-13 02:21:25 -06:00
|
|
|
|
|
|
|
func saveAsMetadata(repo *gitpb.Repo) error {
|
|
|
|
cname := check.GetCurrentBranchName()
|
|
|
|
if check.GoPrimitive {
|
2024-12-13 12:34:47 -06:00
|
|
|
if err := check.AutogenSave([]string{"go.mod"}, cname, true); err != nil {
|
2024-12-13 02:21:25 -06:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
2024-12-13 12:34:47 -06:00
|
|
|
if err := check.AutogenSave([]string{"go.mod", "go.sum"}, cname, true); err != nil {
|
2024-12-13 02:21:25 -06:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|