Skip to content

Commit e895ab5

Browse files
authored
feat(scw): try to read yaml file as yml on fail (#1480)
1 parent 0597224 commit e895ab5

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

scw/config.go

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,18 @@ package scw
22

33
import (
44
"bytes"
5+
goerrors "errors"
56
"io/ioutil"
67
"os"
78
"path/filepath"
9+
"strings"
810
"text/template"
911

12+
"gopkg.in/yaml.v2"
13+
1014
"github.com/scaleway/scaleway-sdk-go/internal/auth"
1115
"github.com/scaleway/scaleway-sdk-go/internal/errors"
1216
"github.com/scaleway/scaleway-sdk-go/logger"
13-
"gopkg.in/yaml.v2"
1417
)
1518

1619
const (
@@ -185,7 +188,24 @@ func MustLoadConfig() *Config {
185188

186189
// LoadConfig read the config from the default path.
187190
func LoadConfig() (*Config, error) {
188-
return LoadConfigFromPath(GetConfigPath())
191+
configPath := GetConfigPath()
192+
cfg, err := LoadConfigFromPath(configPath)
193+
194+
// Special case if using default config path
195+
// if config.yaml does not exist, we should try to read config.yml
196+
if os.Getenv(ScwConfigPathEnv) == "" {
197+
var configNotFoundError *ConfigFileNotFoundError
198+
if err != nil && goerrors.As(err, &configNotFoundError) && strings.HasSuffix(configPath, ".yaml") {
199+
configPath = strings.TrimSuffix(configPath, ".yaml") + ".yml"
200+
cfgYml, errYml := LoadConfigFromPath(configPath)
201+
// If .yml config is not found, return first error when reading .yaml
202+
if errYml == nil || (errYml != nil && !goerrors.As(errYml, &configNotFoundError)) {
203+
return cfgYml, errYml
204+
}
205+
}
206+
}
207+
208+
return cfg, err
189209
}
190210

191211
// LoadConfigFromPath read the config from the given path.

scw/config_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,22 @@ func TestLoadProfileAndActiveProfile(t *testing.T) {
447447
},
448448
expectedError: "scaleway-sdk-go: content of config file {HOME}/.scwrc is invalid json: invalid character ':' after top-level value",
449449
},
450+
451+
// Config file config.yaml and config.yml
452+
{
453+
name: "Read config.yml",
454+
env: map[string]string{
455+
"HOME": "{HOME}",
456+
},
457+
files: map[string]string{
458+
".config/scw/config.yml": v2SimpleValidConfigFile,
459+
},
460+
expectedAccessKey: s(v2ValidAccessKey),
461+
expectedSecretKey: s(v2ValidSecretKey),
462+
expectedDefaultOrganizationID: s(v2ValidDefaultOrganizationID),
463+
expectedDefaultProjectID: s(v2ValidDefaultProjectID),
464+
expectedDefaultRegion: s(v2ValidDefaultRegion),
465+
},
450466
}
451467

452468
// create home dir

0 commit comments

Comments
 (0)