diff options
| author | Jeff Carr <[email protected]> | 2025-10-19 05:08:00 -0500 |
|---|---|---|
| committer | Jeff Carr <[email protected]> | 2025-10-19 05:08:00 -0500 |
| commit | 2bbc498b02cbbe3ba68de391bffbe79ab0f226fc (patch) | |
| tree | 3a09151aeb7a1294e83d61e847e74f9a982b837b | |
| parent | c78eff62bb86594f71f9ff43ff151820ae8e94d9 (diff) | |
new argvv0.0.125
| -rw-r--r-- | argv.custom.go | 40 | ||||
| -rw-r--r-- | argv.struct.go (renamed from argv.go) | 48 | ||||
| -rw-r--r-- | argv.template.go | 80 | ||||
| -rw-r--r-- | cleanGoSum.go | 12 | ||||
| -rw-r--r-- | doStrict.go | 2 | ||||
| -rw-r--r-- | exit.go | 9 | ||||
| -rw-r--r-- | main.go | 28 | ||||
| -rw-r--r-- | structs.go | 21 |
8 files changed, 161 insertions, 79 deletions
diff --git a/argv.custom.go b/argv.custom.go new file mode 100644 index 0000000..3504984 --- /dev/null +++ b/argv.custom.go @@ -0,0 +1,40 @@ +package main + +import ( + "os" + + "go.wit.com/lib/protobuf/argvpb" +) + +// sent via -ldflags +var VERSION string +var BUILDTIME string + +// used for shell auto completion +var APPNAME string = "go-mod-clean" + +func (a args) Description() string { + return ` +go-mod-clean will try to verify your go.* files are using the newest package versions + +* Recreate go.* with 'go mod init' and 'go mod tidy' +* Set your required go in go.mod (default is go1.21 +* Check that the most recent master branch versions are used +* Try to trim go.sum of non-existent entries +` +} + +/* + handles shell autocomplete +*/ + +// sends the strings to bash or zsh that will be your options +func (a args) SendCompletionStrings(pb *argvpb.Argv) { + if pb.Cmd == "" { + base := []string{"strict", "--restore", "purge", "lax", "--version"} + pb.SendStrings(base) + } else { + pb.SubCommand(pb.Goargs...) + } + os.Exit(0) +} @@ -1,15 +1,5 @@ package main -import ( - "os" - - "go.wit.com/lib/protobuf/argvpb" -) - -/* - this parses the command line arguements -*/ - var argv args type args struct { @@ -26,41 +16,3 @@ type args struct { type EmptyCmd struct { } - -func (a args) Description() string { - return ` -go-mod-clean will try to verify your go.* files are using the newest package versions - -* Recreate go.* with 'go mod init' and 'go mod tidy' -* Set your required go in go.mod (default is go1.21 -* Check that the most recent master branch versions are used -* Try to trim go.sum of non-existent entries -` -} - -/* - handles shell autocomplete -*/ - -func (args) Appname() string { - return ARGNAME -} - -func (args) Buildtime() (string, string) { - return BUILDTIME, VERSION -} - -func (args) Version() string { - return argvpb.StandardVersion(ARGNAME, VERSION, BUILDTIME) -} - -// sends the strings to bash or zsh that will be your options -func (a args) SendCompletionStrings(pb *argvpb.Argv) { - if pb.Cmd == "" { - base := []string{"strict", "--restore", "purge", "lax", "--version"} - pb.SendStrings(base) - } else { - pb.SubCommand(pb.Goargs...) - } - os.Exit(0) -} diff --git a/argv.template.go b/argv.template.go new file mode 100644 index 0000000..e7a9948 --- /dev/null +++ b/argv.template.go @@ -0,0 +1,80 @@ +package main + +// these are stubbed in functions needed +// just copy this file from another working app for now +// you shouldn't need to change anything here +// TODO: clean this up in argv + +import ( + "os" + + "go.wit.com/dev/alexflint/arg" + "go.wit.com/gui" + "go.wit.com/lib/fhelp" + "go.wit.com/log" +) + +func (args) InitArgv() (string, string, string) { + return APPNAME, BUILDTIME, VERSION +} + +// this function will send the current argv PB to go-args for parsing +func (args) ParseFlags(flags []string) error { + var err error + if me.pp == nil { + // log.Info("Parse Flags GOT flags:", flags) + me.pp, err = arg.ParseFlags(flags, &argv) + // panic("got to the app's ParseFlags()") + } else { + panic("me.pp was not nil") + } + return err +} + +// add this funcgion: this will print the help +func (args) WriteHelpForSubcommand(cmd string) error { + me.pp.WriteHelpForSubcommand(os.Stderr, cmd) + return nil +} + +// this will print the help for the subcmd +func (args) WriteHelpForAutocomplete(part string, subcmd ...string) error { + return me.pp.WriteHelpForAutocomplete(os.Stderr, os.Stdout, part, subcmd...) +} + +func (args) WriteHelpForAutocompleteDebug(part string, subcmd ...string) error { + f, _ := os.OpenFile("/tmp/argv.junk", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) + return me.pp.WriteHelpForAutocomplete(f, os.Stdout, part, subcmd...) +} + +// add this funcgion: this will print the help +func (args) WriteHelp() error { + me.pp.WriteHelp(os.Stderr) + return nil +} + +func (args) InitGui() error { + // panic("got here") + arg.Register(&gui.ArgvGui) + // me.gui = gui.PreInit() + me.myGui = fhelp.Gui() + return nil +} + +func (args) Exit() { + gui.UnloadToolkits() + if me.argv.Verbose() { + log.Info("argv.Exit() called", APPNAME+".Exit()") + } + // remove this from the template for your app (or make one for youself if you need it) + // forgeExit() // custom forge shutdown function +} + +func (args) Help() string { + return "got app help" +} + +func (args) MustParse() error { + me.pp = arg.MustParse(&argv) + return nil +} diff --git a/cleanGoSum.go b/cleanGoSum.go index b032d83..dbef94a 100644 --- a/cleanGoSum.go +++ b/cleanGoSum.go @@ -28,9 +28,9 @@ func cleanGoDepsCheckOk(check *gitpb.Repo) error { return errors.New("check.GoDeps == nil") } for depRepo := range check.GoDeps.IterAll() { - found := forge.FindByNamespace(depRepo.GetGoPath()) + found := me.forge.FindByNamespace(depRepo.GetGoPath()) if found == nil { - if forge.CheckOverride(depRepo.GetGoPath()) { + if me.forge.CheckOverride(depRepo.GetGoPath()) { // skip this gopath because it's probably broken forever continue } @@ -39,20 +39,20 @@ func cleanGoDepsCheckOk(check *gitpb.Repo) error { } // log.Info("found dep", depRepo.GetGoPath()) if depRepo.GetVersion() != found.GetMasterVersion() { - check := forge.FindByNamespace(depRepo.GetGoPath()) + check := me.forge.FindByNamespace(depRepo.GetGoPath()) var ends string if check.CheckDirty() { ends = "(dirty) " } - if forge.Config.IsReadOnly(check.GetGoPath()) { + if me.forge.Config.IsReadOnly(check.GetGoPath()) { ends += "(ignoring read-only) " if argv.Verbose { log.Printf("%-48s ok error .%s. vs .%s. %s\n", depRepo.GetGoPath(), depRepo.GetVersion(), found.GetMasterVersion(), ends) } } else { - if forge.CheckOverride(depRepo.GetGoPath()) { + if me.forge.CheckOverride(depRepo.GetGoPath()) { ends += "(override) " if argv.Verbose { log.Printf("%-48s ok error .%s. vs .%s. %s\n", depRepo.GetGoPath(), @@ -159,7 +159,7 @@ func trimGoSum(check *gitpb.Repo) error { if good[gopath] { fmt.Fprintf(newf, "%s %s\n", gopath, stuff[gopath]) fmt.Fprintf(newf, "%s %s\n", gopath, modver[gopath]) - check := forge.FindByNamespace(gopath) + check := me.forge.FindByNamespace(gopath) if check == nil { log.Info("gopath does not really exist:", gopath) } diff --git a/doStrict.go b/doStrict.go index 8e49b8f..c36278a 100644 --- a/doStrict.go +++ b/doStrict.go @@ -44,7 +44,7 @@ func doStrict(repo *gitpb.Repo) error { return err } - if forge.Config.IsReadOnly(repo.GetGoPath()) { + if me.forge.Config.IsReadOnly(repo.GetGoPath()) { log.Info("you can not push to read only repositories.", repo.GetGoPath()) log.Info("change your .config/forge/ to indicate you own this repository") return nil @@ -1,7 +1,7 @@ package main import ( - "os" + "fmt" "go.wit.com/lib/protobuf/gitpb" "go.wit.com/log" @@ -10,18 +10,17 @@ import ( func okExit(check *gitpb.Repo, msg string) { log.Info("exit() go-mod-clean on", check.Namespace, "ok") log.DaemonMode(true) - log.Info(msg) - os.Exit(0) + me.argv.GoodExit(msg) } func badExit(check *gitpb.Repo, err error) { log.DaemonMode(true) - log.Info("go-mod-clean failed: ", err, forge.Config.ReposDir) if check != nil { if argv.Strict != nil { // if in strict mode, remove the go.mod and go.sum eraseGoMod(check) } } - os.Exit(-1) + s := fmt.Sprintf("failed: %s", me.forge.Config.ReposDir) + me.argv.BadExit(s, err) } @@ -10,27 +10,17 @@ import ( "go.wit.com/log" ) -// sent via -ldflags -var VERSION string -var BUILDTIME string - -// used for shell auto completion -var ARGNAME string = "go-mod-clean" - -var newargv *argvpb.Argv // more experiments for bash handling -var forge *forgepb.Forge - // var check *gitpb.Repo var configSave bool func main() { - newargv = argvpb.Autocomplete(&argv) // adds shell auto complete to go-args - - forge, _ = forgepb.Init() + me = new(mainType) + me.argv = argvpb.Autocomplete(&argv) // adds shell auto complete to go-args + me.forge, _ = forgepb.Init() // figure out what directory we are running in pwd, _ := os.Getwd() - check := forge.Repos.FindByFullPath(pwd) + check := me.forge.Repos.FindByFullPath(pwd) if check == nil { log.Info("the forge code isn't working right. couldn't figure out path:", pwd) // just run "go mod init" and "go mod tidy" @@ -44,7 +34,7 @@ func main() { badExit(nil, nil) } log.Info("todo: check return values here in go-mod-clean") - newargv.GoodExit("may have worked somewhat") // exits back to the shell via argv (with timing) + me.argv.GoodExit("may have worked somewhat") // exits back to the shell via argv (with timing) } // deletes all the git notes @@ -91,12 +81,12 @@ func findPwdRepo() *gitpb.Repo { // attempt to use the working directory // this is probably what happens most of the time pwd, _ := os.Getwd() - return forge.Repos.FindByFullPath(pwd) + return me.forge.Repos.FindByFullPath(pwd) /* - if strings.HasPrefix(pwd, forge.Config.ReposDir) { - gopath := strings.TrimPrefix(pwd, forge.Config.ReposDir) + if strings.HasPrefix(pwd, me.forge.Config.ReposDir) { + gopath := strings.TrimPrefix(pwd, me.forge.Config.ReposDir) gopath = strings.Trim(gopath, "/") - check = forge.FindByGoPath(gopath) + check = me.forge.FindByGoPath(gopath) if check != nil { log.Info(check.Namespace, "was found ok in forge") return check diff --git a/structs.go b/structs.go new file mode 100644 index 0000000..c767cbb --- /dev/null +++ b/structs.go @@ -0,0 +1,21 @@ +// Copyright 2017-2025 WIT.COM Inc. All rights reserved. +// Use of this source code is governed by the GPL 3.0 + +package main + +import ( + "go.wit.com/dev/alexflint/arg" + "go.wit.com/lib/fhelp" + "go.wit.com/lib/protobuf/argvpb" + "go.wit.com/lib/protobuf/forgepb" +) + +var me *mainType + +// this app's variables +type mainType struct { + argv *argvpb.Argv // more experiments for bash handling + pp *arg.Parser // for parsing the command line args. Yay to alexf lint! + myGui *fhelp.GuiPrep // for initializing the GUI toolkits + forge *forgepb.Forge +} |
