diff --git a/scw/config.go b/scw/config.go index 5fbae345e..500b13ac2 100644 --- a/scw/config.go +++ b/scw/config.go @@ -5,6 +5,7 @@ import ( "io/ioutil" "os" "path/filepath" + "syscall" "text/template" "github.com/scaleway/scaleway-sdk-go/internal/auth" @@ -170,6 +171,9 @@ func LoadConfigFromPath(path string) (*Config, error) { file, err := ioutil.ReadFile(path) if err != nil { + if pathError, isPathError := err.(*os.PathError); isPathError && pathError.Err == syscall.ENOENT { + return nil, configFileNotFound(pathError.Path) + } return nil, errors.Wrap(err, "cannot read config file") } diff --git a/scw/config_test.go b/scw/config_test.go index f4989b39c..042156142 100644 --- a/scw/config_test.go +++ b/scw/config_test.go @@ -265,7 +265,7 @@ func TestLoadProfileAndActiveProfile(t *testing.T) { }, { name: "No config", - expectedError: "scaleway-sdk-go: cannot read config file: open {HOME}/.config/scw/config.yaml: no such file or directory", + expectedError: "scaleway-sdk-go: cannot read config file {HOME}/.config/scw/config.yaml: no such file or directory", env: map[string]string{ "HOME": "{HOME}", }, diff --git a/scw/errors.go b/scw/errors.go index 0473008da..330f6f24b 100644 --- a/scw/errors.go +++ b/scw/errors.go @@ -270,3 +270,18 @@ func (e InvalidClientOptionError) IsScwSdkError() {} func (e InvalidClientOptionError) Error() string { return fmt.Sprintf("scaleway-sdk-go: %s", 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) +} diff --git a/scw/load_config_test.go b/scw/load_config_test.go index a6a85eb10..ea8f40e9b 100644 --- a/scw/load_config_test.go +++ b/scw/load_config_test.go @@ -102,7 +102,7 @@ func TestLoad(t *testing.T) { env: map[string]string{ scwConfigPathEnv: "{HOME}/fake/test.conf", }, - expectedError: "scaleway-sdk-go: cannot read config file: open {HOME}/fake/test.conf: no such file or directory", + expectedError: "scaleway-sdk-go: cannot read config file {HOME}/fake/test.conf: no such file or directory", }, { name: "Err: custom-path config with invalid V2",