diff --git a/cmd/bottinagenda/root.go b/cmd/bottinagenda/root.go index 9a72e42..79d7964 100644 --- a/cmd/bottinagenda/root.go +++ b/cmd/bottinagenda/root.go @@ -2,9 +2,11 @@ package main import ( "fmt" + "log" "os" "strings" + "git.agecem.com/bottin/bottinagenda/pkg/bottinagenda" "github.com/spf13/cobra" "github.com/spf13/viper" ) @@ -52,4 +54,8 @@ func init() { cobra.OnInitialize(initConfig) rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.bottinagenda.yaml)") + + if err := bottinagenda.BindClientFlags(rootCmd.PersistentFlags()); err != nil { + log.Fatal(err) + } } diff --git a/pkg/bottinagenda/flag.go b/pkg/bottinagenda/flag.go new file mode 100644 index 0000000..b382e31 --- /dev/null +++ b/pkg/bottinagenda/flag.go @@ -0,0 +1,46 @@ +package bottinagenda + +import ( + "fmt" + + "github.com/spf13/pflag" + "github.com/spf13/viper" +) + +func BindClientFlags(set *pflag.FlagSet) error { + if set == nil { + return fmt.Errorf("cannot bind client flags on nil flagSet") + } + + set.StringP("client-outputstyle", "o", "json", "CLI output style") + if err := viper.BindPFlag("Client.OutputStyle", set.Lookup("client-outputstyle")); err != nil { + return err + } + + set.String("client-api-host", "localhost", "Remote API server host") + if err := viper.BindPFlag("Client.API.Host", set.Lookup("client-api-host")); err != nil { + return err + } + + set.String("client-api-key", "", "Remote API server key") + if err := viper.BindPFlag("Client.API.Key", set.Lookup("client-api-key")); err != nil { + return err + } + + set.Int("client-api-port", 1313, "Remote API server port") + if err := viper.BindPFlag("Client.API.Port", set.Lookup("client-api-port")); err != nil { + return err + } + + set.Bool("client-api-tls-enabled", false, "Remote API server TLS state") + if err := viper.BindPFlag("Client.API.TLS.Enabled", set.Lookup("client-api-tls-enabled")); err != nil { + return err + } + + set.Bool("client-api-tls-skipverify", false, "Do not verify remote API server TLS certificate") + if err := viper.BindPFlag("Client.API.TLS.SkipVerify", set.Lookup("client-api-tls-skipverify")); err != nil { + return err + } + + return nil +}