Ajouter et implémenter db flags
Ajouter config.RegisterBool
This commit is contained in:
parent
4bbc91878f
commit
7e3531aa90
2 changed files with 58 additions and 1 deletions
|
@ -34,7 +34,7 @@ var apiCmd = &cobra.Command{
|
|||
|
||||
bottinApiClient := bottindata.NewApiClient(cfg.Bottin.API.Key, cfg.Bottin.API.Host, cfg.Bottin.API.Protocol, cfg.Bottin.API.Port)
|
||||
|
||||
dbClient, err := dbclient.New("db", "bottinag", "bottinag", "bottinag", 5432, false)
|
||||
dbClient, err := dbclient.New(cfg.DB.Host, cfg.DB.Database, cfg.DB.User, cfg.DB.Password, cfg.DB.Port, cfg.DB.UseSSL)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
|
|
@ -12,6 +12,7 @@ import (
|
|||
type Config struct {
|
||||
API APIConfig
|
||||
Bottin BottinConfig
|
||||
DB DBConfig
|
||||
Web WebConfig
|
||||
}
|
||||
|
||||
|
@ -28,6 +29,15 @@ type BottinConfig struct {
|
|||
}
|
||||
}
|
||||
|
||||
type DBConfig struct {
|
||||
Database string
|
||||
Host string
|
||||
Password string
|
||||
Port int
|
||||
User string
|
||||
UseSSL bool `mapstructure:"use_ssl"`
|
||||
}
|
||||
|
||||
type WebConfig struct {
|
||||
Port int
|
||||
}
|
||||
|
@ -36,6 +46,17 @@ func UnmarshalConfig() (cfg Config, err error) {
|
|||
return cfg, viper.Unmarshal(&cfg)
|
||||
}
|
||||
|
||||
// RegisterBool registers a new cobra bool flag and associated viper config option
|
||||
func RegisterBool(cmd *cobra.Command, isPersistent bool, viperName, cobraName, cobraDescription string, cobraDefault bool) error {
|
||||
if isPersistent {
|
||||
cmd.PersistentFlags().Bool(cobraName, cobraDefault, cobraDescription)
|
||||
return viper.BindPFlag(viperName, cmd.PersistentFlags().Lookup(cobraName))
|
||||
} else {
|
||||
cmd.Flags().Bool(cobraName, cobraDefault, cobraDescription)
|
||||
return viper.BindPFlag(viperName, cmd.Flags().Lookup(cobraName))
|
||||
}
|
||||
}
|
||||
|
||||
// RegisterInt registers a new cobra int flag and associated viper config option
|
||||
func RegisterInt(cmd *cobra.Command, isPersistent bool, viperName, cobraName, cobraDescription string, cobraDefault int) error {
|
||||
if isPersistent {
|
||||
|
@ -90,6 +111,42 @@ func RegisterFlags(cmd *cobra.Command) error {
|
|||
return err
|
||||
}
|
||||
|
||||
// db.database
|
||||
if err := RegisterString(cmd, true,
|
||||
"db.database", "db-database", "PostgreSQL database", "bottinag"); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// db.host
|
||||
if err := RegisterString(cmd, true,
|
||||
"db.host", "db-host", "PostgreSQL host", "db"); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// db.password
|
||||
if err := RegisterString(cmd, true,
|
||||
"db.password", "db-password", "PostgreSQL password", "bottinag"); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// db.port
|
||||
if err := RegisterInt(cmd, true,
|
||||
"db.port", "db-port", "PostgreSQL port", 5432); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// db.user
|
||||
if err := RegisterString(cmd, true,
|
||||
"db.user", "db-user", "PostgreSQL user", "bottinag"); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// db.use_ssl
|
||||
if err := RegisterBool(cmd, true,
|
||||
"db.use_ssl", "db-use-ssl", "PostgreSQL use_ssl", false); 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 {
|
||||
|
|
Loading…
Reference in a new issue