From ac20221ca35f69b67e85b646b369fb72e7296441 Mon Sep 17 00:00:00 2001 From: Victor Lacasse-Beaudoin Date: Fri, 15 Sep 2023 16:13:05 -0400 Subject: [PATCH 1/3] =?UTF-8?q?Ajouter=20d=C3=A9finitions=20de=20base=20?= =?UTF-8?q?=C3=A0=20config/?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Ajouter structs Config, APIConfig et WebConfig Ajouter GetConfig pour encapsuler viper.Unmarshal Ajouter RegisterFlags pour déclarer flags cobra et associations viper --- config/config.go | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/config/config.go b/config/config.go index e69de29..e7698ea 100644 --- a/config/config.go +++ b/config/config.go @@ -0,0 +1,40 @@ +package config + +import ( + "github.com/spf13/cobra" + "github.com/spf13/viper" +) + +type Config struct { + API APIConfig + Web WebConfig +} + +type APIConfig struct { + Port int +} + +type WebConfig struct { + Port int +} + +func GetConfig() (cfg Config, err error) { + return cfg, viper.Unmarshal(&cfg) +} + +// RegisterFlags adds persistent flags necessary to the application to the specified *cobra.Command +func RegisterFlags(cmd *cobra.Command) error { + // api.port ; --api-port + cmd.PersistentFlags().Int("api-port", 3182, "API server port") + if err := viper.BindPFlag("api.port", cmd.PersistentFlags().Lookup("api-port")); err != nil { + return err + } + + // web.port ; --web-port + cmd.PersistentFlags().Int("web-port", 3183, "Webserver port") + if err := viper.BindPFlag("web.port", cmd.PersistentFlags().Lookup("web-port")); err != nil { + return err + } + + return nil +} From 2bdb22d956a5cd5e6e24a19d51c2d682c93272b4 Mon Sep 17 00:00:00 2001 From: Victor Lacasse-Beaudoin Date: Fri, 15 Sep 2023 16:15:07 -0400 Subject: [PATCH 2/3] Appeler config.RegisterFlags dans rootCmd --- cmd/root.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/cmd/root.go b/cmd/root.go index 3332c5c..d73c696 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -5,8 +5,10 @@ package cmd import ( "fmt" + "log" "os" + "git.agecem.com/agecem/bottin-ag/config" "github.com/spf13/cobra" "github.com/spf13/viper" ) @@ -30,6 +32,10 @@ func init() { cobra.OnInitialize(initConfig) rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.bottin-ag.yaml)") + + if err := config.RegisterFlags(rootCmd); err != nil { + log.Fatal(err) + } } // initConfig reads in config file and ENV variables if set. From 3ba12317232c6af4d1b2f61e2a12f5934fe4bf98 Mon Sep 17 00:00:00 2001 From: Victor Lacasse-Beaudoin Date: Fri, 15 Sep 2023 16:15:32 -0400 Subject: [PATCH 3/3] Appeler config.GetConfig dans apiCmd --- cmd/api.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/cmd/api.go b/cmd/api.go index 1cd3f14..e9779d0 100644 --- a/cmd/api.go +++ b/cmd/api.go @@ -5,7 +5,9 @@ package cmd import ( "fmt" + "log" + "git.agecem.com/agecem/bottin-ag/config" "github.com/spf13/cobra" ) @@ -14,7 +16,13 @@ var apiCmd = &cobra.Command{ Use: "api", Short: "Start the API server", Run: func(cmd *cobra.Command, args []string) { - fmt.Println("api called") + cfg, err := config.GetConfig() + if err != nil { + log.Fatal(err) + } + + fmt.Println("api called with api-port", cfg.API.Port) + fmt.Println("api called with web-port", cfg.Web.Port) }, }