diff --git a/scw/config.go b/scw/config.go index 34b576960..cb07a77b7 100644 --- a/scw/config.go +++ b/scw/config.go @@ -2,15 +2,18 @@ package scw import ( "bytes" + goerrors "errors" "io/ioutil" "os" "path/filepath" + "strings" "text/template" + "gopkg.in/yaml.v2" + "github.com/scaleway/scaleway-sdk-go/internal/auth" "github.com/scaleway/scaleway-sdk-go/internal/errors" "github.com/scaleway/scaleway-sdk-go/logger" - "gopkg.in/yaml.v2" ) const ( @@ -185,7 +188,24 @@ func MustLoadConfig() *Config { // LoadConfig read the config from the default path. func LoadConfig() (*Config, error) { - return LoadConfigFromPath(GetConfigPath()) + configPath := GetConfigPath() + cfg, err := LoadConfigFromPath(configPath) + + // Special case if using default config path + // if config.yaml does not exist, we should try to read config.yml + if os.Getenv(ScwConfigPathEnv) == "" { + var configNotFoundError *ConfigFileNotFoundError + if err != nil && goerrors.As(err, &configNotFoundError) && strings.HasSuffix(configPath, ".yaml") { + configPath = strings.TrimSuffix(configPath, ".yaml") + ".yml" + cfgYml, errYml := LoadConfigFromPath(configPath) + // If .yml config is not found, return first error when reading .yaml + if errYml == nil || (errYml != nil && !goerrors.As(errYml, &configNotFoundError)) { + return cfgYml, errYml + } + } + } + + return cfg, err } // LoadConfigFromPath read the config from the given path. diff --git a/scw/config_test.go b/scw/config_test.go index 859c8f03e..e1085fd50 100644 --- a/scw/config_test.go +++ b/scw/config_test.go @@ -447,6 +447,22 @@ func TestLoadProfileAndActiveProfile(t *testing.T) { }, expectedError: "scaleway-sdk-go: content of config file {HOME}/.scwrc is invalid json: invalid character ':' after top-level value", }, + + // Config file config.yaml and config.yml + { + name: "Read config.yml", + env: map[string]string{ + "HOME": "{HOME}", + }, + files: map[string]string{ + ".config/scw/config.yml": v2SimpleValidConfigFile, + }, + expectedAccessKey: s(v2ValidAccessKey), + expectedSecretKey: s(v2ValidSecretKey), + expectedDefaultOrganizationID: s(v2ValidDefaultOrganizationID), + expectedDefaultProjectID: s(v2ValidDefaultProjectID), + expectedDefaultRegion: s(v2ValidDefaultRegion), + }, } // create home dir