From 4e4f52a8bfe489dd9c9811350c4722dca878303f Mon Sep 17 00:00:00 2001 From: Victor Lacasse-Beaudoin Date: Mon, 23 Sep 2024 21:52:31 -0400 Subject: [PATCH] feature: bind client flags sur rootCmd MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Déclarer flags dans pkg/bottinagenda/flag.go --- cmd/bottinagenda/root.go | 6 ++++++ pkg/bottinagenda/flag.go | 46 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 pkg/bottinagenda/flag.go 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 +}