diff --git a/scw/config.go b/scw/config.go index af0995df..60bfd2a0 100644 --- a/scw/config.go +++ b/scw/config.go @@ -66,7 +66,7 @@ const configFileTemplate = `# Scaleway configuration file # A configuration is a named set of Scaleway properties. # Starting off with a Scaleway SDK or Scaleway CLI, you’ll work with a single configuration named default. -# You can set properties of the default profile by running either scw init or scw config set. +# You can set properties of the default profile by running either scw init or scw config set. # This single default configuration is suitable for most use cases. {{ if .ActiveProfile }}active_profile: {{ .ActiveProfile }}{{ else }}# active_profile: myProfile{{ end }} @@ -195,12 +195,11 @@ func LoadConfig() (*Config, error) { // 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") { + if err != nil && goerrors.Is(err, ErrConfigFileNotFound) && 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)) { + if errYml == nil || (errYml != nil && !goerrors.Is(errYml, ErrConfigFileNotFound)) { return cfgYml, errYml } } @@ -213,7 +212,7 @@ func LoadConfig() (*Config, error) { func LoadConfigFromPath(path string) (*Config, error) { _, err := os.Stat(path) if os.IsNotExist(err) { - return nil, configFileNotFound(path) + return nil, ErrConfigFileNotFound } if err != nil { return nil, err diff --git a/scw/config_test.go b/scw/config_test.go index fa269e94..8dcdbe00 100644 --- a/scw/config_test.go +++ b/scw/config_test.go @@ -277,7 +277,7 @@ func TestLoadProfileAndActiveProfile(t *testing.T) { }, { name: "No config", - expectedError: "scaleway-sdk-go: cannot read config file {HOME}/.config/scw/config.yaml: no such file or directory", + expectedError: ErrConfigFileNotFound.Error(), env: map[string]string{ "HOME": "{HOME}", }, @@ -633,7 +633,7 @@ func TestConfig_ConfigFile(t *testing.T) { # A configuration is a named set of Scaleway properties. # Starting off with a Scaleway SDK or Scaleway CLI, you’ll work with a single configuration named default. -# You can set properties of the default profile by running either scw init or scw config set. +# You can set properties of the default profile by running either scw init or scw config set. # This single default configuration is suitable for most use cases. # active_profile: myProfile @@ -712,7 +712,7 @@ access_key: SCW1234567890ABCDEFG # A configuration is a named set of Scaleway properties. # Starting off with a Scaleway SDK or Scaleway CLI, you’ll work with a single configuration named default. -# You can set properties of the default profile by running either scw init or scw config set. +# You can set properties of the default profile by running either scw init or scw config set. # This single default configuration is suitable for most use cases. # active_profile: myProfile @@ -804,7 +804,7 @@ secret_key: 7363616c-6577-6573-6862-6f7579616161 # A configuration is a named set of Scaleway properties. # Starting off with a Scaleway SDK or Scaleway CLI, you’ll work with a single configuration named default. -# You can set properties of the default profile by running either scw init or scw config set. +# You can set properties of the default profile by running either scw init or scw config set. # This single default configuration is suitable for most use cases. active_profile: flantier diff --git a/scw/errors.go b/scw/errors.go index a89e41fa..4fbcf76b 100644 --- a/scw/errors.go +++ b/scw/errors.go @@ -42,6 +42,8 @@ type ResponseError struct { RawBody json.RawMessage `json:"-"` } +var ErrConfigFileNotFound = errors.New("cannot read config file: no such file or directory") + func (e *ResponseError) UnmarshalJSON(b []byte) error { type tmpResponseError ResponseError tmp := tmpResponseError(*e) @@ -472,22 +474,6 @@ func (e InvalidClientOptionError) Error() string { return "scaleway-sdk-go: " + e.errorType } -// ConfigFileNotFound indicates that the config file could not be found -type ConfigFileNotFoundError struct { - path string -} - -func configFileNotFound(path string) *ConfigFileNotFoundError { - return &ConfigFileNotFoundError{path: path} -} - -// ConfigFileNotFoundError implements the SdkError interface -func (e ConfigFileNotFoundError) IsScwSdkError() {} - -func (e ConfigFileNotFoundError) Error() string { - return fmt.Sprintf("scaleway-sdk-go: cannot read config file %s: no such file or directory", e.path) -} - // ResourceExpiredError implements the SdkError interface type ResourceExpiredError struct { Resource string `json:"resource"` diff --git a/scw/load_config_test.go b/scw/load_config_test.go index 7f3d9a31..6ef0ca38 100644 --- a/scw/load_config_test.go +++ b/scw/load_config_test.go @@ -74,7 +74,7 @@ func TestLoad(t *testing.T) { env: map[string]string{ ScwConfigPathEnv: "{HOME}/fake/test.conf", }, - expectedError: "scaleway-sdk-go: cannot read config file {HOME}/fake/test.conf: no such file or directory", + expectedError: ErrConfigFileNotFound.Error(), }, { name: "Err: custom-path config with invalid V2",