Migrate config to codeberg.org/vlbeaudoin/serpents v1.0.0
Raw cobra+viper flags that were not yet migrated to Register* functions now migrated to serpents as well
This commit is contained in:
parent
75b0819ae4
commit
679b594c80
4 changed files with 47 additions and 76 deletions
114
config/config.go
114
config/config.go
|
@ -5,7 +5,8 @@ config options and flags
|
|||
package config
|
||||
|
||||
import (
|
||||
"github.com/spf13/cobra"
|
||||
"codeberg.org/vlbeaudoin/serpents"
|
||||
"github.com/spf13/pflag"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
|
@ -55,152 +56,119 @@ 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 {
|
||||
cmd.PersistentFlags().Int(cobraName, cobraDefault, cobraDescription)
|
||||
return viper.BindPFlag(viperName, cmd.PersistentFlags().Lookup(cobraName))
|
||||
} else {
|
||||
cmd.Flags().Int(cobraName, cobraDefault, cobraDescription)
|
||||
return viper.BindPFlag(viperName, cmd.Flags().Lookup(cobraName))
|
||||
}
|
||||
}
|
||||
|
||||
// RegisterString registers a new cobra string flag and associated viper config option
|
||||
func RegisterString(cmd *cobra.Command, isPersistent bool, viperName, cobraName, cobraDescription string, cobraDefault string) error {
|
||||
if isPersistent {
|
||||
cmd.PersistentFlags().String(cobraName, cobraDefault, cobraDescription)
|
||||
return viper.BindPFlag(viperName, cmd.PersistentFlags().Lookup(cobraName))
|
||||
} else {
|
||||
cmd.Flags().String(cobraName, cobraDefault, cobraDescription)
|
||||
return viper.BindPFlag(viperName, cmd.Flags().Lookup(cobraName))
|
||||
}
|
||||
}
|
||||
|
||||
// RegisterFlags adds persistent flags necessary to the application to the specified *cobra.Command
|
||||
func RegisterFlags(cmd *cobra.Command) error {
|
||||
func RegisterFlags(flagSet *pflag.FlagSet) 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 {
|
||||
if err := serpents.Int(flagSet,
|
||||
"api.port", "api-port", 3182, "API server port"); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// api.key ; --api-key
|
||||
if err := RegisterString(cmd, true,
|
||||
"api.key", "api-key", "API server key", "bottinag"); err != nil {
|
||||
if err := serpents.String(flagSet,
|
||||
"api.key", "api-key", "bottinag", "API server key"); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// bottin.api.host
|
||||
if err := RegisterString(cmd, true,
|
||||
"bottin.api.host", "bottin-api-host", "Bottin API server host", "localhost"); err != nil {
|
||||
if err := serpents.String(flagSet,
|
||||
"bottin.api.host", "bottin-api-host", "localhost", "Bottin API server host"); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// bottin.api.key
|
||||
if err := RegisterString(cmd, true,
|
||||
"bottin.api.key", "bottin-api-key", "Bottin API server key", "bottin"); err != nil {
|
||||
if err := serpents.String(flagSet,
|
||||
"bottin.api.key", "bottin-api-key", "bottin", "Bottin API server key"); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// bottin.api.port
|
||||
if err := RegisterInt(cmd, true,
|
||||
"bottin.api.port", "bottin-api-port", "Bottin API server port", 1312); err != nil {
|
||||
if err := serpents.Int(flagSet,
|
||||
"bottin.api.port", "bottin-api-port", 1312, "Bottin API server port"); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// bottin.api.protocol
|
||||
if err := RegisterString(cmd, true,
|
||||
"bottin.api.protocol", "bottin-api-protocol", "Bottin API server protocol", "http"); err != nil {
|
||||
if err := serpents.String(flagSet,
|
||||
"bottin.api.protocol", "bottin-api-protocol", "http", "Bottin API server protocol"); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// db.database
|
||||
if err := RegisterString(cmd, true,
|
||||
"db.database", "db-database", "PostgreSQL database", "bottinag"); err != nil {
|
||||
if err := serpents.String(flagSet,
|
||||
"db.database", "db-database", "bottinag", "PostgreSQL database"); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// db.host
|
||||
if err := RegisterString(cmd, true,
|
||||
"db.host", "db-host", "PostgreSQL host", "db"); err != nil {
|
||||
if err := serpents.String(flagSet,
|
||||
"db.host", "db-host", "db", "PostgreSQL host"); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// db.password
|
||||
if err := RegisterString(cmd, true,
|
||||
"db.password", "db-password", "PostgreSQL password", "bottinag"); err != nil {
|
||||
if err := serpents.String(flagSet,
|
||||
"db.password", "db-password", "bottinag", "PostgreSQL password"); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// db.port
|
||||
if err := RegisterInt(cmd, true,
|
||||
"db.port", "db-port", "PostgreSQL port", 5432); err != nil {
|
||||
if err := serpents.Int(flagSet,
|
||||
"db.port", "db-port", 5432, "PostgreSQL port"); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// db.user
|
||||
if err := RegisterString(cmd, true,
|
||||
"db.user", "db-user", "PostgreSQL user", "bottinag"); err != nil {
|
||||
if err := serpents.String(flagSet,
|
||||
"db.user", "db-user", "bottinag", "PostgreSQL user"); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// db.use_ssl
|
||||
if err := RegisterBool(cmd, true,
|
||||
"db.use_ssl", "db-use-ssl", "PostgreSQL use_ssl", false); err != nil {
|
||||
if err := serpents.Bool(flagSet,
|
||||
"db.use_ssl", "db-use-ssl", false, "PostgreSQL use_ssl"); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// web.api.host
|
||||
if err := RegisterString(cmd, true,
|
||||
"web.api.host", "web-api-host", "Webserver API client host", "localhost"); err != nil {
|
||||
if err := serpents.String(flagSet,
|
||||
"web.api.host", "web-api-host", "localhost", "Webserver API client host"); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// web.api.key
|
||||
if err := RegisterString(cmd, true,
|
||||
"web.api.key", "web-api-key", "Webserver API client key", "bottinag"); err != nil {
|
||||
if err := serpents.String(flagSet,
|
||||
"web.api.key", "web-api-key", "bottinag", "Webserver API client key"); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// web.api.port
|
||||
if err := RegisterInt(cmd, true,
|
||||
"web.api.port", "web-api-port", "Webserver API client port", 3182); err != nil {
|
||||
if err := serpents.Int(flagSet,
|
||||
"web.api.port", "web-api-port", 3182, "Webserver API client port"); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// web.api.protocol
|
||||
if err := RegisterString(cmd, true,
|
||||
"web.api.protocol", "web-api-protocol", "Webserver API client protocol", "http"); err != nil {
|
||||
if err := serpents.String(flagSet,
|
||||
"web.api.protocol", "web-api-protocol", "http", "Webserver API client protocol"); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// web.password ; --web-password
|
||||
if err := RegisterString(cmd, true,
|
||||
"web.password", "web-password", "Webserver basic auth password", "bottinag"); err != nil {
|
||||
if err := serpents.String(flagSet,
|
||||
"web.password", "web-password", "bottinag", "Webserver basic auth password"); 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 {
|
||||
if err := serpents.Int(flagSet,
|
||||
"web.port", "web-port", 3183, "Webserver port"); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// web.user ; --web-user
|
||||
if err := RegisterString(cmd, true,
|
||||
"web.user", "web-user", "Webserver basic auth username", "bottinag"); err != nil {
|
||||
if err := serpents.String(flagSet,
|
||||
"web.user", "web-user", "bottinag", "Webserver basic auth username"); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
|
Reference in a new issue