Move the command tree into internal/cli (closes #8)
check / check (push) Successful in 1m40s
check / check (push) Successful in 1m40s
`cmd/simplexcalc/` now holds only `main.go`, whose body is one call, `os.Exit(cli.Main(version))`, as the Go style guide requires. The command tree and the `run` and `version` commands moved unchanged into `internal/cli`, with tests for `version` and for an unknown command. Output streams, exit statuses and the `-X main.version` build flag are as before. Disclosure: the comment in `execute` saying cobra has already printed the error is false, since the root command silences cobra's errors; it moved unchanged and is left for a follow-up. Model: opus-5-5
This commit was merged in pull request #9.
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
// Package cli is the simplexcalc command tree: the run and version
|
||||
// commands, and the exit status the program ends with.
|
||||
package cli
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
// 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.
|
||||
//
|
||||
// It leaves cobra's output streams at their defaults. cobra writes the
|
||||
// usage and an unknown help topic to stderr, but SetOut moves them onto
|
||||
// the stream it is given.
|
||||
func Main(version string) int {
|
||||
return execute(rootCmd(version))
|
||||
}
|
||||
|
||||
// execute runs root and returns the exit status. It takes the command
|
||||
// so that a test can set its arguments and output streams first.
|
||||
func execute(root *cobra.Command) int {
|
||||
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(root.ErrOrStderr(), "exiting: "+err.Error())
|
||||
|
||||
return 1
|
||||
}
|
||||
|
||||
return 0
|
||||
}
|
||||
Reference in New Issue
Block a user