Restore config loading, keep exported Version, keep cgo off
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.
This commit is contained in:
+30
-4
@@ -7,6 +7,7 @@ package cli
|
||||
|
||||
import (
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
// Params configures New.
|
||||
@@ -14,23 +15,48 @@ 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.`,
|
||||
Version: p.Version,
|
||||
SilenceUsage: true,
|
||||
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(p),
|
||||
newInfoCommand(a),
|
||||
newGenerateCommand(),
|
||||
newCBKCommand(),
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user