Compare commits

...

11 Commits

8 changed files with 133 additions and 117 deletions

View File

@ -37,3 +37,6 @@ clean:
all: install all: install
go-mod-clean --all --auto go-mod-clean --all --auto
autocomplete:
go-mod-clean --bash > ~/.local/share/bash-completion/completions/go-mod-clean

37
argv.go
View File

@ -1,5 +1,10 @@
package main package main
import (
"fmt"
"os"
)
/* /*
this parses the command line arguements this parses the command line arguements
*/ */
@ -7,13 +12,18 @@ package main
var argv args var argv args
type args struct { type args struct {
Trim bool `arg:"--trim" default:"true" help:"trim entries from go.sum"` Trim bool `arg:"--trim" default:"true" help:"trim entries from go.sum"`
Verbose bool `arg:"--verbose" help:"show more"` Verbose bool `arg:"--verbose" help:"show more"`
Restore bool `arg:"--restore" help:"only restore from go/pkg/mod/"` Restore bool `arg:"--restore" help:"only restore from go/pkg/mod/"`
Force bool `arg:"--force" help:"remove things and redo them no matter what"` Force bool `arg:"--force" help:"remove things and redo them no matter what"`
Strict bool `arg:"--strict" help:"never make go.* files unless everything is perfect"` Purge *EmptyCmd `arg:"subcommand:purge" help:"purge all git notes. (this is where the autogenerated files are stored)"`
Purge bool `arg:"--purge" help:"purge all the git notes. this might be bad for you."` Smart *EmptyCmd `arg:"subcommand:lax" help:"try something but never do os.Exit(-1)"`
Smart bool `arg:"--smart" help:"whatever seems best at the time. never os.Exit(-1)"` Strict *EmptyCmd `arg:"subcommand:strict" help:"never make go.* files unless everything is perfect"`
Bash bool `arg:"--bash" help:"generate bash completion"`
BashAuto []string `arg:"--auto-complete" help:"todo: move this to go-arg"`
}
type EmptyCmd struct {
} }
func (args) Version() string { func (args) Version() string {
@ -30,3 +40,16 @@ go-mod-clean will try to verify your go.* files are using the newest package ver
* Try to trim go.sum of non-existent entries * Try to trim go.sum of non-existent entries
` `
} }
func (a args) DoAutoComplete(argv []string) {
switch argv[0] {
case "checkout":
fmt.Println("user devel master ")
default:
if argv[0] == ARGNAME {
// list the subcommands here
fmt.Println("strict restore purge lax")
}
}
os.Exit(0)
}

View File

@ -8,19 +8,22 @@ import (
"sort" "sort"
"strings" "strings"
"go.wit.com/lib/gui/shell"
"go.wit.com/lib/protobuf/forgepb" "go.wit.com/lib/protobuf/forgepb"
"go.wit.com/lib/protobuf/gitpb" "go.wit.com/lib/protobuf/gitpb"
"go.wit.com/log" "go.wit.com/log"
) )
// This will recreate your go.sum and go.mod files // This will recreate your go.sum and go.mod files
// checks to see if every 'master' git branch version // checks to see if every 'master' git branch version
// matches the go.sum file // matches the go.sum file
// THIS IS IN lib/forgepb AND THIS SHOULD BE SWITCHED TO THERE
// but I can't do it because forge as an app and forge packages aren't yet strong enough to use here (2025 may)
func cleanGoDepsCheckOk(check *gitpb.Repo) error { func cleanGoDepsCheckOk(check *gitpb.Repo) error {
var err error = nil // var err error = nil
var fixes [][]string // var fixes [][]string
log.Printf("current repo %s go dependancy count: %d", check.GetGoPath(), check.GoDepsLen()) log.Printf("%s repo go dependancy count: %d\n", check.GetGoPath(), check.GoDepsLen())
if check.GoDeps == nil { if check.GoDeps == nil {
return errors.New("check.GoDeps == nil") return errors.New("check.GoDeps == nil")
} }
@ -34,8 +37,7 @@ func cleanGoDepsCheckOk(check *gitpb.Repo) error {
continue continue
} }
log.Info("not found:", depRepo.GetGoPath()) log.Info("not found:", depRepo.GetGoPath())
err = errors.New("not found: " + depRepo.GetGoPath()) return errors.New("not found: " + depRepo.GetGoPath())
continue
} }
// log.Info("found dep", depRepo.GetGoPath()) // log.Info("found dep", depRepo.GetGoPath())
if depRepo.GetVersion() != found.GetMasterVersion() { if depRepo.GetVersion() != found.GetMasterVersion() {
@ -48,38 +50,43 @@ func cleanGoDepsCheckOk(check *gitpb.Repo) error {
if forge.Config.IsReadOnly(check.GetGoPath()) { if forge.Config.IsReadOnly(check.GetGoPath()) {
ends += "(ignoring read-only) " ends += "(ignoring read-only) "
if argv.Verbose { if argv.Verbose {
log.Printf("%-48s ok error .%s. vs .%s. %s", depRepo.GetGoPath(), log.Printf("%-48s ok error .%s. vs .%s. %s\n", depRepo.GetGoPath(),
depRepo.GetVersion(), found.GetMasterVersion(), ends) depRepo.GetVersion(), found.GetMasterVersion(), ends)
} }
} else { } else {
if forge.CheckOverride(depRepo.GetGoPath()) { if forge.CheckOverride(depRepo.GetGoPath()) {
ends += "(override) " ends += "(override) "
if argv.Verbose { if argv.Verbose {
log.Printf("%-48s ok error .%s. vs .%s. %s", depRepo.GetGoPath(), log.Printf("%-48s ok error .%s. vs .%s. %s\n", depRepo.GetGoPath(),
depRepo.GetVersion(), found.GetMasterVersion(), ends) depRepo.GetVersion(), found.GetMasterVersion(), ends)
// skip this gopath because it's probably broken forever // skip this gopath because it's probably broken forever
} }
continue continue
} else { } else {
log.Printf("%-48s error %10s vs %10s %s", depRepo.GetGoPath(), log.Printf("%-48s error %10s vs %10s %s\n", depRepo.GetGoPath(),
depRepo.GetVersion(), found.GetMasterVersion(), ends) depRepo.GetVersion(), found.GetMasterVersion(), ends)
errs := fmt.Sprintf("%s error %s vs %s %s", depRepo.GetGoPath(), errs := fmt.Sprintf("%s error %s vs %s %s", depRepo.GetGoPath(),
depRepo.GetVersion(), found.GetMasterVersion(), ends) depRepo.GetVersion(), found.GetMasterVersion(), ends)
if ok, _ := forgepb.ValidGoVersion(found.GetMasterVersion()); ok { if ok, _ := forgepb.ValidGoVersion(found.GetMasterVersion()); ok {
// can't go get invalid version numbers // can't go get invalid version numbers
cmd := []string{"go", "get", depRepo.GetGoPath() + "@" + found.GetMasterVersion()} cmd := []string{"go", "get", depRepo.GetGoPath() + "@" + found.GetMasterVersion()}
fixes = append(fixes, cmd) // fixes = append(fixes, cmd)
if _, err := shell.RunVerbose(cmd); err != nil {
return err
}
} else {
return errors.New(errs)
} }
err = errors.New(errs)
} }
} }
} }
} }
for i, cmd := range fixes { /*
log.Info("try cmd", i, cmd) for i, cmd := range fixes {
check.RunRealtime(cmd) log.Info("try cmd", i, cmd)
} }
return err */
return nil
} }
func trimGoSum(check *gitpb.Repo) error { func trimGoSum(check *gitpb.Repo) error {
@ -103,6 +110,10 @@ func trimGoSum(check *gitpb.Repo) error {
} }
for _, line := range strings.Split(string(data), "\n") { for _, line := range strings.Split(string(data), "\n") {
line = strings.TrimSpace(line)
if line == "" {
continue
}
parts := strings.Fields(line) parts := strings.Fields(line)
if len(parts) < 3 { if len(parts) < 3 {
log.Info("WIERD OR BAD:", line) log.Info("WIERD OR BAD:", line)

View File

@ -8,9 +8,6 @@ import (
// this will make go.mod and go.sum files, but you have to // this will make go.mod and go.sum files, but you have to
// have the files in .gitignore for now // have the files in .gitignore for now
func doSmart(repo *gitpb.Repo) error { func doSmart(repo *gitpb.Repo) error {
// erase the go.mod and go.sum files
eraseGoMod(repo)
// if the repo has go.mod in the repo... // if the repo has go.mod in the repo...
if err := repo.RepoIgnoresGoMod(); err != nil { if err := repo.RepoIgnoresGoMod(); err != nil {
if repo.ParseGoSum() { if repo.ParseGoSum() {
@ -19,51 +16,42 @@ func doSmart(repo *gitpb.Repo) error {
log.Info("go-mod-clean can not run on this repo", repo.GetGoPath()) log.Info("go-mod-clean can not run on this repo", repo.GetGoPath())
log.Info("go.mod and go.sum must be git metadata to continue") log.Info("go.mod and go.sum must be git metadata to continue")
// return nil // return nil
if repo.Exists("go.sum") {
return nil
}
} }
if repo.Exists("go.mod") { // erase the go.mod and go.sum files
return nil eraseGoMod(repo)
}
// try to restore from the git metadata // try to restore from the git metadata
cname := repo.GetCurrentBranchName() cname := repo.GetCurrentBranchName()
if err := repo.AutogenRestore(cname); err == nil { if err := repo.AutogenRestore(cname); err == nil {
log.Info(repo.GetGoPath(), "files were restored ok from git metadata (notes)") log.Info(repo.GetGoPath(), "files were restored ok from git metadata (notes)")
} if repo.Exists("go.mod") {
if repo.Exists("go.mod") { return nil
return nil }
} // autogen restore didn't find go.mod
// attempt to restore from ~/go/pkg/mod/
if err := restoreFromGoPkg(repo); err == nil {
log.Info(repo.GetGoPath(), "files were restored ok ~/go/mod/")
}
if repo.Exists("go.mod") {
return nil
} }
// actually will re-create go.sum and go.mod now // actually will re-create go.sum and go.mod now
if err := redoGoMod(repo); err != nil { if err := redoGoMod(repo); err != nil {
log.Info(repo.GetGoPath(), "the files were restored with redoGoMod()") return err
}
if repo.Exists("go.mod") {
return nil
} }
log.Info(repo.GetGoPath(), "the files should have been restored with redoGoMod()")
/*
if repo.Exists("go.mod") {
return nil
}
*/
// the first time, it'll attempt to fix some stuff // the first time, it'll attempt to fix some stuff
cleanGoDepsCheckOk(repo) if err := cleanGoDepsCheckOk(repo); err != nil {
// try to trim junk return err
}
// trim junk that might have been added by cleanGoDepsCheckOk()
if err := trimGoSum(repo); err != nil { if err := trimGoSum(repo); err != nil {
return err
} }
if repo.Exists("go.mod") {
return nil
}
repo.ParseGoSum()
if repo.Exists("go.mod") {
return nil
}
// last chance. just run go mod init
repo.RunVerbose([]string{"go", "mod", "init"})
return nil return nil
} }

View File

@ -38,8 +38,8 @@ func doStrict(repo *gitpb.Repo) error {
return errors.New(repo.GetGoPath() + " not in the git master branch") return errors.New(repo.GetGoPath() + " not in the git master branch")
} }
err = repo.RepoIgnoresGoMod() // not sure if this really needs to be run a second time. probably not, but whatever. who cares
if err != nil { if err := repo.RepoIgnoresGoMod(); err != nil {
log.Info(repo.GetGoPath(), "some wierd git error happened. investigate.", err) log.Info(repo.GetGoPath(), "some wierd git error happened. investigate.", err)
return err return err
} }
@ -78,7 +78,7 @@ func doStrict(repo *gitpb.Repo) error {
} }
os.Unsetenv("GO111MODULE") os.Unsetenv("GO111MODULE")
if result, err := repo.RunQuiet([]string{"go", "mod", "tidy", "-go=" + golangVersion}); err != nil { if result, err := repo.RunQuiet([]string{"go", "mod", "tidy"}); err != nil {
// I guess the thing to do, if go mod tidy fails, is to just leave the repo alone // I guess the thing to do, if go mod tidy fails, is to just leave the repo alone
// it's either primitive or could be a go support project but not in go // it's either primitive or could be a go support project but not in go
for _, line := range result.Stdout { for _, line := range result.Stdout {
@ -91,14 +91,16 @@ func doStrict(repo *gitpb.Repo) error {
} }
// the first time, it'll attempt to fix some stuff // the first time, it'll attempt to fix some stuff
log.Info("Running: updateToNewestReleases()") log.Info(repo.GetGoPath(), "Running: updateToNewestReleases()")
cleanGoDepsCheckOk(repo) cleanGoDepsCheckOk(repo)
// try to trim junk if repo.Exists("go.sum") {
log.Info("Running: trimGoSum()") // try to trim junk
if err := trimGoSum(repo); err != nil { log.Info(repo.GetGoPath(), "Running: trimGoSum()")
log.Info(repo.GetGoPath(), "trimGoSum() failed", err) if err := trimGoSum(repo); err != nil {
return err log.Info(repo.GetGoPath(), "trimGoSum() failed", err)
return err
}
} }
if repo.ParseGoSum() { if repo.ParseGoSum() {
@ -107,17 +109,28 @@ func doStrict(repo *gitpb.Repo) error {
log.Info(repo.GetGoPath(), "ParseGoSum() failed") log.Info(repo.GetGoPath(), "ParseGoSum() failed")
} }
// check go.sum file if !repo.GetGoPrimitive() {
if err := cleanGoDepsCheckOk(repo); err != nil { // check go.sum file
log.Info("forge.FinalGoDepsCheck() failed. boo. :", repo.GetGoPath()) if err := cleanGoDepsCheckOk(repo); err != nil {
return err log.Info(repo.GetGoPath(), "forge.FinalGoDepsCheck() failed. boo.")
return err
}
}
if repo.Exists("go.sum") {
// try to trim junk
log.Info(repo.GetGoPath(), "Running: trimGoSum()")
if err := trimGoSum(repo); err != nil {
log.Info(repo.GetGoPath(), "trimGoSum() failed", err)
return err
}
} }
// put the files in the notes section in git // put the files in the notes section in git
// this way, git commits are not messed up // this way, git commits are not messed up
// with this autogenerated code // with this autogenerated code
if err := saveAsMetadata(repo); err != nil { if err := saveAsMetadata(repo); err != nil {
log.Info("save go.mod as git metadata failed", repo.GetGoPath(), err) log.Info(repo.GetGoPath(), "save go.mod as git metadata failed", err)
return err return err
} }

View File

@ -8,7 +8,7 @@ import (
) )
func okExit(check *gitpb.Repo, msg string) { func okExit(check *gitpb.Repo, msg string) {
log.Info("exit() go-mod-clean on", check.GetGoPath(), "ok") log.Info("exit() go-mod-clean on", check.Namespace, "ok")
log.DaemonMode(true) log.DaemonMode(true)
log.Info(msg) log.Info(msg)
os.Exit(0) os.Exit(0)
@ -16,9 +16,9 @@ func okExit(check *gitpb.Repo, msg string) {
func badExit(check *gitpb.Repo, err error) { func badExit(check *gitpb.Repo, err error) {
log.DaemonMode(true) log.DaemonMode(true)
log.Info("go-mod-clean failed: ", err, forge.GetGoSrc()) log.Info("go-mod-clean failed: ", err, forge.Config.ReposDir)
if check != nil { if check != nil {
if argv.Strict { if argv.Strict != nil {
// if in strict mode, remove the go.mod and go.sum // if in strict mode, remove the go.mod and go.sum
eraseGoMod(check) eraseGoMod(check)
} }

31
main.go
View File

@ -5,6 +5,7 @@ import (
"strings" "strings"
"go.wit.com/dev/alexflint/arg" "go.wit.com/dev/alexflint/arg"
"go.wit.com/lib/gui/prep"
"go.wit.com/lib/protobuf/forgepb" "go.wit.com/lib/protobuf/forgepb"
"go.wit.com/lib/protobuf/gitpb" "go.wit.com/lib/protobuf/gitpb"
"go.wit.com/log" "go.wit.com/log"
@ -14,7 +15,8 @@ import (
var VERSION string var VERSION string
var BUILDTIME string var BUILDTIME string
var golangVersion string = "1.21" // used for shell auto completion
var ARGNAME string = "go-mod-clean"
var pp *arg.Parser var pp *arg.Parser
var forge *forgepb.Forge var forge *forgepb.Forge
@ -23,24 +25,25 @@ var forge *forgepb.Forge
var configSave bool var configSave bool
func main() { func main() {
var check *gitpb.Repo
log.Info("go-mod-clean version", VERSION, "built on", BUILDTIME) log.Info("go-mod-clean version", VERSION, "built on", BUILDTIME)
// command line parsing & handling
prep.Bash(ARGNAME, argv.DoAutoComplete) // todo: make this: prep.Bash(argv)
pp = arg.MustParse(&argv) pp = arg.MustParse(&argv)
// load the ~/.config/forge/ config forge = forgepb.Init()
// this lets you configure repos you have read/write access too
forge = forgepb.InitPB() // todo: make this scan only if pb file not found
// figure out what directory we are running in // figure out what directory we are running in
check = findPwdRepo() check := findPwdRepo()
if check == nil { if check == nil {
log.Info("this directory isn't in a golang project (not in ~/go/src nor a go.work file)") log.Info("this directory isn't in a golang project (not in ~/go/src nor a go.work file)")
badExit(nil, nil) badExit(nil, nil)
} }
// deletes all the git notes // deletes all the git notes
if argv.Purge { if argv.Purge != nil {
purgeNotes(check) purgeNotes(check) // purges all the 'git notes'
eraseGoMod(check) // erase the go.mod and go.sum files
okExit(check, "notes gone") okExit(check, "notes gone")
} }
@ -52,14 +55,14 @@ func main() {
okExit(check, "go.mod and go.sum restored from ~/go/pkg/mod/") okExit(check, "go.mod and go.sum restored from ~/go/pkg/mod/")
} }
if argv.Strict { if argv.Strict != nil {
log.Info("Starting --strict mode") log.Info("Starting --strict mode")
if err := doStrict(check); err != nil { if err := doStrict(check); err != nil {
badExit(check, err) badExit(check, err)
} }
okExit(check, "go.mod seems clean") okExit(check, "go.mod seems clean")
} }
if argv.Smart { if argv.Smart != nil {
// best effort // best effort
if err := doSmart(check); err != nil { if err := doSmart(check); err != nil {
// badExit(check, err) // badExit(check, err)
@ -81,16 +84,16 @@ func findPwdRepo() *gitpb.Repo {
// attempt to use the working directory // attempt to use the working directory
// this is probably what happens most of the time // this is probably what happens most of the time
pwd, _ := os.Getwd() pwd, _ := os.Getwd()
if strings.HasPrefix(pwd, forge.GetGoSrc()) { if strings.HasPrefix(pwd, forge.Config.ReposDir) {
gopath := strings.TrimPrefix(pwd, forge.GetGoSrc()) gopath := strings.TrimPrefix(pwd, forge.Config.ReposDir)
gopath = strings.Trim(gopath, "/") gopath = strings.Trim(gopath, "/")
log.Info("findRepo() trying gopath", gopath)
check = forge.FindByGoPath(gopath) check = forge.FindByGoPath(gopath)
if check != nil { if check != nil {
log.Info("findRepo() worked", check.GetGoPath()) log.Info(check.Namespace, "was found ok in forge")
return check return check
} }
} }
log.Info("findRepo() forge could not find GO path:", pwd)
return nil return nil
} }

View File

@ -5,7 +5,6 @@ package main
import ( import (
"fmt" "fmt"
"os" "os"
"strings"
"github.com/go-cmd/cmd" "github.com/go-cmd/cmd"
"go.wit.com/lib/protobuf/gitpb" "go.wit.com/lib/protobuf/gitpb"
@ -31,7 +30,7 @@ func setGoVersion(repo *gitpb.Repo, version string) error {
} }
func goTidy(fullpath string) (cmd.Status, error) { func goTidy(fullpath string) (cmd.Status, error) {
if result, err := runVerbose(fullpath, []string{"go", "mod", "tidy", "-go=" + golangVersion}); err == nil { if result, err := runVerbose(fullpath, []string{"go", "mod", "tidy"}); err == nil {
return result, nil return result, nil
} else { } else {
return result, err return result, err
@ -46,22 +45,14 @@ func redoGoMod(repo *gitpb.Repo) error {
log.Warn("rm go.mod go.sum failed", err) log.Warn("rm go.mod go.sum failed", err)
return err return err
} }
if _, err := repo.RunQuiet([]string{"go", "mod", "init", repo.GetGoPath()}); err != nil { if _, err := repo.RunQuiet([]string{"go", "mod", "init", repo.GetGoPath()}); err != nil {
log.Warn("go mod init failed", err) log.Warn("go mod init failed", err)
return err return err
} }
if result, err := goTidy(repo.FullPath); err != nil { if _, err := repo.RunQuiet([]string{"go", "mod", "tidy"}); err != nil {
if tinyFixer(result) { log.Warn("go mod init failed", err)
if _, err := goTidy(repo.FullPath); err != nil {
return err
}
}
}
// most things should build with golang after 1.21 // todo: allow this to be set somewhere
if err := setGoVersion(repo, golangVersion); err != nil {
log.Warn(repo.GetGoPath(), "go mod edit failed", err)
return err return err
} }
@ -72,19 +63,3 @@ func redoGoMod(repo *gitpb.Repo) error {
return fmt.Errorf("check.ParseGoSum() failed") return fmt.Errorf("check.ParseGoSum() failed")
} }
func tinyFixer(result cmd.Status) bool {
for _, line := range result.Stdout {
if strings.Contains(line, "requires go@") {
log.Info("tinyFixer:", line)
parts := strings.Split(line, "requires go@")
if len(parts) == 2 {
parts = strings.Split(parts[1], ",")
golangVersion = parts[0]
return true
}
log.Info("tinyFixer:", line, "golangVersion", golangVersion)
}
}
return false
}