go-mod-clean/main.go

125 lines
2.6 KiB
Go
Raw Permalink Normal View History

2024-12-10 14:07:14 -06:00
package main
import (
"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
2024-12-18 00:41:52 -06:00
var golangVersion string = "1.21"
2024-12-10 14:07:14 -06:00
var pp *arg.Parser
var forge *forgepb.Forge
2024-12-13 16:16:18 -06:00
// var check *gitpb.Repo
var configSave bool
2024-12-10 14:07:14 -06:00
func main() {
2024-12-13 16:16:18 -06:00
var check *gitpb.Repo
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-18 01:29:07 -06:00
// 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)")
badExit(nil, nil)
}
// deletes all the git notes
if argv.Purge {
purgeNotes(check)
okExit(check, "notes gone")
}
if argv.Restore {
// attempt to restore from ~/go/pkg/mod/
if err := restoreFromGoPkg(check); err != nil {
badExit(check, err)
2024-12-11 00:03:08 -06:00
}
2024-12-18 01:29:07 -06:00
okExit(check, "go.mod and go.sum restored from ~/go/pkg/mod/")
}
2024-12-10 14:07:14 -06:00
2025-01-19 04:31:49 -06:00
if argv.Strict {
if err := doStrict(check); err != nil {
badExit(check, err)
}
okExit(check, "go.mod seems clean")
}
2024-12-18 01:29:07 -06:00
if err := doMain(check); err != nil {
badExit(check, err)
}
if argv.Force {
if err := doForce(check); err != nil {
2024-12-15 22:39:54 -06:00
badExit(check, err)
}
2024-12-18 01:29:07 -06:00
} else {
if err := doSmart(check); err != nil {
badExit(check, err)
2024-12-13 02:21:25 -06:00
}
2024-12-11 13:51:06 -06:00
}
2024-12-13 16:16:18 -06:00
if configSave {
forge.ConfigSave()
2024-12-13 02:21:25 -06:00
}
2024-12-17 07:03:07 -06:00
log.Info("forge.FinalGoDepsCheck() worked :", check.GetGoPath())
2024-12-13 16:16:18 -06:00
okExit(check, "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)
2024-12-18 03:34:58 -06:00
check = forge.FindByGoPath(gopath)
2024-12-11 00:03:08 -06:00
if check != nil {
2024-12-17 07:03:07 -06:00
log.Info("findRepo() worked", check.GetGoPath())
2024-12-11 00:03:08 -06:00
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 02:21:25 -06:00
func saveAsMetadata(repo *gitpb.Repo) error {
2024-12-13 16:16:18 -06:00
cname := repo.GetCurrentBranchName()
2024-12-17 20:47:09 -06:00
if repo.GetGoPrimitive() {
2024-12-13 16:16:18 -06:00
if err := repo.AutogenSave([]string{"go.mod"}, cname, true); err != nil {
2024-12-13 02:21:25 -06:00
return err
}
} else {
2024-12-13 16:16:18 -06:00
if err := repo.AutogenSave([]string{"go.mod", "go.sum"}, cname, true); err != nil {
2024-12-13 02:21:25 -06:00
return err
}
}
return nil
}
2024-12-13 16:16:18 -06:00
2024-12-15 22:39:54 -06:00
func doMain(repo *gitpb.Repo) error {
if argv.Force {
err := doForce(repo)
return err
}
// if --force or --strict is not set, fall back to a "smart" guess
// at what the user probably would want
return doSmart(repo)
}