Skip to content
This repository has been archived by the owner on Oct 23, 2023. It is now read-only.

Remove support for complex config keys #45

Merged
merged 2 commits into from
Apr 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions options/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,24 @@ func getEnvVarFile(service string) *envVarOverrides {
return &env
}

func (e *envVarOverrides) setEnvVariable(setting string, value string) error {
result := strings.ToUpper(setting)
// replace - with _ for keys such as add-known-secrets and edgex-startup-duration
result = strings.ReplaceAll(result, "-", "_")
// replace . with _ for config file overrides such as service.port
result = strings.ReplaceAll(result, ".", "_")
log.Infof("Mapping %s to %s", setting, result)
_, err := fmt.Fprintf(e.buffer, "export %s=%s\n", result, value)
func (e *envVarOverrides) setEnvVariable(key string, value string) error {
envKey, err := configKeyToEnvVar(key)
if err != nil {
return fmt.Errorf("error converting config key to environment variable key: %s", err)
}
log.Infof("Mapping %s to %s", key, envKey)
_, err = fmt.Fprintf(e.buffer, "export %s=%s\n", envKey, value)
return err
}

// convert my-var to MY_VAR
func configKeyToEnvVar(configKey string) (string, error) {
if strings.Contains(configKey, ".") {
return "", fmt.Errorf("config key must not contain dots: %s", configKey)
}
return strings.ReplaceAll(strings.ToUpper(configKey), "-", "_"), nil
}

func (e *envVarOverrides) getEnvFilename() string {

// The app-service-configurable snap is the one outlier snap that doesn't
Expand Down
8 changes: 6 additions & 2 deletions options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@ func processGlobalConfigOptions(services []string) error {
for _, service := range services {
overrides := getEnvVarFile(service)
for env, value := range configuration {
overrides.setEnvVariable(env, value)
if err := overrides.setEnvVariable(env, value); err != nil {
return err
}
}
overrides.writeEnvFile(false)
}
Expand Down Expand Up @@ -192,7 +194,9 @@ func processAppConfigOptions(services []string) error {
for env, value := range configuration {

log.Debugf("Processing overrides setEnvVariable: %v=%v", env, value)
overrides.setEnvVariable(env, value)
if err := overrides.setEnvVariable(env, value); err != nil {
return err
}
}
overrides.writeEnvFile(true)
}
Expand Down
10 changes: 5 additions & 5 deletions options/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func TestProcessAppConfig(t *testing.T) {
})

t.Run("global options", func(t *testing.T) {
const key, value = "config.x.y", "value"
const key, value = "config.x-y", "value"

t.Cleanup(func() {
assert.NoError(t, snapctl.Unset(key).Run())
Expand All @@ -80,7 +80,7 @@ func TestProcessAppConfig(t *testing.T) {
require.Error(t, options.ProcessAppConfig(testService, testService2))
})

t.Run("set/unset", func(t *testing.T) {
t.Run("set+unset", func(t *testing.T) {
require.NoError(t, snapctl.Set(configEnabled, "true").Run())
t.Cleanup(func() {
require.NoError(t, snapctl.Unset("config").Run())
Expand Down Expand Up @@ -113,14 +113,14 @@ func TestProcessAppConfig(t *testing.T) {
})

t.Run("single app options", func(t *testing.T) {
const key, value = "apps." + testService + ".config.x.y", "value"
const key, value = "apps." + testService + ".config.x-y", "value"

t.Cleanup(func() {
assert.NoError(t, snapctl.Unset(key).Run())
assert.NoError(t, os.RemoveAll(envFile))
})

t.Run("set/unset", func(t *testing.T) {
t.Run("set+unset", func(t *testing.T) {
require.NoError(t, snapctl.Set(configEnabled, "true").Run())
t.Cleanup(func() {
require.NoError(t, snapctl.Unset("apps").Run())
Expand Down Expand Up @@ -176,7 +176,7 @@ func TestProcessAppConfig(t *testing.T) {
t.Run("reject mixed legacy options", func(t *testing.T) {
const (
legacyKey, legacyValue = "env.core-data.service.host", "legacy"
key, value = "apps.core-data.config.x.y", "value"
key, value = "apps.core-data.config.x-y", "value"
)

configCoreDataDir := fmt.Sprintf("%s/core-data/res/", env.SnapDataConf)
Expand Down