2024-12-10 14:07:14 -06:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-12-11 00:03:08 -06:00
|
|
|
"fmt"
|
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
|
|
|
// rescan just in case (?) todo: decide what forge should default too
|
|
|
|
forge.ScanGoSrc()
|
2024-12-10 14:07:14 -06:00
|
|
|
|
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-12 18:59:16 -06:00
|
|
|
log.Info("starting go-mod-clean for", check.GoPath)
|
2024-12-11 00:03:08 -06:00
|
|
|
log.Info("go src dir is set to", forge.GetGoSrc())
|
|
|
|
|
|
|
|
if argv.Recursive {
|
|
|
|
if forge.IsGoWork() {
|
|
|
|
var warning []string
|
2024-12-12 18:59:16 -06:00
|
|
|
warning = append(warning, "go-mod-clean --recursive may not work unless you are in ~/go/src")
|
2024-12-11 00:03:08 -06:00
|
|
|
warning = append(warning, "you can continue anyway, but it hasn't been tested as much.")
|
|
|
|
simpleStdin(true, warning)
|
|
|
|
}
|
|
|
|
var warning []string
|
2024-12-12 18:59:16 -06:00
|
|
|
warning = append(warning, "go-mod-clean will recreate go.mod and go.sum")
|
2024-12-11 00:03:08 -06:00
|
|
|
warning = append(warning, "because you have selected --recursive")
|
|
|
|
warning = append(warning, "this will redo _every_ repo. This is probably fine.")
|
|
|
|
warning = append(warning, fmt.Sprintf("You have %d total repositories in %s", forge.Repos.Len(), forge.GetGoSrc()))
|
|
|
|
warning = append(warning, "")
|
2024-12-11 01:14:24 -06:00
|
|
|
warning = append(warning, "However, this will also, at times, do:")
|
2024-12-11 00:03:08 -06:00
|
|
|
warning = append(warning, "")
|
|
|
|
warning = append(warning, "rm -rf ~/go/pkg/")
|
|
|
|
warning = append(warning, "rm -rf ~/.config/go-build/")
|
|
|
|
warning = append(warning, "")
|
|
|
|
warning = append(warning, "Which is also probably fine, but will clear all your build cache and go mod cache")
|
|
|
|
warning = append(warning, "")
|
|
|
|
simpleStdin(false, warning)
|
2024-12-11 01:14:24 -06:00
|
|
|
// purgeGoCaches()
|
2024-12-11 00:03:08 -06:00
|
|
|
} else {
|
2024-12-12 18:59:16 -06:00
|
|
|
simpleStdin(true, []string{"go-mod-clean will recreate go.mod and go.sum"})
|
2024-12-10 14:07:14 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// re-create go.sum and go.mod
|
2024-12-11 00:03:08 -06:00
|
|
|
if _, err := check.RedoGoMod(); err != nil {
|
|
|
|
badExit(err)
|
|
|
|
}
|
|
|
|
|
2024-12-11 13:51:06 -06:00
|
|
|
// try to trim junk
|
|
|
|
if err := forge.TrimGoSum(check); err != nil {
|
|
|
|
badExit(err)
|
|
|
|
}
|
|
|
|
|
2024-12-11 00:03:08 -06:00
|
|
|
// check go.sum file
|
2024-12-11 13:51:06 -06:00
|
|
|
if err := forge.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
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
func okExit(thing string) {
|
|
|
|
log.Info(thing, "ok")
|
2024-12-12 18:59:16 -06:00
|
|
|
log.Info("Finished go-mod-clean on", check.GetGoPath(), "ok")
|
2024-12-10 14:07:14 -06:00
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
func badExit(err error) {
|
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)
|
|
|
|
}
|