check / check (push) Successful in 1m4s
cmd/simplexcalc/ now holds only main.go: it passes the version the linker sets to cli.Main and exits with the status that returns. root.go and run.go move to internal/cli with their comments, changed only to take the version as a parameter. Main reads the process's arguments and output; execute takes them as parameters so the new tests can check the version line and the non-zero status of an unknown command. Commands, flags, output, log lines and exit status are unchanged, and the -X main.version flag in the Dockerfile still applies. Model: opus-5-5
39 lines
995 B
Go
39 lines
995 B
Go
// Package cli is the simplexcalc command tree: the run and version
|
|
// commands, and the exit status the program ends with.
|
|
package cli
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
)
|
|
|
|
// appname is the name the program reports in its logs and its version.
|
|
const appname = "simplexcalc"
|
|
|
|
// Main runs the command named on the command line and returns the exit
|
|
// status for main to pass to os.Exit.
|
|
func Main(version string) int {
|
|
return execute(version, os.Args[1:], os.Stdout, os.Stderr)
|
|
}
|
|
|
|
// execute is Main with the arguments and the output passed in, so that
|
|
// a test can supply them.
|
|
func execute(version string, args []string, stdout, stderr io.Writer) int {
|
|
root := rootCmd(version)
|
|
root.SetArgs(args)
|
|
root.SetOut(stdout)
|
|
root.SetErr(stderr)
|
|
|
|
err := root.Execute()
|
|
if err != nil {
|
|
// cobra has already printed the error; this only sets the exit
|
|
// status, which is what a supervisor and a shell script read.
|
|
_, _ = fmt.Fprintln(stderr, "exiting: "+err.Error())
|
|
|
|
return 1
|
|
}
|
|
|
|
return 0
|
|
}
|