Merge branch 'feature/db-flags' into main

This commit is contained in:
Victor Lacasse-Beaudoin 2023-09-16 18:40:49 -04:00
commit a98d11f4b4
2 changed files with 58 additions and 1 deletions

View file

@ -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) 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 { if err != nil {
log.Fatal(err) log.Fatal(err)
} }

View file

@ -12,6 +12,7 @@ import (
type Config struct { type Config struct {
API APIConfig API APIConfig
Bottin BottinConfig Bottin BottinConfig
DB DBConfig
Web WebConfig 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 { type WebConfig struct {
Port int Port int
} }
@ -36,6 +46,17 @@ func UnmarshalConfig() (cfg Config, err error) {
return cfg, viper.Unmarshal(&cfg) 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 // 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 { func RegisterInt(cmd *cobra.Command, isPersistent bool, viperName, cobraName, cobraDescription string, cobraDefault int) error {
if isPersistent { if isPersistent {
@ -90,6 +111,42 @@ func RegisterFlags(cmd *cobra.Command) error {
return err 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 // web.port ; --web-port
cmd.PersistentFlags().Int("web-port", 3183, "Webserver port") cmd.PersistentFlags().Int("web-port", 3183, "Webserver port")
if err := viper.BindPFlag("web.port", cmd.PersistentFlags().Lookup("web-port")); err != nil { if err := viper.BindPFlag("web.port", cmd.PersistentFlags().Lookup("web-port")); err != nil {