From 3a7d5a176350447cf568d9fbc7ea804895549968 Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Sat, 18 Oct 2025 11:53:14 -0500 Subject: the long road to betterness --- argv.custom.go | 92 +++++++++++++++++++++++++ argv.go | 184 -------------------------------------------------- argv.struct.go | 108 +++++++++++++++++++++++++++++ argv.template.go | 74 ++++++++++++++++++++ main.go | 7 -- resources/TESTVERSION | 22 ++++++ structs.go | 2 + 7 files changed, 298 insertions(+), 191 deletions(-) create mode 100644 argv.custom.go delete mode 100644 argv.go create mode 100644 argv.struct.go create mode 100644 argv.template.go create mode 100644 resources/TESTVERSION diff --git a/argv.custom.go b/argv.custom.go new file mode 100644 index 0000000..c8c97be --- /dev/null +++ b/argv.custom.go @@ -0,0 +1,92 @@ +// Copyright 2017-2025 WIT.COM Inc. All rights reserved. +// Use of this source code is governed by the GPL 3.0 + +package main + +/* + this parses the command line arguements + this enables command line options from other packages like 'gui' and 'log' +*/ + +import ( + "os" + + "go.wit.com/lib/debugger" + "go.wit.com/lib/fhelp" + "go.wit.com/lib/gui/logsettings" + "go.wit.com/lib/protobuf/argvpb" + "go.wit.com/log" +) + +// sent via -ldflags +var VERSION string +var BUILDTIME string + +// used for shell auto completion +var APPNAME string = "wit" // todo: get this from $0 ? + +func init() { + if debugger.ArgDebug() { + log.Info("cmd line --debugger == true") + go func() { + log.Sleep(2) + debugger.DebugWindow() + }() + } + + if debugger.ArgLogger() { + log.Info("cmd line --loggger == true") + go func() { + log.Sleep(4) + logsettings.LogWindow() + logsettings.LogWindow() + }() + } +} + +func (args) Buildtime() (string, string) { + return BUILDTIME, VERSION +} + +func (args) Version() string { + return "wit-test " + VERSION + " Built on " + BUILDTIME +} + +/* + handles shell autocomplete +*/ + +func (args) Appname() string { + return APPNAME +} + +func (args) ArgvGui() error { + me.myGui = fhelp.Gui() // adds the GUI package argv support + return nil +} + +func (a args) DoAutoComplete(pb *argvpb.Argv) { + base := []string{"build", "upgrade", "git", "publish", "pb", "linux", "droplet"} + base = append(base, "--version", "--force", "--all") + + // add these only if installed + if _, err := fhelp.CheckCmd("zood"); err == nil { + base = append(base, "zoo") + } + if _, err := fhelp.CheckCmd("forge"); err == nil { + base = append(base, "forge") + } + if _, err := fhelp.CheckCmd("go-clone"); err == nil { + base = append(base, "go-clone") + } + if areSuperuser() { + base = append(base, "upgrade") + base = append(base, "rdate") + } + if pb.Cmd == "" { + pb.SendStrings(base) + } else { + pb.SubCommand(pb.Goargs...) + } + os.Exit(0) +} diff --git a/argv.go b/argv.go deleted file mode 100644 index 81d8431..0000000 --- a/argv.go +++ /dev/null @@ -1,184 +0,0 @@ -// Copyright 2017-2025 WIT.COM Inc. All rights reserved. -// Use of this source code is governed by the GPL 3.0 - -package main - -/* - this parses the command line arguements - this enables command line options from other packages like 'gui' and 'log' -*/ - -import ( - "os" - - "go.wit.com/lib/debugger" - "go.wit.com/lib/fhelp" - "go.wit.com/lib/gui/logsettings" - "go.wit.com/lib/protobuf/argvpb" - "go.wit.com/log" -) - -var argv args - -type args struct { - Git *GitCmd `arg:"subcommand:git" help:"git stuff"` - Build *BuildCmd `arg:"subcommand:build" help:"build and install things"` - ListPkgs *EmptyCmd `arg:"subcommand:list" help:"list all the packages on mirrors.wit.com"` - Clone *CloneCmd `arg:"subcommand:go-clone" help:"go-clone from a gowebd repomap"` - Linux *LinuxCmd `arg:"subcommand:linux" help:"helpful linux things"` - Zoo *EmptyCmd `arg:"subcommand:zoo" help:"WIT Private Cloud info"` - PB *ProtoCmd `arg:"subcommand:pb" help:"example .pb files"` - Droplet *DropletCmd `arg:"subcommand:droplet" help:"do things on virtual machines"` - Upgrade *UpgradeCmd `arg:"subcommand:upgrade" help:"apt upgrade packages installed from mirrors.wit.com"` - Publish *PublishCmd `arg:"subcommand:publish" help:"publish packages"` - Gui *GuiCmd `arg:"subcommand:gui" help:"start the gui"` - RepoMap string `arg:"--repomap" help:"location of the repomap"` - DryRun bool `arg:"--dry-run" help:"only show what would be packaged"` - Install bool `arg:"--install" help:"go install the binaries first"` - Verbose bool `arg:"--verbose" help:"be loud about it"` - Force bool `arg:"--force" help:"rebuild everything"` - All bool `arg:"--all" help:"show everything"` - Recursive bool `arg:"--recursive" help:"go-clone --recursive"` - WITCOM bool `arg:"--witcom" help:"add the GPL header"` - Max int32 `arg:"--max" help:"stop building after max builds"` -} - -type BuildCmd struct { - Debian *DebianCmd `arg:"subcommand:deb" help:"build missing .deb packages"` - MacBuild *EmptyCmd `arg:"subcommand:macos" help:"build macos packages"` - Install *DefaultCmd `arg:"subcommand:install" help:"run make install in each repo"` -} - -type ProtoCmd struct { - Identify string `arg:"--identify" help:"identify a .pb file"` -} - -type DropletCmd struct { - Trim *EmptyCmd `arg:"subcommand:trim" help:"clean out stuff and power off vm"` -} - -type GuiCmd struct { - GuiToolkit string `arg:"--gui" help:"which GUI toolkit to load"` - GuiDebug string `arg:"--gui-debug" help:"enable GUI debugging"` - GuiBuild string `arg:"--gui-build" help:"build the GUI plugins"` -} - -type LinuxCmd struct { - Rdate *EmptyCmd `arg:"subcommand:rdate" help:"rdate: check the time from the network"` -} - -type DebianCmd struct { - Release bool `arg:"--release" help:"use go-deb --release"` - DryRun bool `arg:"--dry-run" help:"only show what would be packaged"` - Verbose bool `arg:"--verbose" help:"be loud about it"` - Force bool `arg:"--force" help:"force devel .deb builds into /incoming"` - Priv bool `arg:"--private" help:"build private repos"` - All bool `arg:"--all" help:"build everything again"` - BuildVersion int `arg:"--buildversion" help:"the build version"` - Arch string `arg:"--arch" help:"what arch"` -} - -type CloneCmd struct { - Check *EmptyCmd `arg:"subcommand:check" help:"check urls against the repomap"` - Fix *EmptyCmd `arg:"subcommand:fix" help:"fix urls from the repomap"` - Repomap *EmptyCmd `arg:"subcommand:repomap" help:"make updated repomap"` - DryRun bool `arg:"--dry-run" help:"show what has not yet come to pass"` - Verbose bool `arg:"--verbose" help:"be loud about it"` -} - -type UpgradeCmd struct { - All bool `arg:"--all" help:"show all the packages"` - Force bool `arg:"--force" help:"force un-install and re-install each package"` -} - -type PublishCmd struct { - All bool `arg:"--all" help:"show all the packages"` - Gomod bool `arg:"--keep-gomod" help:"don't really do anything"` -} - -type DefaultCmd struct { - DryRun bool `arg:"--dry-run" help:"show what has not yet come to pass"` - Verbose bool `arg:"--verbose" help:"be loud about it"` - Force bool `arg:"--force" help:"rebuild everything"` -} - -type GitCmd struct { - Log *EmptyCmd `arg:"subcommand:log" help:"git log"` - Who *EmptyCmd `arg:"subcommand:who" help:"git who"` - Tag *EmptyCmd `arg:"subcommand:tag" help:"show tags"` - Pull *EmptyCmd `arg:"subcommand:pull" help:"pull the wit standard paths"` - Push *EmptyCmd `arg:"subcommand:push" help:"push the wit standard paths"` - Create string `arg:"--create" help:"create new repo"` - Edit string `arg:"--edit" help:"edit the subject of the last commit"` - ChopHEAD int `arg:"--chop-head-off" help:"remove the last commit (HEAD-1)"` - DeleteUntracked bool `arg:"--delete-untracked" help:"delete the untracked files"` -} - -type EmptyCmd struct { -} - -func init() { - if debugger.ArgDebug() { - log.Info("cmd line --debugger == true") - go func() { - log.Sleep(2) - debugger.DebugWindow() - }() - } - - if debugger.ArgLogger() { - log.Info("cmd line --loggger == true") - go func() { - log.Sleep(4) - logsettings.LogWindow() - logsettings.LogWindow() - }() - } -} - -func (args) Buildtime() (string, string) { - return BUILDTIME, VERSION -} - -func (args) Version() string { - return "wit-test " + VERSION + " Built on " + BUILDTIME -} - -/* - handles shell autocomplete -*/ - -func (args) Appname() string { - return ARGNAME -} - -func (args) ArgvGui() error { - me.myGui = fhelp.Gui() // adds the GUI package argv support - return nil -} - -func (a args) DoAutoComplete(pb *argvpb.Argv) { - base := []string{"build", "upgrade", "git", "publish", "pb", "linux", "droplet"} - base = append(base, "--version", "--force", "--all") - - // add these only if installed - if _, err := fhelp.CheckCmd("zood"); err == nil { - base = append(base, "zoo") - } - if _, err := fhelp.CheckCmd("forge"); err == nil { - base = append(base, "forge") - } - if _, err := fhelp.CheckCmd("go-clone"); err == nil { - base = append(base, "go-clone") - } - if areSuperuser() { - base = append(base, "upgrade") - base = append(base, "rdate") - } - if pb.Cmd == "" { - pb.SendStrings(base) - } else { - pb.SubCommand(pb.Goargs...) - } - os.Exit(0) -} diff --git a/argv.struct.go b/argv.struct.go new file mode 100644 index 0000000..2414ac2 --- /dev/null +++ b/argv.struct.go @@ -0,0 +1,108 @@ +// Copyright 2017-2025 WIT.COM Inc. All rights reserved. +// Use of this source code is governed by the GPL 3.0 + +package main + +/* + this parses the command line arguements + this enables command line options from other packages like 'gui' and 'log' +*/ + +var argv args + +type args struct { + Git *GitCmd `arg:"subcommand:git" help:"git stuff"` + Build *BuildCmd `arg:"subcommand:build" help:"build and install things"` + ListPkgs *EmptyCmd `arg:"subcommand:list" help:"list all the packages on mirrors.wit.com"` + Clone *CloneCmd `arg:"subcommand:go-clone" help:"go-clone from a gowebd repomap"` + Linux *LinuxCmd `arg:"subcommand:linux" help:"helpful linux things"` + Zoo *EmptyCmd `arg:"subcommand:zoo" help:"WIT Private Cloud info"` + PB *ProtoCmd `arg:"subcommand:pb" help:"example .pb files"` + Droplet *DropletCmd `arg:"subcommand:droplet" help:"do things on virtual machines"` + Upgrade *UpgradeCmd `arg:"subcommand:upgrade" help:"apt upgrade packages installed from mirrors.wit.com"` + Publish *PublishCmd `arg:"subcommand:publish" help:"publish packages"` + Gui *GuiCmd `arg:"subcommand:gui" help:"start the gui"` + RepoMap string `arg:"--repomap" help:"location of the repomap"` + DryRun bool `arg:"--dry-run" help:"only show what would be packaged"` + Install bool `arg:"--install" help:"go install the binaries first"` + Verbose bool `arg:"--verbose" help:"be loud about it"` + Force bool `arg:"--force" help:"rebuild everything"` + All bool `arg:"--all" help:"show everything"` + Recursive bool `arg:"--recursive" help:"go-clone --recursive"` + WITCOM bool `arg:"--witcom" help:"add the GPL header"` + Max int32 `arg:"--max" help:"stop building after max builds"` +} + +type BuildCmd struct { + Debian *DebianCmd `arg:"subcommand:deb" help:"build missing .deb packages"` + MacBuild *EmptyCmd `arg:"subcommand:macos" help:"build macos packages"` + Install *DefaultCmd `arg:"subcommand:install" help:"run make install in each repo"` +} + +type ProtoCmd struct { + Identify string `arg:"--identify" help:"identify a .pb file"` +} + +type DropletCmd struct { + Trim *EmptyCmd `arg:"subcommand:trim" help:"clean out stuff and power off vm"` +} + +type GuiCmd struct { + GuiToolkit string `arg:"--gui" help:"which GUI toolkit to load"` + GuiDebug string `arg:"--gui-debug" help:"enable GUI debugging"` + GuiBuild string `arg:"--gui-build" help:"build the GUI plugins"` +} + +type LinuxCmd struct { + Rdate *EmptyCmd `arg:"subcommand:rdate" help:"rdate: check the time from the network"` +} + +type DebianCmd struct { + Release bool `arg:"--release" help:"use go-deb --release"` + DryRun bool `arg:"--dry-run" help:"only show what would be packaged"` + Verbose bool `arg:"--verbose" help:"be loud about it"` + Force bool `arg:"--force" help:"force devel .deb builds into /incoming"` + Priv bool `arg:"--private" help:"build private repos"` + All bool `arg:"--all" help:"build everything again"` + BuildVersion int `arg:"--buildversion" help:"the build version"` + Arch string `arg:"--arch" help:"what arch"` +} + +type CloneCmd struct { + Check *EmptyCmd `arg:"subcommand:check" help:"check urls against the repomap"` + Fix *EmptyCmd `arg:"subcommand:fix" help:"fix urls from the repomap"` + Repomap *EmptyCmd `arg:"subcommand:repomap" help:"make updated repomap"` + DryRun bool `arg:"--dry-run" help:"show what has not yet come to pass"` + Verbose bool `arg:"--verbose" help:"be loud about it"` +} + +type UpgradeCmd struct { + All bool `arg:"--all" help:"show all the packages"` + Force bool `arg:"--force" help:"force un-install and re-install each package"` +} + +type PublishCmd struct { + All bool `arg:"--all" help:"show all the packages"` + Gomod bool `arg:"--keep-gomod" help:"don't really do anything"` +} + +type DefaultCmd struct { + DryRun bool `arg:"--dry-run" help:"show what has not yet come to pass"` + Verbose bool `arg:"--verbose" help:"be loud about it"` + Force bool `arg:"--force" help:"rebuild everything"` +} + +type GitCmd struct { + Log *EmptyCmd `arg:"subcommand:log" help:"git log"` + Who *EmptyCmd `arg:"subcommand:who" help:"git who"` + Tag *EmptyCmd `arg:"subcommand:tag" help:"show tags"` + Pull *EmptyCmd `arg:"subcommand:pull" help:"pull the wit standard paths"` + Push *EmptyCmd `arg:"subcommand:push" help:"push the wit standard paths"` + Create string `arg:"--create" help:"create new repo"` + Edit string `arg:"--edit" help:"edit the subject of the last commit"` + ChopHEAD int `arg:"--chop-head-off" help:"remove the last commit (HEAD-1)"` + DeleteUntracked bool `arg:"--delete-untracked" help:"delete the untracked files"` +} + +type EmptyCmd struct { +} diff --git a/argv.template.go b/argv.template.go new file mode 100644 index 0000000..d438b20 --- /dev/null +++ b/argv.template.go @@ -0,0 +1,74 @@ +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 +} + +// 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 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/main.go b/main.go index 5672d44..1731828 100644 --- a/main.go +++ b/main.go @@ -13,13 +13,6 @@ import ( "go.wit.com/log" ) -// sent via -ldflags -var VERSION string -var BUILDTIME string - -// used for shell auto completion -var ARGNAME string = "wit" // todo: get this from $0 ? - //go:embed resources/* var resources embed.FS diff --git a/resources/TESTVERSION b/resources/TESTVERSION new file mode 100644 index 0000000..92bb4ed --- /dev/null +++ b/resources/TESTVERSION @@ -0,0 +1,22 @@ +-rwxr-xr-x 1 jcarr jcarr 19014488 Oct 18 11:15 basicwindow +-rwxr-xr-x 1 jcarr jcarr 24416080 Oct 18 11:13 forge +-rwxr-xr-x 1 jcarr jcarr 24309824 Oct 18 11:11 wit +-rwxr-xr-x 1 jcarr jcarr 37211480 Oct 18 10:43 gus +-rwxr-xr-x 1 jcarr jcarr 21406968 Oct 18 10:43 zood +-rwxr-xr-x 1 jcarr jcarr 25231576 Oct 18 10:43 virtigod +-rwxr-xr-x 1 jcarr jcarr 23480008 Oct 18 10:43 gowebd +-rwxr-xr-x 1 jcarr jcarr 24697680 Oct 18 10:43 guireleaser +-rwxr-xr-x 1 jcarr jcarr 22481568 Oct 18 10:43 go-gui-toolkits +-rwxr-xr-x 1 jcarr jcarr 22606424 Oct 18 10:43 go-mod-clean +-rwxr-xr-x 1 jcarr jcarr 22574936 Oct 18 10:43 go-clone +-rwxr-xr-x 1 jcarr jcarr 23427472 Oct 18 10:43 virtigo +-rwxr-xr-x 1 jcarr jcarr 32913280 Oct 18 10:43 regex +-rwxr-xr-x 1 jcarr jcarr 21725120 Oct 18 10:43 zookeeper +-rwxr-xr-x 1 jcarr jcarr 22644600 Oct 18 10:43 go-deb +-rwxr-xr-x 1 jcarr jcarr 20928632 Oct 18 10:43 virtigoctl +-rwxr-xr-x 1 jcarr jcarr 21523896 Oct 18 10:43 jcarrgitpull +-rwxr-xr-x 1 jcarr jcarr 22328608 Oct 18 10:43 fixup +-rwxr-xr-x 1 jcarr jcarr 21460328 Oct 18 10:43 mirrors +-rwxr-xr-x 1 jcarr jcarr 9937936 Oct 18 10:43 going2git +-rwxr-xr-x 1 jcarr jcarr 9444608 Oct 18 10:43 startxplacement +-rwxr-xr-x 1 jcarr jcarr 8701912 Oct 18 10:43 powerpaneld diff --git a/structs.go b/structs.go index 4ab8dd7..732bb2a 100644 --- a/structs.go +++ b/structs.go @@ -6,6 +6,7 @@ package main import ( "sync" + "go.wit.com/dev/alexflint/arg" "go.wit.com/lib/debian" "go.wit.com/lib/fhelp" "go.wit.com/lib/protobuf/argvpb" @@ -19,6 +20,7 @@ var me *mainType type mainType struct { once sync.Once // one-time initialized data argv *argvpb.Argv // more experiments for bash handling + pp *arg.Parser // for parsing the command line args. Yay to alexf lint! forge *forgepb.Forge // your customized repo preferences and settings machine *zoopb.Machine // your customized repo preferences and settings homedir string // where the user homedir is -- cgit v1.2.3