Bring back the in-progress viper and go-homedir configuration loading from the original root command as a persistent pre-run on the root command, with a --config flag and a config line in sutil info. Restore the exported main.Version with a nolint directive instead of renaming it. Export CGO_ENABLED=0 from the Makefile, drop -race from make test, and remove the C toolchain from the Docker builder stage.
66 lines
1.9 KiB
Go
66 lines
1.9 KiB
Go
// Package cli implements the sutil command tree.
|
|
//
|
|
// The tree is built by New rather than assembled in init functions, so
|
|
// the package holds no global state and tests can construct it repeatedly
|
|
// with captured input and output.
|
|
package cli
|
|
|
|
import (
|
|
"github.com/spf13/cobra"
|
|
"github.com/spf13/viper"
|
|
)
|
|
|
|
// Params configures New.
|
|
type Params struct {
|
|
// Version is the version string embedded at build time. It is shown
|
|
// by `sutil version`, `sutil --version`, and `sutil info`.
|
|
Version string
|
|
|
|
// ConfigDir is the directory searched for the optional .sneak
|
|
// configuration file. Empty means the user's home directory; tests set
|
|
// it to a temporary directory so the real home is never consulted.
|
|
ConfigDir string
|
|
}
|
|
|
|
// app carries the state shared by every command: the build parameters,
|
|
// the --config flag, and the configuration loaded before any command runs.
|
|
type app struct {
|
|
params Params
|
|
configFile string
|
|
config *viper.Viper
|
|
}
|
|
|
|
// New builds the root sutil command with every subcommand attached.
|
|
func New(p Params) *cobra.Command {
|
|
a := &app{params: p, configFile: "", config: nil}
|
|
|
|
root := &cobra.Command{
|
|
Use: "sutil",
|
|
Short: "sneak's shell utility",
|
|
Long: `sutil is a small collection of shell utilities built on the packages in
|
|
sneak.berlin/go/x: fast pseudorandom byte streams, memorable passwords,
|
|
BIP39 mnemonics, and crypto_box keypairs derived from them.
|
|
|
|
Before any command runs, sutil loads the optional configuration file named
|
|
by --config, or else ~/.sneak.yaml (or .json, .toml, ...) if it exists, and
|
|
makes environment variables available alongside it.`,
|
|
Version: p.Version,
|
|
SilenceUsage: true,
|
|
PersistentPreRunE: a.loadConfig,
|
|
}
|
|
|
|
root.PersistentFlags().StringVar(
|
|
&a.configFile, "config", "",
|
|
"config file (default is "+configName+".* in the home directory)",
|
|
)
|
|
|
|
root.AddCommand(
|
|
newVersionCommand(p),
|
|
newInfoCommand(a),
|
|
newGenerateCommand(),
|
|
newCBKCommand(),
|
|
)
|
|
|
|
return root
|
|
}
|