Skip to content

Commit

Permalink
Renamed function to better reflect his behaviour
Browse files Browse the repository at this point in the history
  • Loading branch information
cmaglie committed Jul 30, 2024
1 parent ab9d2f0 commit d72a2c2
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 9 deletions.
4 changes: 2 additions & 2 deletions internal/cli/configuration/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,10 @@ func getDefaultUserDir() string {
}
}

// FindConfigFileInArgsFallbackOnEnv returns the config file path using the
// FindConfigFlagsInArgsOrFallbackOnEnv returns the config file path using the
// argument '--config-file' (if specified), if empty looks for the ARDUINO_CONFIG_FILE env,
// or looking in the current working dir
func FindConfigFileInArgsFallbackOnEnv(args []string) string {
func FindConfigFlagsInArgsOrFallbackOnEnv(args []string) string {
// Look for '--config-dir' argument
for i, arg := range args {
if arg == "--config-dir" {
Expand Down
12 changes: 6 additions & 6 deletions internal/cli/configuration/configuration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,23 +43,23 @@ func TestInit(t *testing.T) {

func TestFindConfigFile(t *testing.T) {
defaultConfigFile := filepath.Join(getDefaultArduinoDataDir(), "arduino-cli.yaml")
configFile := FindConfigFileInArgsFallbackOnEnv([]string{"--config-file"})
configFile := FindConfigFlagsInArgsOrFallbackOnEnv([]string{"--config-file"})
require.Equal(t, defaultConfigFile, configFile)

configFile = FindConfigFileInArgsFallbackOnEnv([]string{"--config-file", "some/path/to/config"})
configFile = FindConfigFlagsInArgsOrFallbackOnEnv([]string{"--config-file", "some/path/to/config"})
require.Equal(t, "some/path/to/config", configFile)

configFile = FindConfigFileInArgsFallbackOnEnv([]string{"--config-file", "some/path/to/config/arduino-cli.yaml"})
configFile = FindConfigFlagsInArgsOrFallbackOnEnv([]string{"--config-file", "some/path/to/config/arduino-cli.yaml"})
require.Equal(t, "some/path/to/config/arduino-cli.yaml", configFile)

configFile = FindConfigFileInArgsFallbackOnEnv([]string{})
configFile = FindConfigFlagsInArgsOrFallbackOnEnv([]string{})
require.Equal(t, defaultConfigFile, configFile)

t.Setenv("ARDUINO_CONFIG_FILE", "some/path/to/config")
configFile = FindConfigFileInArgsFallbackOnEnv([]string{})
configFile = FindConfigFlagsInArgsOrFallbackOnEnv([]string{})
require.Equal(t, "some/path/to/config", configFile)

// when both env and flag are specified flag takes precedence
configFile = FindConfigFileInArgsFallbackOnEnv([]string{"--config-file", "flag/path"})
configFile = FindConfigFlagsInArgsOrFallbackOnEnv([]string{"--config-file", "flag/path"})
require.Equal(t, "flag/path", configFile)
}
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func main() {
logrus.SetOutput(io.Discard)

// Search for the configuration file in the command line arguments and in the environment
configFile := configuration.FindConfigFileInArgsFallbackOnEnv(os.Args)
configFile := configuration.FindConfigFlagsInArgsOrFallbackOnEnv(os.Args)
ctx := config.SetConfigFile(context.Background(), configFile)

// Create a new ArduinoCoreServer
Expand Down

0 comments on commit d72a2c2

Please sign in to comment.