initial commit
This commit is contained in:
77
internal/config/config.go
Normal file
77
internal/config/config.go
Normal file
@@ -0,0 +1,77 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"git.haelnorr.com/h/golib/ezconf"
|
||||
"git.haelnorr.com/h/golib/hlog"
|
||||
"git.haelnorr.com/h/golib/hws"
|
||||
"git.haelnorr.com/h/golib/hwsauth"
|
||||
"git.haelnorr.com/h/oslstats/internal/db"
|
||||
"github.com/joho/godotenv"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
DB *db.Config
|
||||
HWS *hws.Config
|
||||
HWSAuth *hwsauth.Config
|
||||
HLOG *hlog.Config
|
||||
Flags *Flags
|
||||
}
|
||||
|
||||
// Load the application configuration and get a pointer to the Config object
|
||||
// If doconly is specified, only the loader will be returned
|
||||
func GetConfig(flags *Flags) (*Config, *ezconf.ConfigLoader, error) {
|
||||
err := godotenv.Load(flags.EnvFile)
|
||||
if err != nil && flags.GenEnv == "" && !flags.EnvDoc {
|
||||
return nil, nil, errors.Wrap(err, "gotgodotenv.Load")
|
||||
}
|
||||
|
||||
loader := ezconf.New()
|
||||
loader.RegisterIntegrations(
|
||||
hlog.NewEZConfIntegration(),
|
||||
hws.NewEZConfIntegration(),
|
||||
hwsauth.NewEZConfIntegration(),
|
||||
db.NewEZConfIntegration(),
|
||||
)
|
||||
if err := loader.ParseEnvVars(); err != nil {
|
||||
return nil, nil, errors.Wrap(err, "loader.ParseEnvVars")
|
||||
}
|
||||
|
||||
if flags.GenEnv != "" || flags.EnvDoc {
|
||||
return nil, loader, nil
|
||||
}
|
||||
|
||||
if err := loader.LoadConfigs(); err != nil {
|
||||
return nil, nil, errors.Wrap(err, "loader.LoadConfigs")
|
||||
}
|
||||
|
||||
hwscfg, ok := loader.GetConfig("hws")
|
||||
if !ok {
|
||||
return nil, nil, errors.New("HWS Config not loaded")
|
||||
}
|
||||
|
||||
hwsauthcfg, ok := loader.GetConfig("hwsauth")
|
||||
if !ok {
|
||||
return nil, nil, errors.New("HWSAuth Config not loaded")
|
||||
}
|
||||
|
||||
hlogcfg, ok := loader.GetConfig("hlog")
|
||||
if !ok {
|
||||
return nil, nil, errors.New("HLog Config not loaded")
|
||||
}
|
||||
|
||||
dbcfg, ok := loader.GetConfig("db")
|
||||
if !ok {
|
||||
return nil, nil, errors.New("DB Config not loaded")
|
||||
}
|
||||
|
||||
config := &Config{
|
||||
DB: dbcfg.(*db.Config),
|
||||
HWS: hwscfg.(*hws.Config),
|
||||
HWSAuth: hwsauthcfg.(*hwsauth.Config),
|
||||
HLOG: hlogcfg.(*hlog.Config),
|
||||
Flags: flags,
|
||||
}
|
||||
|
||||
return config, loader, nil
|
||||
}
|
||||
32
internal/config/flags.go
Normal file
32
internal/config/flags.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"flag"
|
||||
)
|
||||
|
||||
type Flags struct {
|
||||
ResetDB bool
|
||||
EnvDoc bool
|
||||
ShowEnv bool
|
||||
GenEnv string
|
||||
EnvFile string
|
||||
}
|
||||
|
||||
func SetupFlags() *Flags {
|
||||
// Parse commandline args
|
||||
resetDB := flag.Bool("resetdb", false, "Reset all the database tables with the updated models")
|
||||
envDoc := flag.Bool("envdoc", false, "Print all environment variables and their documentation")
|
||||
showEnv := flag.Bool("showenv", false, "Print all environment variable values and their documentation")
|
||||
genEnv := flag.String("genenv", "", "Generate a .env file with all environment variables (specify filename)")
|
||||
envfile := flag.String("envfile", ".env", "Specify a .env file to use for the configuration")
|
||||
flag.Parse()
|
||||
|
||||
flags := &Flags{
|
||||
ResetDB: *resetDB,
|
||||
EnvDoc: *envDoc,
|
||||
ShowEnv: *showEnv,
|
||||
GenEnv: *genEnv,
|
||||
EnvFile: *envfile,
|
||||
}
|
||||
return flags
|
||||
}
|
||||
Reference in New Issue
Block a user