summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJeff Carr <[email protected]>2025-10-19 02:55:45 -0500
committerJeff Carr <[email protected]>2025-10-19 02:55:45 -0500
commitf9a62d1c3aeb9e99214cc16a664705d9a517fac3 (patch)
treec0c2dc5199b9388669f6509419a0b93ae054b371
parent3a7d5a176350447cf568d9fbc7ea804895549968 (diff)
make doTestVersion()
-rw-r--r--argv.custom.go2
-rw-r--r--argv.struct.go8
-rw-r--r--argv.template.go6
-rw-r--r--doTest.go115
-rw-r--r--main.go80
-rw-r--r--sh/sh.go28
-rw-r--r--subCommand.go96
7 files changed, 254 insertions, 81 deletions
diff --git a/argv.custom.go b/argv.custom.go
index c8c97be..25904ee 100644
--- a/argv.custom.go
+++ b/argv.custom.go
@@ -66,7 +66,7 @@ func (args) ArgvGui() error {
}
func (a args) DoAutoComplete(pb *argvpb.Argv) {
- base := []string{"build", "upgrade", "git", "publish", "pb", "linux", "droplet"}
+ base := []string{"build", "upgrade", "git", "publish", "pb", "linux", "droplet", "test"}
base = append(base, "--version", "--force", "--all")
// add these only if installed
diff --git a/argv.struct.go b/argv.struct.go
index 2414ac2..ae0d1f9 100644
--- a/argv.struct.go
+++ b/argv.struct.go
@@ -21,7 +21,7 @@ type args struct {
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"`
+ Test *TestCmd `arg:"subcommand:test" help:"test things"`
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"`
@@ -47,10 +47,8 @@ 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 TestCmd struct {
+ Version *EmptyCmd `arg:"subcommand:version" help:"clean out stuff and power off vm"`
}
type LinuxCmd struct {
diff --git a/argv.template.go b/argv.template.go
index d438b20..5ac7060 100644
--- a/argv.template.go
+++ b/argv.template.go
@@ -31,6 +31,12 @@ func (args) ParseFlags(flags []string) error {
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...)
diff --git a/doTest.go b/doTest.go
new file mode 100644
index 0000000..01f7caa
--- /dev/null
+++ b/doTest.go
@@ -0,0 +1,115 @@
+// Copyright 2017-2025 WIT.COM Inc. All rights reserved.
+// Use of this source code is governed by the GPL 3.0
+
+package main
+
+import (
+ "errors"
+ "strings"
+
+ "github.com/go-cmd/cmd"
+ "go.wit.com/lib/config"
+ "go.wit.com/lib/gui/shell"
+ "go.wit.com/log"
+)
+
+func doTest() (string, error) {
+ var s string
+ var err error
+
+ if argv.Test.Version != nil {
+ var data []byte
+ data, err = resources.ReadFile("resources/TESTVERSION")
+ if err != nil {
+ s = "open repomap failed"
+ log.Info(s, err)
+ return s, err
+ }
+ if len(data) == 0 {
+ s = "resources/repomap is empty"
+ log.Info(s, err)
+ return s, config.ErrEmpty
+ }
+ var broken string
+ for _, line := range strings.Split(string(data), "\n") {
+ line := strings.TrimSpace(line)
+ if line == "" {
+ continue
+ }
+ parts := strings.Fields(line)
+ cmdname := parts[len(parts)-1]
+ log.Info("LINE=", cmdname)
+ r := shell.Run([]string{cmdname, "--version"})
+ if r.Error != nil {
+ return cmdname + " version test failed", r.Error
+ }
+ if len(r.Stdout) != 1 {
+ for _, line := range r.Stderr {
+ log.Info("STDERR:", line)
+ }
+ for _, line := range r.Stdout {
+ log.Info("STDOUT:", line)
+ }
+ broken += cmdname + " "
+ }
+ }
+ if broken != "" {
+ return "version tests failed", errors.New(broken)
+ }
+ return "all version tests passed", nil
+ }
+ return "nothing tested", nil
+}
+
+func versionStderr(cmdname string, r *cmd.Status) error {
+ stderr := errors.New(cmdname + " version test broken")
+
+ for _, line := range r.Stderr {
+ log.Info("STDERR:", line)
+ }
+ for _, line := range r.Stdout {
+ log.Info("STDOUT:", line)
+ }
+ if r.Error != nil {
+ return errors.Join(stderr, r.Error)
+ }
+ if r.Exit == 0 {
+ return errors.Join(stderr, errors.New("exit -1"))
+ }
+ return stderr
+}
+
+func doTestVersion(cmdname string) error {
+
+ r := shell.Run([]string{cmdname, "--version"})
+ if r.Exit != 0 {
+ return versionStderr(cmdname, &r)
+ }
+ if r.Error != nil {
+ return versionStderr(cmdname, &r)
+ }
+ if len(r.Stdout) == 0 {
+ return versionStderr(cmdname, &r)
+ }
+ if len(r.Stdout) == 1 {
+ line := r.Stdout[0]
+ if !strings.HasPrefix(line, cmdname) {
+ return versionStderr(cmdname, &r)
+ }
+ // probably okay
+ return nil
+ }
+ if len(r.Stdout) == 2 {
+ line := r.Stdout[0]
+ if !strings.HasPrefix(line, "APPNAME=") {
+ return versionStderr(cmdname, &r)
+ }
+ line = r.Stdout[1]
+ if !strings.HasPrefix(line, cmdname) {
+ return versionStderr(cmdname, &r)
+ }
+ // probably okay
+ return nil
+ }
+ return versionStderr(cmdname, &r)
+}
diff --git a/main.go b/main.go
index 1731828..c8d5e80 100644
--- a/main.go
+++ b/main.go
@@ -9,7 +9,6 @@ import (
"unicode"
"go.wit.com/lib/protobuf/argvpb"
- "go.wit.com/lib/protobuf/filepb"
"go.wit.com/log"
)
@@ -35,84 +34,15 @@ func main() {
var s string
var err error
- if argv.Upgrade != nil {
- doUpgrade()
- me.argv.GoodExit("")
- }
-
- if argv.Linux != nil {
- if argv.Linux.Rdate != nil {
- s, err = doRdate()
- } else {
- setTerminalTitle("pinging google", "ping", []string{"google.com"})
- }
- }
-
- if argv.Build != nil {
- s, err = doBuild()
- }
-
- if argv.Git != nil {
- initMachine()
- s, err = doGit()
- }
-
- if argv.PB != nil {
- pbuuid, pbver, pberr := filepb.IdentifyPB(argv.PB.Identify)
- if pberr == nil {
- log.Info("pb version is:", pbver)
- log.Info("pb uuid is:", pbuuid)
- s = "pb identify worked"
- } else {
- s = "identify failed"
- err = pberr
- }
- }
-
- if argv.Clone != nil {
- doClone()
- me.argv.GoodExit("")
- }
-
- if argv.WITCOM {
- doWITCOM()
- me.argv.GoodExit("")
- }
-
- if argv.Clone != nil {
- doClone()
- me.argv.GoodExit("")
- }
-
- if argv.WITCOM {
- doWITCOM()
- me.argv.GoodExit("")
- }
-
- if argv.Upgrade != nil {
- doUpgrade()
- }
+ // process the argv subcommand (subCommand.go)
+ s, err = subCommand()
- if argv.Droplet != nil {
- s, err = doDroplet()
- }
-
- if argv.Publish != nil {
- if err := doPublish(); err != nil {
- me.argv.BadExit("doPublish failed", err)
- }
- me.argv.GoodExit("")
- }
-
- if argv.Zoo != nil {
- if areSuperuser() {
- exitOnErrorRealtime([]string{"journalctl", "-n", "100", "-f", "_SYSTEMD_UNIT=zood.service"})
- }
- me.argv.GoodExit("do something here")
- }
+ // argv provides timing and other features on exit
if err != nil {
+ // bad exit back to the shell via argv
me.argv.BadExit(s, err)
}
+ // a good exit back to the shell via argv
me.argv.GoodExit(s)
}
diff --git a/sh/sh.go b/sh/sh.go
new file mode 100644
index 0000000..f69de02
--- /dev/null
+++ b/sh/sh.go
@@ -0,0 +1,28 @@
+package sh
+
+// Implementations must not retain p.
+type Writer interface {
+ Write(p []byte) (n int, err error)
+}
+
+
+// Implementations must not retain p.
+type Argv interface {
+ InitIO(out io.Writer, err io.Writer)
+ InitPB(pb *argvpb.Argv)
+ WriteOUT(bytes []byte)
+ WriteERR(bytes []byte)
+ Stdout(out io.Writer)
+ Stderr(err io.Writer)
+ Match() (matches []string, err error)
+ Help() (help string, err error)
+ Exit(s string, err error)
+}
+
+// Implementations must not retain p.
+type Command interface {
+ Run(pb *shpb.Command)
+ Start(pb *shpb.Command)
+ Exit(pb *shpb.Command)
+}
+
diff --git a/subCommand.go b/subCommand.go
new file mode 100644
index 0000000..fd75fc2
--- /dev/null
+++ b/subCommand.go
@@ -0,0 +1,96 @@
+// 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/lib/protobuf/filepb"
+ "go.wit.com/log"
+)
+
+func subCommand() (string, error) {
+ // Standard subcommand handling starts here
+ var s string
+ var err error
+
+ if argv.Upgrade != nil {
+ doUpgrade()
+ me.argv.GoodExit("")
+ }
+
+ if argv.Linux != nil {
+ if argv.Linux.Rdate != nil {
+ s, err = doRdate()
+ } else {
+ setTerminalTitle("pinging google", "ping", []string{"google.com"})
+ }
+ }
+
+ if argv.Build != nil {
+ s, err = doBuild()
+ }
+
+ if argv.Test != nil {
+ s, err = doTest()
+ }
+
+ if argv.Git != nil {
+ initMachine()
+ s, err = doGit()
+ }
+
+ if argv.PB != nil {
+ pbuuid, pbver, pberr := filepb.IdentifyPB(argv.PB.Identify)
+ if pberr == nil {
+ log.Info("pb version is:", pbver)
+ log.Info("pb uuid is:", pbuuid)
+ s = "pb identify worked"
+ } else {
+ s = "identify failed"
+ err = pberr
+ }
+ }
+
+ if argv.Clone != nil {
+ doClone()
+ me.argv.GoodExit("")
+ }
+
+ if argv.WITCOM {
+ doWITCOM()
+ me.argv.GoodExit("")
+ }
+
+ if argv.Clone != nil {
+ doClone()
+ me.argv.GoodExit("")
+ }
+
+ if argv.WITCOM {
+ doWITCOM()
+ me.argv.GoodExit("")
+ }
+
+ if argv.Upgrade != nil {
+ doUpgrade()
+ }
+
+ if argv.Droplet != nil {
+ s, err = doDroplet()
+ }
+
+ if argv.Publish != nil {
+ if err := doPublish(); err != nil {
+ me.argv.BadExit("doPublish failed", err)
+ }
+ me.argv.GoodExit("")
+ }
+
+ if argv.Zoo != nil {
+ if areSuperuser() {
+ exitOnErrorRealtime([]string{"journalctl", "-n", "100", "-f", "_SYSTEMD_UNIT=zood.service"})
+ }
+ me.argv.GoodExit("do something here")
+ }
+ return s, err
+}