From d979bfc49ea930134ddedba28eb087716f5c25f8 Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Tue, 28 Feb 2017 16:56:17 +0100 Subject: [PATCH] Add flag validation --- cmd/root.go | 21 ++++++++++++++++----- keys.yaml | 1 - 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index 896b7c66..05fffd48 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -19,12 +19,14 @@ import ( "os" "strings" + "github.com/pkg/errors" "github.com/spf13/cobra" "github.com/spf13/viper" ) var ( rootDir string + format string ) // RootCmd represents the base command when called without any subcommands @@ -37,9 +39,6 @@ These keys may be in any format supported by go-crypto and can be used by light-clients, full nodes, or any other application that needs to sign with a private key.`, PersistentPreRunE: bindFlags, - // Uncomment the following line if your bare application - // has an action associated with it: - // Run: func(cmd *cobra.Command, args []string) { }, } // Execute adds all child commands to the root command sets flags appropriately. @@ -53,7 +52,8 @@ func Execute() { func init() { cobra.OnInitialize(initEnv) - RootCmd.PersistentFlags().StringP("root", "r", os.ExpandEnv("$HOME/.tlc"), "root directory for config and data (default is $HOME/.tlc)") + RootCmd.PersistentFlags().StringP("root", "r", os.ExpandEnv("$HOME/.tlc"), "root directory for config and data (default is TM_ROOT or $HOME/.tlc)") + RootCmd.PersistentFlags().StringP("format", "f", "text", "Output format (text|json)") } // initEnv sets to use ENV variables if set. @@ -80,5 +80,16 @@ func bindFlags(cmd *cobra.Command, args []string) error { fmt.Println("Using config file:", viper.ConfigFileUsed()) } - return nil + return validateFlags(cmd) +} + +// validateFlags asserts all RootCmd flags are valid +func validateFlags(cmd *cobra.Command) error { + format = viper.GetString("format") + switch format { + case "text", "json": + return nil + default: + return errors.Errorf("Unsupported format: %s", format) + } } diff --git a/keys.yaml b/keys.yaml index 3aec88a7..812adb72 100644 --- a/keys.yaml +++ b/keys.yaml @@ -1,2 +1 @@ -format: super name: george