Merge branch 'refactor/serpents'
This commit is contained in:
commit
4b42f8d215
4 changed files with 47 additions and 76 deletions
|
@ -31,7 +31,7 @@ func init() {
|
|||
|
||||
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.bottin-ag.yaml)")
|
||||
|
||||
if err := config.RegisterFlags(rootCmd); err != nil {
|
||||
if err := config.RegisterFlags(rootCmd.PersistentFlags()); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
|
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
|
||||
}
|
||||
|
||||
|
|
5
go.mod
5
go.mod
|
@ -1,13 +1,15 @@
|
|||
module git.agecem.com/agecem/bottin-ag
|
||||
|
||||
go 1.21.0
|
||||
go 1.21.1
|
||||
|
||||
require (
|
||||
codeberg.org/vlbeaudoin/serpents v1.0.0
|
||||
codeberg.org/vlbeaudoin/voki v1.3.1
|
||||
git.agecem.com/agecem/bottin/v5 v5.3.0
|
||||
github.com/jmoiron/sqlx v1.3.5
|
||||
github.com/labstack/echo/v4 v4.11.1
|
||||
github.com/spf13/cobra v1.7.0
|
||||
github.com/spf13/pflag v1.0.5
|
||||
github.com/spf13/viper v1.16.0
|
||||
)
|
||||
|
||||
|
@ -27,7 +29,6 @@ require (
|
|||
github.com/spf13/afero v1.9.5 // indirect
|
||||
github.com/spf13/cast v1.5.1 // indirect
|
||||
github.com/spf13/jwalterweatherman v1.1.0 // indirect
|
||||
github.com/spf13/pflag v1.0.5 // indirect
|
||||
github.com/subosito/gotenv v1.4.2 // indirect
|
||||
github.com/valyala/bytebufferpool v1.0.0 // indirect
|
||||
github.com/valyala/fasttemplate v1.2.2 // indirect
|
||||
|
|
2
go.sum
2
go.sum
|
@ -35,6 +35,8 @@ cloud.google.com/go/storage v1.6.0/go.mod h1:N7U0C8pVQ/+NIKOBQyamJIeKQKkZ+mxpohl
|
|||
cloud.google.com/go/storage v1.8.0/go.mod h1:Wv1Oy7z6Yz3DshWRJFhqM/UCfaWIRTdp0RXyy7KQOVs=
|
||||
cloud.google.com/go/storage v1.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9ullr3+Kg0=
|
||||
cloud.google.com/go/storage v1.14.0/go.mod h1:GrKmX003DSIwi9o29oFT7YDnHYwZoctc3fOKtUw0Xmo=
|
||||
codeberg.org/vlbeaudoin/serpents v1.0.0 h1:3mN5QdVojmd3U0ISh/jgSslNOd7By0H3u9enRwgimkE=
|
||||
codeberg.org/vlbeaudoin/serpents v1.0.0/go.mod h1:3bE/R0ToABwcUJtS1VcGEBa86K5FYhrZGAbFl2qL8kQ=
|
||||
codeberg.org/vlbeaudoin/voki v1.3.1 h1:TxJj3qmOys0Pbq1dPKnOEXMXKqQLQqrBYd4QqiWWXcw=
|
||||
codeberg.org/vlbeaudoin/voki v1.3.1/go.mod h1:5XTLx/KiW/OfiupF3o7PAAAU/UhsPdKSrVMmtHbmkPI=
|
||||
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
|
||||
|
|
Reference in a new issue