bottin-ag/config/config.go

177 lines
3.8 KiB
Go
Raw Permalink Normal View History

/*
Package config provides types and methods for managing this application's
config options and flags
*/
package config
import (
"codeberg.org/vlbeaudoin/serpents"
"github.com/spf13/pflag"
"github.com/spf13/viper"
)
type Config struct {
2023-09-16 18:12:02 -04:00
API APIConfig
Bottin BottinConfig
DB DBConfig
2023-09-16 18:12:02 -04:00
Web WebConfig
}
type APIConfig struct {
Key string
Port int
}
2023-09-16 18:12:02 -04:00
type BottinConfig struct {
API struct {
Host string
Key string
Port int
Protocol string
2023-09-16 18:12:02 -04:00
}
}
type DBConfig struct {
Database string
Host string
Password string
Port int
User string
UseSSL bool `mapstructure:"use_ssl"`
}
type WebConfig struct {
API struct {
Host string
Key string
Port int
Protocol string
}
2023-09-19 16:46:44 -04:00
Password string
Port int
User string
}
func UnmarshalConfig() (cfg Config, err error) {
return cfg, viper.Unmarshal(&cfg)
}
// RegisterFlags adds persistent flags necessary to the application to the specified *cobra.Command
func RegisterFlags(flagSet *pflag.FlagSet) error {
// api.port ; --api-port
if err := serpents.Int(flagSet,
"api.port", "api-port", 3182, "API server port"); err != nil {
return err
}
// api.key ; --api-key
if err := serpents.String(flagSet,
"api.key", "api-key", "bottinag", "API server key"); err != nil {
return err
}
2023-09-16 18:12:02 -04:00
// bottin.api.host
if err := serpents.String(flagSet,
"bottin.api.host", "bottin-api-host", "localhost", "Bottin API server host"); err != nil {
2023-09-16 18:12:02 -04:00
return err
}
// bottin.api.key
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 := serpents.Int(flagSet,
"bottin.api.port", "bottin-api-port", 1312, "Bottin API server port"); err != nil {
return err
}
// bottin.api.protocol
if err := serpents.String(flagSet,
"bottin.api.protocol", "bottin-api-protocol", "http", "Bottin API server protocol"); err != nil {
return err
}
// db.database
if err := serpents.String(flagSet,
"db.database", "db-database", "bottinag", "PostgreSQL database"); err != nil {
return err
}
// db.host
if err := serpents.String(flagSet,
"db.host", "db-host", "db", "PostgreSQL host"); err != nil {
return err
}
// db.password
if err := serpents.String(flagSet,
"db.password", "db-password", "bottinag", "PostgreSQL password"); err != nil {
return err
}
// db.port
if err := serpents.Int(flagSet,
"db.port", "db-port", 5432, "PostgreSQL port"); err != nil {
return err
}
// db.user
if err := serpents.String(flagSet,
"db.user", "db-user", "bottinag", "PostgreSQL user"); err != nil {
return err
}
// db.use_ssl
if err := serpents.Bool(flagSet,
"db.use_ssl", "db-use-ssl", false, "PostgreSQL use_ssl"); err != nil {
return err
}
// web.api.host
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 := serpents.String(flagSet,
"web.api.key", "web-api-key", "bottinag", "Webserver API client key"); err != nil {
return err
}
// web.api.port
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 := serpents.String(flagSet,
"web.api.protocol", "web-api-protocol", "http", "Webserver API client protocol"); err != nil {
return err
}
2023-09-19 16:46:44 -04:00
// web.password ; --web-password
if err := serpents.String(flagSet,
"web.password", "web-password", "bottinag", "Webserver basic auth password"); err != nil {
2023-09-19 16:46:44 -04:00
return err
}
// web.port ; --web-port
if err := serpents.Int(flagSet,
"web.port", "web-port", 3183, "Webserver port"); err != nil {
return err
}
2023-09-19 16:46:44 -04:00
// web.user ; --web-user
if err := serpents.String(flagSet,
"web.user", "web-user", "bottinag", "Webserver basic auth username"); err != nil {
2023-09-19 16:46:44 -04:00
return err
}
return nil
}