feature: bind client flags sur rootCmd

Déclarer flags dans pkg/bottinagenda/flag.go
This commit is contained in:
Victor Lacasse-Beaudoin 2024-09-23 21:52:31 -04:00
parent c9e906bcee
commit 4e4f52a8bf
2 changed files with 52 additions and 0 deletions

View file

@ -2,9 +2,11 @@ package main
import ( import (
"fmt" "fmt"
"log"
"os" "os"
"strings" "strings"
"git.agecem.com/bottin/bottinagenda/pkg/bottinagenda"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper" "github.com/spf13/viper"
) )
@ -52,4 +54,8 @@ func init() {
cobra.OnInitialize(initConfig) cobra.OnInitialize(initConfig)
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.bottinagenda.yaml)") rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.bottinagenda.yaml)")
if err := bottinagenda.BindClientFlags(rootCmd.PersistentFlags()); err != nil {
log.Fatal(err)
}
} }

46
pkg/bottinagenda/flag.go Normal file
View file

@ -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
}