Skip to content

Commit

Permalink
feat(scw): try to read yaml file as yml on fail (#1480)
Browse files Browse the repository at this point in the history
  • Loading branch information
Codelax authored Dec 14, 2022
1 parent 0597224 commit e895ab5
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
24 changes: 22 additions & 2 deletions scw/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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.
Expand Down
16 changes: 16 additions & 0 deletions scw/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit e895ab5

Please sign in to comment.