wip: integration between cmd.go and config.go
This commit is contained in:
parent
b67955ab28
commit
0123d9d37c
3 changed files with 93 additions and 58 deletions
48
cmd.go
48
cmd.go
|
@ -107,6 +107,12 @@ func init() {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// db.sslmode
|
||||||
|
apiCmd.Flags().String(FlagDBSSLMode, DefaultDBSSLMode, DescriptionDBSSLMode)
|
||||||
|
if err := viper.BindPFlag(ViperDBSSLMode, apiCmd.Flags().Lookup(FlagDBSSLMode)); err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
// db.host
|
// db.host
|
||||||
apiCmd.Flags().String(FlagDBHost, DefaultDBHost, DescriptionDBHost)
|
apiCmd.Flags().String(FlagDBHost, DefaultDBHost, DescriptionDBHost)
|
||||||
if err := viper.BindPFlag(ViperDBHost, apiCmd.Flags().Lookup(FlagDBHost)); err != nil {
|
if err := viper.BindPFlag(ViperDBHost, apiCmd.Flags().Lookup(FlagDBHost)); err != nil {
|
||||||
|
@ -208,13 +214,13 @@ var webCmd = &cobra.Command{
|
||||||
Short: "Démarrer le client web",
|
Short: "Démarrer le client web",
|
||||||
Args: cobra.ExactArgs(0),
|
Args: cobra.ExactArgs(0),
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
webApiHost = viper.GetString(viperWebAPIHost)
|
webApiHost = viper.GetString(ViperWebAPIHost)
|
||||||
webApiKey = viper.GetString(viperWebAPIKey)
|
webApiKey = viper.GetString(ViperWebAPIKey)
|
||||||
webApiPort = viper.GetInt(viperWebAPIPort)
|
webApiPort = viper.GetInt(ViperWebAPIPort)
|
||||||
webApiProtocol = viper.GetString(viperWebAPIProtocol)
|
webApiProtocol = viper.GetString(ViperWebAPIProtocol)
|
||||||
webPassword = viper.GetString(viperWebPassword)
|
webPassword = viper.GetString(ViperWebPassword)
|
||||||
webPort = viper.GetInt(viperWebPort)
|
webPort = viper.GetInt(ViperWebPort)
|
||||||
webUser = viper.GetString(viperWebUser)
|
webUser = viper.GetString(ViperWebUser)
|
||||||
|
|
||||||
// Ping API server
|
// Ping API server
|
||||||
/*
|
/*
|
||||||
|
@ -271,44 +277,44 @@ func init() {
|
||||||
//templatesFS = web.GetTemplates()
|
//templatesFS = web.GetTemplates()
|
||||||
|
|
||||||
// web.api.host
|
// web.api.host
|
||||||
webCmd.Flags().String(flagWebAPIHost, defaultWebAPIHost, descriptionWebAPIHost)
|
webCmd.Flags().String(FlagWebAPIHost, DefaultWebAPIHost, DescriptionWebAPIHost)
|
||||||
if err := viper.BindPFlag(viperWebAPIHost, webCmd.Flags().Lookup(flagWebAPIHost)); err != nil {
|
if err := viper.BindPFlag(ViperWebAPIHost, webCmd.Flags().Lookup(FlagWebAPIHost)); err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// web.api.key
|
// web.api.key
|
||||||
webCmd.Flags().String(flagWebAPIKey, defaultWebAPIKey, descriptionWebAPIKey)
|
webCmd.Flags().String(FlagWebAPIKey, DefaultWebAPIKey, DescriptionWebAPIKey)
|
||||||
if err := viper.BindPFlag(viperWebAPIKey, webCmd.Flags().Lookup(flagWebAPIKey)); err != nil {
|
if err := viper.BindPFlag(ViperWebAPIKey, webCmd.Flags().Lookup(FlagWebAPIKey)); err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// web.api.protocol
|
// web.api.protocol
|
||||||
webCmd.Flags().String(flagWebAPIProtocol, defaultWebAPIProtocol, descriptionWebAPIProtocol)
|
webCmd.Flags().String(FlagWebAPIProtocol, DefaultWebAPIProtocol, DescriptionWebAPIProtocol)
|
||||||
if err := viper.BindPFlag(viperWebAPIProtocol, webCmd.Flags().Lookup(flagWebAPIProtocol)); err != nil {
|
if err := viper.BindPFlag(ViperWebAPIProtocol, webCmd.Flags().Lookup(FlagWebAPIProtocol)); err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// web.api.port
|
// web.api.port
|
||||||
webCmd.Flags().Int(flagWebAPIPort, defaultWebAPIPort, descriptionWebAPIPort)
|
webCmd.Flags().Int(FlagWebAPIPort, DefaultWebAPIPort, DescriptionWebAPIPort)
|
||||||
if err := viper.BindPFlag(viperWebAPIPort, webCmd.Flags().Lookup(flagWebAPIPort)); err != nil {
|
if err := viper.BindPFlag(ViperWebAPIPort, webCmd.Flags().Lookup(FlagWebAPIPort)); err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// web.password
|
// web.password
|
||||||
webCmd.Flags().String(flagWebPassword, defaultWebPassword, descriptionWebPassword)
|
webCmd.Flags().String(FlagWebPassword, DefaultWebPassword, DescriptionWebPassword)
|
||||||
if err := viper.BindPFlag(viperWebPassword, webCmd.Flags().Lookup(flagWebPassword)); err != nil {
|
if err := viper.BindPFlag(ViperWebPassword, webCmd.Flags().Lookup(FlagWebPassword)); err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// web.port
|
// web.port
|
||||||
webCmd.Flags().Int(flagWebPort, defaultWebPort, descriptionWebPort)
|
webCmd.Flags().Int(FlagWebPort, DefaultWebPort, DescriptionWebPort)
|
||||||
if err := viper.BindPFlag(viperWebPort, webCmd.Flags().Lookup(flagWebPort)); err != nil {
|
if err := viper.BindPFlag(ViperWebPort, webCmd.Flags().Lookup(FlagWebPort)); err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// web.user
|
// web.user
|
||||||
webCmd.Flags().String(flagWebUser, defaultWebUser, descriptionWebUser)
|
webCmd.Flags().String(FlagWebUser, DefaultWebUser, DescriptionWebUser)
|
||||||
if err := viper.BindPFlag(viperWebUser, webCmd.Flags().Lookup(flagWebUser)); err != nil {
|
if err := viper.BindPFlag(ViperWebUser, webCmd.Flags().Lookup(FlagWebUser)); err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
84
config.go
84
config.go
|
@ -1,5 +1,7 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
|
//TODO move flag declarations here
|
||||||
|
|
||||||
const (
|
const (
|
||||||
ViperAPIPort string = "api.port"
|
ViperAPIPort string = "api.port"
|
||||||
FlagAPIPort string = "api-port"
|
FlagAPIPort string = "api-port"
|
||||||
|
@ -16,6 +18,11 @@ const (
|
||||||
DefaultDBDatabase string = "bottin"
|
DefaultDBDatabase string = "bottin"
|
||||||
DescriptionDBDatabase string = "Postgres database"
|
DescriptionDBDatabase string = "Postgres database"
|
||||||
|
|
||||||
|
ViperDBSSLMode string = "db.sslmode"
|
||||||
|
FlagDBSSLMode string = "db-sslmode"
|
||||||
|
DefaultDBSSLMode string = "prefer"
|
||||||
|
DescriptionDBSSLMode string = "Postgres sslmode"
|
||||||
|
|
||||||
ViperDBHost string = "db.host"
|
ViperDBHost string = "db.host"
|
||||||
FlagDBHost string = "db-host"
|
FlagDBHost string = "db-host"
|
||||||
DefaultDBHost string = "db"
|
DefaultDBHost string = "db"
|
||||||
|
@ -36,40 +43,40 @@ const (
|
||||||
DefaultDBUser string = "bottin"
|
DefaultDBUser string = "bottin"
|
||||||
DescriptionDBUser string = "Postgres user"
|
DescriptionDBUser string = "Postgres user"
|
||||||
|
|
||||||
viperWebUser string = "web.user"
|
ViperWebUser string = "web.user"
|
||||||
flagWebUser string = "web-user"
|
FlagWebUser string = "web-user"
|
||||||
defaultWebUser string = "bottin"
|
DefaultWebUser string = "bottin"
|
||||||
descriptionWebUser string = "Web client basic auth user"
|
DescriptionWebUser string = "Web client basic auth user"
|
||||||
|
|
||||||
viperWebPassword string = "web.password"
|
ViperWebPassword string = "web.password"
|
||||||
flagWebPassword string = "web-password"
|
FlagWebPassword string = "web-password"
|
||||||
defaultWebPassword string = "bottin"
|
DefaultWebPassword string = "bottin"
|
||||||
descriptionWebPassword string = "Web client basic auth password"
|
DescriptionWebPassword string = "Web client basic auth password"
|
||||||
|
|
||||||
viperWebPort string = "web.port"
|
ViperWebPort string = "web.port"
|
||||||
flagWebPort string = "web-port"
|
FlagWebPort string = "web-port"
|
||||||
defaultWebPort int = 2312
|
DefaultWebPort int = 2312
|
||||||
descriptionWebPort string = "Web client port"
|
DescriptionWebPort string = "Web client port"
|
||||||
|
|
||||||
viperWebAPIHost string = "api.host"
|
ViperWebAPIHost string = "api.host"
|
||||||
flagWebAPIHost string = "api-host"
|
FlagWebAPIHost string = "api-host"
|
||||||
defaultWebAPIHost string = "api"
|
DefaultWebAPIHost string = "api"
|
||||||
descriptionWebAPIHost string = "Target API server host"
|
DescriptionWebAPIHost string = "Target API server host"
|
||||||
|
|
||||||
viperWebAPIKey string = "api.key"
|
ViperWebAPIKey string = "api.key"
|
||||||
flagWebAPIKey string = "api-key"
|
FlagWebAPIKey string = "api-key"
|
||||||
defaultWebAPIKey string = "bottin"
|
DefaultWebAPIKey string = "bottin"
|
||||||
descriptionWebAPIKey string = "Target API server key"
|
DescriptionWebAPIKey string = "Target API server key"
|
||||||
|
|
||||||
viperWebAPIPort string = "api.port"
|
ViperWebAPIPort string = "api.port"
|
||||||
flagWebAPIPort string = "api-port"
|
FlagWebAPIPort string = "api-port"
|
||||||
defaultWebAPIPort int = 1312
|
DefaultWebAPIPort int = 1312
|
||||||
descriptionWebAPIPort string = "Target API server port"
|
DescriptionWebAPIPort string = "Target API server port"
|
||||||
|
|
||||||
viperWebAPIProtocol string = "api.protocol"
|
ViperWebAPIProtocol string = "api.protocol"
|
||||||
flagWebAPIProtocol string = "api-protocol"
|
FlagWebAPIProtocol string = "api-protocol"
|
||||||
defaultWebAPIProtocol string = "http"
|
DefaultWebAPIProtocol string = "http"
|
||||||
descriptionWebAPIProtocol string = "Target API server protocol (http/https)"
|
DescriptionWebAPIProtocol string = "Target API server protocol (http/https)"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
|
@ -97,3 +104,24 @@ type Config struct {
|
||||||
} `yaml:"api"`
|
} `yaml:"api"`
|
||||||
} `yaml:"web"`
|
} `yaml:"web"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DefaultConfig returns a Config filled with the default values from the
|
||||||
|
// `Default*` constants defined in this file.
|
||||||
|
func DefaultConfig() (cfg Config) {
|
||||||
|
cfg.API.Port = DefaultAPIPort
|
||||||
|
cfg.API.Key = DefaultAPIKey
|
||||||
|
cfg.DB.Database = DefaultDBDatabase
|
||||||
|
cfg.DB.Host = DefaultDBHost
|
||||||
|
cfg.DB.SSLMode = DefaultDBSSLMode
|
||||||
|
cfg.DB.Password = DefaultDBPassword
|
||||||
|
cfg.DB.Port = DefaultDBPort
|
||||||
|
cfg.DB.User = DefaultDBUser
|
||||||
|
cfg.Web.User = DefaultWebUser
|
||||||
|
cfg.Web.Password = DefaultWebPassword
|
||||||
|
cfg.Web.Port = DefaultWebPort
|
||||||
|
cfg.Web.API.Host = DefaultWebAPIHost
|
||||||
|
cfg.Web.API.Key = DefaultWebAPIKey
|
||||||
|
cfg.Web.API.Port = DefaultWebAPIPort
|
||||||
|
cfg.Web.API.Protocol = DefaultWebAPIProtocol
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
19
db_test.go
19
db_test.go
|
@ -6,12 +6,13 @@ import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/jackc/pgx/v5/pgxpool"
|
"github.com/jackc/pgx/v5/pgxpool"
|
||||||
|
"github.com/spf13/viper"
|
||||||
)
|
)
|
||||||
|
|
||||||
// path to a file containing the db password
|
|
||||||
var passfile string
|
|
||||||
|
|
||||||
func TestDB(t *testing.T) {
|
func TestDB(t *testing.T) {
|
||||||
|
cfg := DefaultConfig()
|
||||||
|
cfg.DB.Password = viper.GetString(ViperDBPassword)
|
||||||
|
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
|
|
||||||
//prep
|
//prep
|
||||||
|
@ -19,12 +20,12 @@ func TestDB(t *testing.T) {
|
||||||
ctx,
|
ctx,
|
||||||
fmt.Sprintf(
|
fmt.Sprintf(
|
||||||
"user=%s password=%s database=%s host=%s port=%d sslmode=%s ",
|
"user=%s password=%s database=%s host=%s port=%d sslmode=%s ",
|
||||||
"bottin",
|
cfg.DB.User,
|
||||||
dbPassword,
|
cfg.DB.Password,
|
||||||
"bottin",
|
cfg.DB.Database,
|
||||||
"postgres.agecem.com",
|
cfg.DB.Host,
|
||||||
5432,
|
cfg.DB.Port,
|
||||||
"require", //TODO change to "require"
|
cfg.DB.SSLMode,
|
||||||
))
|
))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
|
|
Loading…
Reference in a new issue