initial commit
This commit is contained in:
109
internal/config/config.go
Normal file
109
internal/config/config.go
Normal file
@@ -0,0 +1,109 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
"reflect"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/hashicorp/logutils"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
// time configuration
|
||||
TimeFormat string `env:"TIME_FORMAT" default:"2006-01-02 15:04:05"`
|
||||
TimeZoneLocal string `env:"TIME_ZONE" default:"America/Chicago"`
|
||||
TZoneLocal *time.Location `ignored:"true"`
|
||||
TZoneUTC *time.Location `ignored:"true"`
|
||||
|
||||
// logging
|
||||
LogLevel int `env:"LOG_LEVEL" default:"0"`
|
||||
Log *logutils.LevelFilter `ignored:"true"`
|
||||
|
||||
// configuration file
|
||||
ConfigFile string `ignored:"true" env:"CONFIG_FILE" required:"false"`
|
||||
ConfigFileExample string `ignored:"true"`
|
||||
ConfigFileData ConfigFileStruct `ignored:"true"`
|
||||
|
||||
// misc
|
||||
VaultUser string `env:"VAULT_USER" required:"false" default:""`
|
||||
VaultPass string `env:"VAULT_PASS" required:"false" default:""`
|
||||
SecretID string `env:"VAULT_SECRET_ID" required:"false" default:""`
|
||||
VaultInstance string `env:"VAULT_INSTANCE" required:"false" default:"dev"`
|
||||
}
|
||||
|
||||
// DefaultConfig initializes the config variable for use with a prepared set of defaults.
|
||||
func DefaultConfig() *Config {
|
||||
home, err := os.UserHomeDir()
|
||||
if err != nil {
|
||||
log.Fatalf("[FATAL] Unable to determine user home directory for config file: %v", err)
|
||||
}
|
||||
|
||||
return &Config{
|
||||
Log: &logutils.LevelFilter{
|
||||
Levels: []logutils.LogLevel{"TRACE", "DEBUG", "INFO", "WARNING", "ERROR"},
|
||||
Writer: os.Stderr,
|
||||
},
|
||||
ConfigFile: home + "/.getvaultpw.yml",
|
||||
ConfigFileExample: `---
|
||||
salt: exampleSalt
|
||||
|
||||
vaultEnvironment:
|
||||
dev:
|
||||
host: vault.dev.example.com
|
||||
user: userName
|
||||
test:
|
||||
host: vault.test.example.com
|
||||
user: userName
|
||||
stage:
|
||||
host: vault.stage.example.com
|
||||
user: userName
|
||||
prod:
|
||||
host: vault.prod.example.com
|
||||
user: userName
|
||||
|
||||
credentials:
|
||||
serviceAccountNumberOne:
|
||||
vaultPath: /secrets/serviceAccountNumberOne/credentials
|
||||
vaultID: password
|
||||
vaultInstance: dev
|
||||
serviceAccountNumberTwo:
|
||||
vaultPath: /secrets/serviceAccountNumberTwo/credentials
|
||||
vaultID: password
|
||||
`,
|
||||
}
|
||||
}
|
||||
|
||||
func (cfg *Config) setLogLevel() {
|
||||
switch {
|
||||
case cfg.LogLevel <= 20:
|
||||
cfg.Log.SetMinLevel(logutils.LogLevel("ERROR"))
|
||||
case cfg.LogLevel > 20 && cfg.LogLevel <= 40:
|
||||
cfg.Log.SetMinLevel(logutils.LogLevel("WARNING"))
|
||||
case cfg.LogLevel > 40 && cfg.LogLevel <= 60:
|
||||
cfg.Log.SetMinLevel(logutils.LogLevel("INFO"))
|
||||
case cfg.LogLevel > 60 && cfg.LogLevel <= 80:
|
||||
cfg.Log.SetMinLevel(logutils.LogLevel("DEBUG"))
|
||||
case cfg.LogLevel > 80:
|
||||
cfg.Log.SetMinLevel(logutils.LogLevel("TRACE"))
|
||||
}
|
||||
log.SetOutput(cfg.Log)
|
||||
}
|
||||
|
||||
func (cfg *Config) printRunningConfig(cfgInfo []StructInfo) {
|
||||
log.Printf("[DEBUG] Current Running Configuration Values:")
|
||||
for _, info := range cfgInfo {
|
||||
switch info.Type.String() {
|
||||
case "string":
|
||||
p := reflect.ValueOf(cfg).Elem().FieldByName(info.Name).Addr().Interface().(*string)
|
||||
log.Printf("[DEBUG]\t%s\t\t= %s\n", info.Alt, *p)
|
||||
case "bool":
|
||||
p := reflect.ValueOf(cfg).Elem().FieldByName(info.Name).Addr().Interface().(*bool)
|
||||
log.Printf("[DEBUG]\t%s\t\t= %s\n", info.Alt, strconv.FormatBool(*p))
|
||||
case "int":
|
||||
p := reflect.ValueOf(cfg).Elem().FieldByName(info.Name).Addr().Interface().(*int)
|
||||
log.Printf("[DEBUG]\t%s\t\t= %s\n", info.Alt, strconv.FormatInt(int64(*p), 10))
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user