golang-base/internal/config/initialize.go

42 lines
781 B
Go

package config
import (
"log"
"os"
"time"
l "example.com/golang-base/internal/log"
)
func Init() Config {
cfg := New()
// parse config structure
cfgInfo, err := getStructInfo(&cfg)
if err != nil {
log.Fatalf("Unable to initialize program: %v", err)
}
// get command line flags
if err := cfg.parseFlags(cfgInfo); err != nil {
log.Fatalf("Unable to initialize program: %v", err)
}
// set logging Level
l.SetNumericLevel(cfg.LogLevel)
// set timezone & time format
cfg.TZUTC, _ = time.LoadLocation("UTC")
cfg.TZLocal, err = time.LoadLocation(cfg.TimeZoneLocal)
if err != nil {
l.L.Error("Unable to parse timezone string", "error", err)
os.Exit(1)
}
// print running config
printRunningConfig(&cfg, cfgInfo)
// return configuration
return cfg
}