collapses config into one library

This commit is contained in:
2022-10-11 08:11:29 -05:00
parent 7468a6dcb4
commit cd14bc11fa
4 changed files with 20 additions and 25 deletions

View File

@@ -3,7 +3,6 @@ package config
import (
"fmt"
"log"
"mutating-webhook/internal/envconfig"
"os"
"reflect"
"strconv"
@@ -31,6 +30,7 @@ type Config struct {
WebServerIdleTimeout int `env:"WEBSERVER_IDLE_TIMEOUT" default:"2"`
}
// DefaultConfig initializes the config variable for use with a prepared set of defaults.
func DefaultConfig() *Config {
return &Config{
Log: &logutils.LevelFilter{
@@ -40,7 +40,7 @@ func DefaultConfig() *Config {
}
}
func (cfg *Config) Validate() error {
func (cfg *Config) validate() error {
checks := []struct {
bad bool
errMsg string
@@ -63,7 +63,7 @@ func (cfg *Config) Validate() error {
return nil
}
func (cfg *Config) SetLogLevel() {
func (cfg *Config) setLogLevel() {
switch {
case cfg.LogLevel <= 20:
cfg.Log.SetMinLevel(logutils.LogLevel("ERROR"))
@@ -79,7 +79,7 @@ func (cfg *Config) SetLogLevel() {
log.SetOutput(cfg.Log)
}
func (cfg *Config) PrintRunningConfig(cfgInfo []envconfig.StructInfo) {
func (cfg *Config) printRunningConfig(cfgInfo []StructInfo) {
log.Printf("[DEBUG] Current Running Configuration Values:")
for _, info := range cfgInfo {
switch info.Type.String() {

View File

@@ -1,4 +1,4 @@
package envconfig
package config
import (
"fmt"
@@ -7,12 +7,6 @@ import (
"strings"
)
/*
ignored:"true"
env:"ENVIRONMENT_VARIABLE"
default:"default value"
*/
type StructInfo struct {
Name string
Alt string
@@ -23,7 +17,7 @@ type StructInfo struct {
DefaultValue interface{}
}
func GetStructInfo(spec interface{}) ([]StructInfo, error) {
func getStructInfo(spec interface{}) ([]StructInfo, error) {
s := reflect.ValueOf(spec)
if s.Kind() != reflect.Pointer {

View File

@@ -1,4 +1,4 @@
package initialize
package config
import (
"flag"
@@ -7,9 +7,6 @@ import (
"reflect"
"strconv"
"time"
"mutating-webhook/internal/config"
"mutating-webhook/internal/envconfig"
)
// getEnvString returns string from environment variable
@@ -59,10 +56,13 @@ func getEnvBool(env string, def bool) bool {
return retVal
}
func Init() *config.Config {
cfg := config.DefaultConfig()
// Init initializes the application configuration by reading default values from the struct's tags
// and environment variables. Tags processed by this process are as follows:
// `ignored:"true" env:"ENVIRONMENT_VARIABLE" default:"default value"`
func Init() *Config {
cfg := DefaultConfig()
cfgInfo, err := envconfig.GetStructInfo(cfg)
cfgInfo, err := getStructInfo(cfg)
if err != nil {
log.Fatalf("[FATAL] %v", err)
}
@@ -98,10 +98,11 @@ func Init() *config.Config {
flag.Parse()
// set logging level
cfg.SetLogLevel()
cfg.setLogLevel()
//
if err = cfg.Validate(); err != nil {
// validate some required values are defined.
// need to break this out to a required:"true" struct tag
if err = cfg.validate(); err != nil {
log.Fatalf("[FATAL] %v", err)
}
@@ -117,7 +118,7 @@ func Init() *config.Config {
time.Now().Format(cfg.TimeFormat)
// print running config
cfg.PrintRunningConfig(cfgInfo)
cfg.printRunningConfig(cfgInfo)
log.Println("[INFO] initialization complete")
return cfg