go-mod-clean/main.go

216 lines
5.3 KiB
Go
Raw Normal View History

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
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-13 16:16:18 -06:00
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)
}
}
2024-12-13 16:16:18 -06:00
} else {
// figure out what directory we are running in
2024-12-13 18:59:40 -06:00
check = findPwdRepo()
2024-12-13 16:16:18 -06:00
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)
2024-12-11 00:03:08 -06:00
}
2024-12-10 14:07:14 -06:00
2024-12-13 16:16:18 -06:00
if err := doMain(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-11 13:51:06 -06:00
log.Info("forge.FinalGoDepsCheck() worked :", check.GoPath)
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)
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 02:21:25 -06:00
func saveAsMetadata(repo *gitpb.Repo) error {
2024-12-13 16:16:18 -06:00
cname := repo.GetCurrentBranchName()
if repo.GoPrimitive {
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
func doMain(repo *gitpb.Repo) error {
var perfect bool = true
2024-12-13 16:16:18 -06:00
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")
}
2024-12-14 20:35:00 -06:00
log.Info(repo.GoPath, "is valid according to forge")
2024-12-13 16:16:18 -06:00
// skip restore if --force
2024-12-14 20:35:00 -06:00
if argv.Force {
repo.Run([]string{"git", "notes", "remove"})
2024-12-14 23:22:12 -06:00
eraseGoMod(repo)
2024-12-14 20:35:00 -06:00
}
// erase the go.mod and go.sum files
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
2024-12-13 16:16:18 -06:00
}
2024-12-13 18:59:40 -06:00
// double check here. use --force to remake them
if err := repo.ValidGoSum(); err == nil {
log.Info(repo.GoPath, "go.mod and go.sum are already valid")
return nil
}
2024-12-13 16:16:18 -06:00
if repo.GetMasterBranchName() != repo.GetCurrentBranchName() {
perfect = false
if argv.Strict {
log.Info("")
log.Info("You are not operating on your git master branch.")
log.Info("Publishing go.mod & go.sum files must come from from git version tag on the master branch")
log.Info("")
return errors.New(repo.GoPath + " not in the git master branch")
}
2024-12-13 16:16:18 -06:00
}
2024-12-13 18:59:40 -06:00
ok, err := repoIgnoresGoMod(repo)
if err != nil {
2024-12-13 18:59:40 -06:00
log.Info(repo.GoPath, "some wierd git error happened. investigate.", err)
return err
}
// if ok, then git owns 'go.mod' and we can't really do anything
// todo: ignore this with --force
if ok {
2024-12-13 18:59:40 -06:00
log.Info(repo.GoPath, "git says it does not own go.mod")
// continue and attempt to create go.mod and go.sum
} else {
if forge.Config.IsReadOnly(repo.GoPath) {
log.Info("you can not push to read only repositories.", repo.GoPath)
log.Info("change your .config/forge/ to indicate you own this repository")
return nil
}
perfect = false
2024-12-13 18:59:40 -06:00
// continue and attempt to create go.mod and go.sum
}
if repo.CheckDirty() {
perfect = false
if argv.Strict {
log.Info("")
log.Info("You can not continue on a dirty git repo.")
log.Info("")
return errors.New(repo.GoPath + " git repo is dirty")
}
}
2024-12-13 18:59:40 -06:00
log.Info(repo.GoPath, "GOING TO MAKE NEW go.* FILES")
// actually will re-create go.sum and go.mod now
2024-12-13 16:16:18 -06:00
if err := redoGoMod(repo); err != nil {
return err
}
if argv.Trim {
2024-12-13 20:31:55 -06:00
// the first time, it'll attempt to fix some stuff
cleanGoDepsCheckOk(repo)
2024-12-13 16:16:18 -06:00
// try to trim junk
if err := trimGoSum(repo); err != nil {
return err
}
2024-12-13 20:31:55 -06:00
repo.ParseGoSum()
2024-12-13 16:16:18 -06:00
}
2024-12-13 20:31:55 -06:00
/*
data, _ := repo.ReadFile("go.mod")
log.Info(string(data))
data, _ = repo.ReadFile("go.sum")
log.Info(string(data))
*/
2024-12-13 16:16:18 -06:00
// check go.sum file
if err := cleanGoDepsCheckOk(repo); err != nil {
log.Info("forge.FinalGoDepsCheck() failed. boo. :", repo.GoPath)
return err
}
// if everything is perfect, save them as git metadata
if perfect {
// 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
}
2024-12-13 16:16:18 -06:00
}
// everything worked!
configSave = true
return nil
}