From ac20221ca35f69b67e85b646b369fb72e7296441 Mon Sep 17 00:00:00 2001 From: Victor Lacasse-Beaudoin Date: Fri, 15 Sep 2023 16:13:05 -0400 Subject: [PATCH] =?UTF-8?q?Ajouter=20d=C3=A9finitions=20de=20base=20=C3=A0?= =?UTF-8?q?=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 +}