Ajouter définitions de base à config/
Ajouter structs Config, APIConfig et WebConfig Ajouter GetConfig pour encapsuler viper.Unmarshal Ajouter RegisterFlags pour déclarer flags cobra et associations viper
This commit is contained in:
parent
499bbc55dc
commit
ac20221ca3
1 changed files with 40 additions and 0 deletions
|
@ -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
|
||||
}
|
Reference in a new issue