2023-09-15 17:10:57 -04:00
|
|
|
/*
|
|
|
|
Package config provides types and methods for managing this application's
|
|
|
|
config options and flags
|
|
|
|
*/
|
2023-09-15 16:13:05 -04:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Config struct {
|
2023-09-16 18:12:02 -04:00
|
|
|
API APIConfig
|
|
|
|
Bottin BottinConfig
|
2023-09-16 18:40:18 -04:00
|
|
|
DB DBConfig
|
2023-09-16 18:12:02 -04:00
|
|
|
Web WebConfig
|
2023-09-15 16:13:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type APIConfig struct {
|
2023-09-17 16:32:40 -04:00
|
|
|
Key string
|
2023-09-15 16:13:05 -04:00
|
|
|
Port int
|
|
|
|
}
|
|
|
|
|
2023-09-16 18:12:02 -04:00
|
|
|
type BottinConfig struct {
|
|
|
|
API struct {
|
2023-09-16 18:21:39 -04:00
|
|
|
Host string
|
|
|
|
Key string
|
|
|
|
Port int
|
|
|
|
Protocol string
|
2023-09-16 18:12:02 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-16 18:40:18 -04:00
|
|
|
type DBConfig struct {
|
|
|
|
Database string
|
|
|
|
Host string
|
|
|
|
Password string
|
|
|
|
Port int
|
|
|
|
User string
|
|
|
|
UseSSL bool `mapstructure:"use_ssl"`
|
|
|
|
}
|
|
|
|
|
2023-09-15 16:13:05 -04:00
|
|
|
type WebConfig struct {
|
2023-09-17 16:32:40 -04:00
|
|
|
API struct {
|
|
|
|
Host string
|
|
|
|
Key string
|
|
|
|
Port int
|
|
|
|
Protocol string
|
|
|
|
}
|
2023-09-19 16:46:44 -04:00
|
|
|
Password string
|
|
|
|
Port int
|
|
|
|
User string
|
2023-09-15 16:13:05 -04:00
|
|
|
}
|
|
|
|
|
2023-09-15 17:10:57 -04:00
|
|
|
func UnmarshalConfig() (cfg Config, err error) {
|
2023-09-15 16:13:05 -04:00
|
|
|
return cfg, viper.Unmarshal(&cfg)
|
|
|
|
}
|
|
|
|
|
2023-09-16 18:40:18 -04:00
|
|
|
// 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))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-16 18:10:14 -04:00
|
|
|
// 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))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-15 16:13:05 -04:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2023-09-17 16:32:40 -04:00
|
|
|
// api.key ; --api-key
|
|
|
|
if err := RegisterString(cmd, true,
|
|
|
|
"api.key", "api-key", "API server key", "bottinag"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-09-16 18:12:02 -04:00
|
|
|
// bottin.api.host
|
|
|
|
if err := RegisterString(cmd, true,
|
|
|
|
"bottin.api.host", "bottin-api-host", "Bottin API server host", "localhost"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-09-16 18:18:19 -04:00
|
|
|
// bottin.api.key
|
|
|
|
if err := RegisterString(cmd, true,
|
|
|
|
"bottin.api.key", "bottin-api-key", "Bottin API server key", "bottin"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-09-16 18:21:39 -04:00
|
|
|
// bottin.api.port
|
|
|
|
if err := RegisterInt(cmd, true,
|
|
|
|
"bottin.api.port", "bottin-api-port", "Bottin API server port", 1312); 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 {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-09-16 18:40:18 -04:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2023-09-17 16:32:40 -04:00
|
|
|
// web.api.host
|
|
|
|
if err := RegisterString(cmd, true,
|
|
|
|
"web.api.host", "web-api-host", "Webserver API client host", "localhost"); 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 {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// web.api.port
|
|
|
|
if err := RegisterInt(cmd, true,
|
|
|
|
"web.api.port", "web-api-port", "Webserver API client port", 3182); 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 {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-09-19 16:46:44 -04:00
|
|
|
// web.password ; --web-password
|
|
|
|
if err := RegisterString(cmd, true,
|
|
|
|
"web.password", "web-password", "Webserver basic auth password", "bottinag"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-09-15 16:13:05 -04:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2023-09-19 16:46:44 -04:00
|
|
|
// web.user ; --web-user
|
|
|
|
if err := RegisterString(cmd, true,
|
|
|
|
"web.user", "web-user", "Webserver basic auth username", "bottinag"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-09-15 16:13:05 -04:00
|
|
|
return nil
|
|
|
|
}
|