From f730ed88ee48128da1d4f841fd43616621613f00 Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Sun, 19 Oct 2025 05:44:42 -0500 Subject: new argv that somewhat works better --- argv.custom.go | 50 +++++++++++++++++++++++++ argv.go | 109 ------------------------------------------------------- argv.struct.go | 39 ++++++++++++++++++++ argv.template.go | 80 ++++++++++++++++++++++++++++++++++++++++ exit.go | 8 ++-- main.go | 6 --- structs.go | 9 ++++- 7 files changed, 180 insertions(+), 121 deletions(-) create mode 100644 argv.custom.go delete mode 100644 argv.go create mode 100644 argv.struct.go create mode 100644 argv.template.go diff --git a/argv.custom.go b/argv.custom.go new file mode 100644 index 0000000..f49adcd --- /dev/null +++ b/argv.custom.go @@ -0,0 +1,50 @@ +package main + +import ( + "os" + + "go.wit.com/lib/protobuf/argvpb" + "go.wit.com/log" +) + +// sent via -ldflags +var VERSION string +var BUILDTIME string + +var APPNAME string = "virtigo" + +func (a args) Description() string { + return ` + virtigo: control your cluster + +This maintains a master list of all your vm's (aka 'droplets') +in your homelab cloud. You can import libvirt xml files. +This app talks to your hypervisors via the virtigod daemon. +` +} + +var INFO *log.LogFlag +var POLL *log.LogFlag +var WARN *log.LogFlag +var EVENT *log.LogFlag + +func init() { + full := "go.wit.com/apps/virtigo" + short := "virtigo" + + INFO = log.NewFlag("INFO", false, full, short, "general virtigo") + POLL = log.NewFlag("POLL", false, full, short, "virtigo polling") + WARN = log.NewFlag("WARN", true, full, short, "bad things") + EVENT = log.NewFlag("EVENT", true, full, short, "hypeprvisor/droplet events") +} + +// sends the strings to bash or zsh that will be your options +func (a args) SendCompletionStrings(pb *argvpb.Argv) { + if pb.Cmd == "" { + base := []string{"--version", "list", "droplet"} + pb.SendStrings(base) + } else { + pb.SubCommand(pb.Goargs...) + } + os.Exit(0) +} diff --git a/argv.go b/argv.go deleted file mode 100644 index aba76e7..0000000 --- a/argv.go +++ /dev/null @@ -1,109 +0,0 @@ -package main - -import ( - "os" - - "go.wit.com/lib/protobuf/argvpb" - "go.wit.com/log" -) - -/* - this parses the command line arguements - - this enables command line options from other packages like 'gui' and 'log' -*/ - -var argv args - -type args struct { - List *ListCmd `arg:"subcommand:list" help:"list things"` - Droplet *DropletCmd `arg:"subcommand:droplet" help:"send events to a droplet"` - Config string `arg:"env:VIRTIGO_HOME" help:"defaults to ~/.config/virtigo/"` - Server string `arg:"env:VIRTIGO_SERVER" help:"what virtigo cluster to connect to"` - Localhost bool `arg:"--localhost" help:"use the local libvirt"` - Daemon bool `arg:"--daemon" help:"run as a daemon"` - Verbose bool `arg:"--verbose" help:"talk more"` - Port int `arg:"--port" default:"8080" help:"allow droplet events via http"` - Xml []string `arg:"--libvirt" help:"import qemu xml files: --libvirt /etc/libvirt/qemu/*.xml"` - Admin bool `arg:"--admin" help:"enter admin mode"` - Bash bool `arg:"--bash" help:"generate bash completion"` - BashAuto []string `arg:"--auto-complete" help:"todo: move this to go-arg"` -} - -type EmptyCmd struct { -} - -type testCmd string - -type ListCmd struct { - Droplets *EmptyCmd `arg:"subcommand:droplets" help:"list droplets"` - Hypervisors *EmptyCmd `arg:"subcommand:hypervisors" help:"list hypervisors"` - On bool `arg:"--on" help:"only show things that are on"` -} - -type DropletCmd struct { - Start *EmptyCmd `arg:"subcommand:start" help:"start droplet"` - Stop *EmptyCmd `arg:"subcommand:stop" help:"stop droplet"` - Show *EmptyCmd `arg:"subcommand:show" help:"show droplet"` - Console *EmptyCmd `arg:"subcommand:console" help:"open serial console"` - VNC *EmptyCmd `arg:"subcommand:vnc" help:"open VNC console"` - Spice *EmptyCmd `arg:"subcommand:spice" help:"open spiceconsole"` - Name string `arg:"--name" help:"what droplet to start"` -} - -func (a args) Description() string { - return ` - virtigo: control your cluster - -This maintains a master list of all your vm's (aka 'droplets') -in your homelab cloud. You can import libvirt xml files. -This app talks to your hypervisors via the virtigod daemon. -` -} - -var INFO *log.LogFlag -var POLL *log.LogFlag -var WARN *log.LogFlag -var EVENT *log.LogFlag - -func init() { - full := "go.wit.com/apps/virtigo" - short := "virtigo" - - INFO = log.NewFlag("INFO", false, full, short, "general virtigo") - POLL = log.NewFlag("POLL", false, full, short, "virtigo polling") - WARN = log.NewFlag("WARN", true, full, short, "bad things") - EVENT = log.NewFlag("EVENT", true, full, short, "hypeprvisor/droplet events") -} - -/* - handles shell autocomplete -*/ - -func (args) Version() string { - return argvpb.StandardVersion(ARGNAME, VERSION, BUILDTIME) -} - -func (args) Appname() string { - return ARGNAME -} - -func (args) Buildtime() (string, string) { - return BUILDTIME, VERSION -} - -func (args) ArgvGui() error { - me.myGui = argvpb.Gui() // adds the GUI package argv support - return nil -} - -// sends the strings to bash or zsh that will be your options -func (a args) SendCompletionStrings(pb *argvpb.Argv) { - if pb.Cmd == "" { - base := []string{"--version", "list", "droplet"} - 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..36f7927 --- /dev/null +++ b/argv.struct.go @@ -0,0 +1,39 @@ +package main + +var argv args + +type args struct { + List *ListCmd `arg:"subcommand:list" help:"list things"` + Droplet *DropletCmd `arg:"subcommand:droplet" help:"send events to a droplet"` + Config string `arg:"env:VIRTIGO_HOME" help:"defaults to ~/.config/virtigo/"` + Server string `arg:"env:VIRTIGO_SERVER" help:"what virtigo cluster to connect to"` + Localhost bool `arg:"--localhost" help:"use the local libvirt"` + Daemon bool `arg:"--daemon" help:"run as a daemon"` + Verbose bool `arg:"--verbose" help:"talk more"` + Port int `arg:"--port" default:"8080" help:"allow droplet events via http"` + Xml []string `arg:"--libvirt" help:"import qemu xml files: --libvirt /etc/libvirt/qemu/*.xml"` + Admin bool `arg:"--admin" help:"enter admin mode"` + Bash bool `arg:"--bash" help:"generate bash completion"` + BashAuto []string `arg:"--auto-complete" help:"todo: move this to go-arg"` +} + +type EmptyCmd struct { +} + +type testCmd string + +type ListCmd struct { + Droplets *EmptyCmd `arg:"subcommand:droplets" help:"list droplets"` + Hypervisors *EmptyCmd `arg:"subcommand:hypervisors" help:"list hypervisors"` + On bool `arg:"--on" help:"only show things that are on"` +} + +type DropletCmd struct { + Start *EmptyCmd `arg:"subcommand:start" help:"start droplet"` + Stop *EmptyCmd `arg:"subcommand:stop" help:"stop droplet"` + Show *EmptyCmd `arg:"subcommand:show" help:"show droplet"` + Console *EmptyCmd `arg:"subcommand:console" help:"open serial console"` + VNC *EmptyCmd `arg:"subcommand:vnc" help:"open VNC console"` + Spice *EmptyCmd `arg:"subcommand:spice" help:"open spiceconsole"` + Name string `arg:"--name" help:"what droplet to start"` +} 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/exit.go b/exit.go index 896ff29..a7de318 100644 --- a/exit.go +++ b/exit.go @@ -12,26 +12,26 @@ import ( func okExit(note string) { if note != "" { - log.Info(ARGNAME, "exit:", note, "ok") + log.Info(APPNAME, "exit:", note, "ok") } gui.StandardExit() os.Exit(0) } func badExit(err error) { - log.Info(ARGNAME, "failed: ", err) + log.Info(APPNAME, "failed: ", err) gui.StandardExit() os.Exit(-1) } func exit(note string, err error) { if note != "" { - log.Info(ARGNAME, "exit:", note, "ok") + log.Info(APPNAME, "exit:", note, "ok") } gui.StandardExit() if err == nil { os.Exit(0) } - log.Info(ARGNAME, "failed: ", err) + log.Info(APPNAME, "failed: ", err) os.Exit(-1) } diff --git a/main.go b/main.go index a18b147..34fc02d 100644 --- a/main.go +++ b/main.go @@ -14,12 +14,6 @@ import ( "go.wit.com/log" ) -// sent via -ldflags -var VERSION string -var BUILDTIME string - -var ARGNAME string = "virtigo" - //go:embed resources/* var resources embed.FS diff --git a/structs.go b/structs.go index 39a85b1..5dfcdc1 100644 --- a/structs.go +++ b/structs.go @@ -4,9 +4,12 @@ import ( "net/url" "time" + "go.wit.com/dev/alexflint/arg" "go.wit.com/gui" + "go.wit.com/lib/fhelp" "go.wit.com/lib/gadgets" "go.wit.com/lib/protobuf/argvpb" + "go.wit.com/lib/protobuf/forgepb" "go.wit.com/lib/protobuf/virtpb" ) @@ -24,8 +27,10 @@ func (b *virtigoT) Enable() { // this app's variables type virtigoT struct { - argv *argvpb.Argv // shell autocomplete - myGui *argvpb.GuiPrep // the gui toolkit handle + 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 // your customized repo preferences and settings e *virtpb.Events // virt protobuf events hmap map[*virtpb.Hypervisor]*HyperT // map to the local struct names []string // ? -- cgit v1.2.3