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
50 lines
1.4 KiB
Go
50 lines
1.4 KiB
Go
package cli
|
|
|
|
import (
|
|
"fmt"
|
|
"runtime"
|
|
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
// rootCmd builds the command tree. Configuration comes from the
|
|
// environment (see internal/config), not from flags: the bot runs in a
|
|
// container, where the environment is the interface, and one source of
|
|
// truth means there is no precedence rule to get wrong.
|
|
func rootCmd(version string) *cobra.Command {
|
|
root := &cobra.Command{
|
|
Use: appname,
|
|
Short: appname + " — a SimpleX Chat bot that answers arithmetic",
|
|
Long: appname + " is a SimpleX Chat bot that answers arithmetic.\n\n" +
|
|
"It runs the simplex-chat command-line client, which must be on\n" +
|
|
"PATH. Configuration is read from the environment and from an\n" +
|
|
"optional .env file in the working directory. See README.md\n" +
|
|
"for the full list of variables.",
|
|
SilenceUsage: true,
|
|
// Without this, cobra prints the error itself and main prints
|
|
// it again.
|
|
SilenceErrors: true,
|
|
}
|
|
|
|
root.AddCommand(runCmd(version), versionCmd(version))
|
|
|
|
return root
|
|
}
|
|
|
|
func versionCmd(version string) *cobra.Command {
|
|
return &cobra.Command{
|
|
Use: "version",
|
|
Short: "print the version and exit",
|
|
Args: cobra.NoArgs,
|
|
RunE: func(cmd *cobra.Command, _ []string) error {
|
|
_, err := fmt.Fprintf(cmd.OutOrStdout(), "%s %s %s/%s\n",
|
|
appname, version, runtime.GOOS, runtime.GOARCH)
|
|
if err != nil {
|
|
return fmt.Errorf("writing version: %w", err)
|
|
}
|
|
|
|
return nil
|
|
},
|
|
}
|
|
}
|