-
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #324 from roots/cli-config-refactor
CLI config refactor and improvements
- Loading branch information
Showing
17 changed files
with
477 additions
and
140 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package app_paths | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
"runtime" | ||
) | ||
|
||
const ( | ||
appData = "AppData" | ||
trellisConfigDir = "TRELLIS_CONFIG_DIR" | ||
localAppData = "LocalAppData" | ||
xdgCacheHome = "XDG_CACHE_HOME" | ||
xdgConfigHome = "XDG_CONFIG_HOME" | ||
xdgDataHome = "XDG_DATA_HOME" | ||
) | ||
|
||
// Config path precedence: TRELLIS_CONFIG_DIR, XDG_CONFIG_HOME, AppData (windows only), HOME. | ||
func ConfigDir() string { | ||
var path string | ||
|
||
if a := os.Getenv(trellisConfigDir); a != "" { | ||
path = a | ||
} else if b := os.Getenv(xdgConfigHome); b != "" { | ||
path = filepath.Join(b, "trellis") | ||
} else if c := os.Getenv(appData); runtime.GOOS == "windows" && c != "" { | ||
path = filepath.Join(c, "Trellis CLI") | ||
} else { | ||
d, _ := os.UserHomeDir() | ||
path = filepath.Join(d, ".config", "trellis") | ||
} | ||
|
||
return path | ||
} | ||
|
||
func ConfigPath(path string) string { | ||
return filepath.Join(ConfigDir(), path) | ||
} | ||
|
||
// Cache path precedence: XDG_CACHE_HOME, LocalAppData (windows only), HOME. | ||
func CacheDir() string { | ||
var path string | ||
if a := os.Getenv(xdgCacheHome); a != "" { | ||
path = filepath.Join(a, "trellis") | ||
} else if b := os.Getenv(localAppData); runtime.GOOS == "windows" && b != "" { | ||
path = filepath.Join(b, "Trellis CLI") | ||
} else { | ||
c, _ := os.UserHomeDir() | ||
path = filepath.Join(c, ".local", "state", "trellis") | ||
} | ||
return path | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package cli_config | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"os" | ||
"reflect" | ||
"strconv" | ||
"strings" | ||
|
||
"gopkg.in/yaml.v2" | ||
) | ||
|
||
type Config struct { | ||
AskVaultPass bool `yaml:"ask_vault_pass"` | ||
CheckForUpdates bool `yaml:"check_for_updates"` | ||
LoadPlugins bool `yaml:"load_plugins"` | ||
Open map[string]string `yaml:"open"` | ||
VirtualenvIntegration bool `yaml:"virtualenv_integration"` | ||
} | ||
|
||
var ( | ||
ErrUnsupportedType = errors.New("Invalid env var config setting: value is an unsupported type.") | ||
ErrCouldNotParse = errors.New("Invalid env var config setting: failed to parse value") | ||
) | ||
|
||
func NewConfig(defaultConfig Config) Config { | ||
return defaultConfig | ||
} | ||
|
||
func (c *Config) LoadFile(path string) error { | ||
configYaml, err := os.ReadFile(path) | ||
|
||
if err != nil && !os.IsNotExist(err) { | ||
return err | ||
} | ||
|
||
if err := yaml.Unmarshal(configYaml, &c); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (c *Config) LoadEnv(prefix string) error { | ||
structType := reflect.ValueOf(c).Elem() | ||
fields := reflect.VisibleFields(structType.Type()) | ||
|
||
for _, env := range os.Environ() { | ||
parts := strings.Split(env, "=") | ||
originalKey := parts[0] | ||
value := parts[1] | ||
|
||
key := strings.TrimPrefix(originalKey, prefix) | ||
|
||
if originalKey == key { | ||
// key is unchanged and didn't start with prefix | ||
continue | ||
} | ||
|
||
for _, field := range fields { | ||
if strings.ToLower(key) == field.Tag.Get("yaml") { | ||
structValue := structType.FieldByName(field.Name) | ||
|
||
if !structValue.CanSet() { | ||
continue | ||
} | ||
|
||
switch field.Type.Kind() { | ||
case reflect.Bool: | ||
val, err := strconv.ParseBool(value) | ||
|
||
if err != nil { | ||
return fmt.Errorf("%w '%s'\n'%s' can't be parsed as a boolean", ErrCouldNotParse, env, value) | ||
} | ||
|
||
structValue.SetBool(val) | ||
case reflect.Int: | ||
val, err := strconv.ParseInt(value, 10, 32) | ||
|
||
if err != nil { | ||
return fmt.Errorf("%w '%s'\n'%s' can't be parsed as an integer", ErrCouldNotParse, env, value) | ||
} | ||
|
||
structValue.SetInt(val) | ||
case reflect.Float32: | ||
val, err := strconv.ParseFloat(value, 32) | ||
if err != nil { | ||
return fmt.Errorf("%w '%s'\n'%s' can't be parsed as a float", ErrCouldNotParse, env, value) | ||
} | ||
|
||
structValue.SetFloat(val) | ||
default: | ||
return fmt.Errorf("%w\n%s setting of type %s is unsupported.", ErrUnsupportedType, env, field.Type.String()) | ||
} | ||
} | ||
} | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
package cli_config | ||
|
||
import ( | ||
_ "fmt" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func TestLoadFile(t *testing.T) { | ||
conf := Config{ | ||
AskVaultPass: false, | ||
LoadPlugins: true, | ||
} | ||
|
||
dir := t.TempDir() | ||
path := filepath.Join(dir, "cli.yml") | ||
content := ` | ||
ask_vault_pass: true | ||
open: | ||
roots: https://roots.io | ||
` | ||
|
||
if err := os.WriteFile(path, []byte(content), os.ModePerm); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
conf.LoadFile(path) | ||
|
||
if conf.LoadPlugins != true { | ||
t.Errorf("expected LoadPlugins to be true (default value)") | ||
} | ||
|
||
if conf.AskVaultPass != true { | ||
t.Errorf("expected AskVaultPass to be true") | ||
} | ||
|
||
open := conf.Open["roots"] | ||
expected := "https://roots.io" | ||
|
||
if open != expected { | ||
t.Errorf("expected open to be %s, got %s", expected, open) | ||
} | ||
} | ||
|
||
func TestLoadEnv(t *testing.T) { | ||
t.Setenv("TRELLIS_ASK_VAULT_PASS", "true") | ||
t.Setenv("TRELLIS_NOPE", "foo") | ||
t.Setenv("ASK_VAULT_PASS", "false") | ||
|
||
conf := Config{ | ||
AskVaultPass: false, | ||
} | ||
|
||
conf.LoadEnv("TRELLIS_") | ||
|
||
if conf.AskVaultPass != true { | ||
t.Errorf("expected AskVaultPass to be true") | ||
} | ||
} | ||
|
||
func TestLoadBoolParseError(t *testing.T) { | ||
t.Setenv("TRELLIS_ASK_VAULT_PASS", "foo") | ||
|
||
conf := Config{} | ||
|
||
err := conf.LoadEnv("TRELLIS_") | ||
|
||
if err == nil { | ||
t.Errorf("expected LoadEnv to return an error") | ||
} | ||
|
||
msg := err.Error() | ||
|
||
expected := ` | ||
Invalid env var config setting: failed to parse value 'TRELLIS_ASK_VAULT_PASS=foo' | ||
'foo' can't be parsed as a boolean | ||
` | ||
|
||
if msg != strings.TrimSpace(expected) { | ||
t.Errorf("expected error %s got %s", expected, msg) | ||
} | ||
} | ||
|
||
func TestLoadEnvUnsupportedType(t *testing.T) { | ||
t.Setenv("TRELLIS_OPEN", "foo") | ||
|
||
conf := Config{} | ||
|
||
err := conf.LoadEnv("TRELLIS_") | ||
|
||
if err == nil { | ||
t.Errorf("expected LoadEnv to return an error") | ||
} | ||
|
||
msg := err.Error() | ||
|
||
expected := ` | ||
Invalid env var config setting: value is an unsupported type. | ||
TRELLIS_OPEN=foo setting of type map[string]string is unsupported. | ||
` | ||
|
||
if msg != strings.TrimSpace(expected) { | ||
t.Errorf("expected error %s got %s", expected, msg) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.