Skip to content

Commit

Permalink
config: Rework the config code
Browse files Browse the repository at this point in the history
The existing config code uses a lot of workarounds for viper's issues with
the HCL format.  With the move TOML format for the config files, a lot of
this code can be eliminated by using standard viper calls.

Rework the config code to remove workarounds and reorganize the config
function.

Suggested-by: Zaq? Wiedmann <[email protected]>
Signed-off-by: Prarit Bhargava <[email protected]>
  • Loading branch information
prarit committed Sep 4, 2020
1 parent 44d164e commit 2c6e0d8
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 81 deletions.
2 changes: 0 additions & 2 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,11 @@ func ConvertHCLtoTOML(oldpath string, newpath string, file string) {

_, err := os.Stat(oldconfig)
if os.IsNotExist(err) {
fmt.Println("oldfile not found", oldconfig)
return
}

_, err = os.Stat(newconfig)
if err == nil {
fmt.Println("newfile found", newconfig)
return
}

Expand Down
108 changes: 29 additions & 79 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,32 @@ import (
var version = "master"

func loadConfig() (string, string, string, bool) {

// Attempt to auto-configure for GitLab CI.
// Always do this before reading in the config file o/w CI will end up
// with the wrong data.
host, user, token := config.CI()
if host != "" && user != "" && token != "" {
return host, user, token, false
}

// Try to find XDG_CONFIG_HOME which is declared in XDG base directory
// specification and use it's location as the config directory
home, err := os.UserHomeDir()
if err != nil {
log.Fatal(err)
}

// Try XDG_CONFIG_HOME which is declared in XDG base directory specification
confpath := os.Getenv("XDG_CONFIG_HOME")
if confpath == "" {
confpath = path.Join(home, ".config")
}
labconfpath := confpath+"/lab"
if _, err := os.Stat(labconfpath); os.IsNotExist(err) {
os.MkdirAll(labconfpath, 0700)
if _, err := os.Stat(confpath); os.IsNotExist(err) {
os.MkdirAll(confpath, 0700)
}

// convert old hcl files to toml format

// Convert old hcl files to toml format.
// NO NEW FILES SHOULD BE ADDED BELOW.
config.ConvertHCLtoTOML(".", ".", "lab")
config.ConvertHCLtoTOML(confpath, labconfpath, "lab")
var labgitDir string
Expand All @@ -60,92 +69,33 @@ func loadConfig() (string, string, string, bool) {
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
viper.AutomaticEnv()

var tlsSkipVerify bool
tlsSkipVerify = viper.GetBool("tls.skip_verify")

host, user, token := viper.GetString("core.host"), viper.GetString("core.user"), viper.GetString("core.token")
if host != "" && user != "" && token != "" {
return host, user, token, tlsSkipVerify
} else if host != "" && token != "" {
user = getUser(host, token, tlsSkipVerify)
return host, user, token, tlsSkipVerify
}

// Attempt to auto-configure for GitLab CI
host, user, token = config.CI()
if host != "" && user != "" && token != "" {
return host, user, token, tlsSkipVerify
}

if _, ok := viper.ReadInConfig().(viper.ConfigFileNotFoundError); ok {
if err := config.New(path.Join(labconfpath, "lab.toml"), os.Stdin); err != nil {
err := config.New(path.Join(labconfpath, "lab.toml"), os.Stdin)
if err != nil {
log.Fatal(err)
}

if err := viper.ReadInConfig(); err != nil {
err = viper.ReadInConfig()
if err != nil {
log.Fatal(err)
}
}

c := viper.AllSettings()
var cfg map[string]interface{}
switch v := c["core"].(type) {
// Most run this is the type
case []map[string]interface{}:
cfg = v[0]
// On the first run when the cfg is created it comes in as this type
// for whatever reason
case map[string]interface{}:
cfg = v
}
host = viper.GetString("core.host")
user = viper.GetString("core.user")
token = viper.GetString("core.token")
tlsSkipVerify := viper.GetBool("tls.skip_verify")

for _, v := range []string{"host", "token"} {
if cv, ok := cfg[v]; !ok {
log.Println(cv)
log.Fatalf("missing config value core.%s in %s", v, viper.ConfigFileUsed())
}
if host != "" && user != "" && token != "" {
return host, user, token, tlsSkipVerify
}

var tls map[string]interface{}
switch v := c["tls"].(type) {
// Most run this is the type
case []map[string]interface{}:
tls = v[0]
// On the first run when the cfg is created it comes in as this type
// for whatever reason
case map[string]interface{}:
tls = v
}
if v, ok := tls["skip_verify"]; ok {
tlsSkipVerify = v.(bool)
user = getUser(host, token, tlsSkipVerify)
if strings.TrimSpace(os.Getenv("LAB_CORE_TOKEN")) == "" && strings.TrimSpace(os.Getenv("LAB_CORE_HOST")) == "" {
viper.Set("core.user", user)
viper.WriteConfig()
}

// Set environment overrides
// Note: the code below that uses `cfg["host"]` to access these values
// is tough to simplify since cfg["host"] is accessing the array "core"
// and viper.GetString("core.host") is expecting a non-array so it
// doens't match
if v := viper.GetString("core.host"); v != "" {
cfg["host"] = v
}
if v := viper.GetString("core.token"); v != "" {
cfg["token"] = v
}
if v := viper.GetString("core.user"); v != "" {
cfg["user"] = v
}
if v := viper.Get("tls.skip_verify"); v != nil {
tlsSkipVerify = v.(string) == "true"
}
host = cfg["host"].(string)
token = cfg["token"].(string)
if v, ok := cfg["user"]; ok {
user = v.(string)
}
if user == "" {
user = getUser(host, token, tlsSkipVerify)
}
viper.Set("core.user", user)
return host, user, token, tlsSkipVerify
}

Expand Down

0 comments on commit 2c6e0d8

Please sign in to comment.