Skip to content

fix(config): simplify config file not found error #2622

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions scw/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}

Expand Down Expand Up @@ -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
}
}
Expand All @@ -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
Expand Down
8 changes: 4 additions & 4 deletions scw/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}",
},
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down
18 changes: 2 additions & 16 deletions scw/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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"`
Expand Down
2 changes: 1 addition & 1 deletion scw/load_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down