Skip to content

Commit

Permalink
fix regression: setting 3rd party URL via ARDUINO_BOARD_MANAGER_ADDIT…
Browse files Browse the repository at this point in the history
…IONAL_URLS env var
  • Loading branch information
cmaglie committed Jun 18, 2024
1 parent 55753bd commit 1460613
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 2 deletions.
2 changes: 1 addition & 1 deletion internal/cli/configuration/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func InjectEnvVars(settings *Settings) {
// Bind env aliases to keep backward compatibility
setIfEnvExists := func(key, env string) {
if v, ok := os.LookupEnv(env); ok {
settings.SetFromCLIArgs(key, v)
settings.SetFromENV(key, v)
}
}
setIfEnvExists("library.enable_unsafe_install", "ARDUINO_ENABLE_UNSAFE_LIBRARY_INSTALL")
Expand Down
39 changes: 38 additions & 1 deletion internal/go-configmap/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,43 @@ import (
"strings"
)

func (c *Map) SetFromENV(key string, arg string) error {
// in case of schemaless configuration, we don't know the type of the setting
// we will save it as a string
if len(c.schema) == 0 {
c.Set(key, arg)
return nil
}

// Find the correct type for the given setting
valueType, ok := c.schema[key]
if !ok {
return fmt.Errorf("key not found: %s", key)
}

var value any
{
var conversionError error
switch valueType.String() {
case "uint":
value, conversionError = strconv.Atoi(arg)
case "bool":
value, conversionError = strconv.ParseBool(arg)
case "string":
value = arg
case "[]string":
value = strings.Split(arg, " ")
default:
return fmt.Errorf("unhandled type: %s", valueType)
}
if conversionError != nil {
return fmt.Errorf("error setting value: %v", conversionError)
}
}

return c.Set(key, value)
}

func (c *Map) SetFromCLIArgs(key string, args ...string) error {
if len(args) == 0 {
c.Delete(key)
Expand Down Expand Up @@ -101,7 +138,7 @@ func (c *Map) InjectEnvVars(env []string, prefix string) []error {
}

// Update the configuration value
if err := c.SetFromCLIArgs(key, envValue); err != nil {
if err := c.SetFromENV(key, envValue); err != nil {
errs = append(errs, err)
}
}
Expand Down
29 changes: 29 additions & 0 deletions internal/integrationtest/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -902,3 +902,32 @@ build.unk: 123
require.NoError(t, err)
require.Equal(t, "en", strings.TrimSpace(string(out)))
}

func TestConfigViaEnvVars(t *testing.T) {
env, cli := integrationtest.CreateArduinoCLIWithEnvironment(t)
defer env.CleanUp()

// array of strings
out, _, err := cli.RunWithCustomEnv(
map[string]string{"ARDUINO_BOARD_MANAGER_ADDITIONAL_URLS": "https://espressif.github.io/arduino-esp32/package_esp32_index.json https://arduino.esp8266.com/stable/package_esp8266com_index.json"},
"config", "get", "board_manager.additional_urls",
)
require.NoError(t, err)
require.Equal(t, "- https://espressif.github.io/arduino-esp32/package_esp32_index.json\n- https://arduino.esp8266.com/stable/package_esp8266com_index.json\n\n", string(out))

// boolean
out, _, err = cli.RunWithCustomEnv(
map[string]string{"ARDUINO_LIBRARY_ENABLE_UNSAFE_INSTALL": "True"},
"config", "get", "library.enable_unsafe_install",
)
require.NoError(t, err)
require.Equal(t, "true\n\n", string(out))

// integer
out, _, err = cli.RunWithCustomEnv(
map[string]string{"ARDUINO_BUILD_CACHE_COMPILATIONS_BEFORE_PURGE": "20"},
"config", "get", "build_cache.compilations_before_purge",
)
require.NoError(t, err)
require.Equal(t, "20\n\n", string(out))
}

0 comments on commit 1460613

Please sign in to comment.