Victor Lacasse-Beaudoin
ac20221ca3
Ajouter structs Config, APIConfig et WebConfig Ajouter GetConfig pour encapsuler viper.Unmarshal Ajouter RegisterFlags pour déclarer flags cobra et associations viper
40 lines
853 B
Go
40 lines
853 B
Go
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
|
|
}
|