Skip to content

Commit 7cd0dd3

Browse files
Mia-CrossLaure-di
authored andcommitted
feat(instance): redirect to scw login when no credentials are provided (scaleway#4617)
1 parent fb8c0f6 commit 7cd0dd3

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

core/client.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package core
22

33
import (
4-
"fmt"
54
"net/http"
65

76
"github.com/scaleway/scaleway-cli/v2/internal/platform"
@@ -25,7 +24,7 @@ func createAnonymousClient(httpClient *http.Client, buildInfo *BuildInfo) (*scw.
2524
}
2625

2726
func createClientError(err error) error {
28-
credentialsHint := "You can get your credentials here: https://console.scaleway.com/iam/api-keys"
27+
credentialsHint := "You can check your credentials here: https://console.scaleway.com/iam/api-keys"
2928

3029
if clientError, isClientError := err.(*platform.ClientError); isClientError {
3130
err = &CliError{
@@ -35,5 +34,5 @@ func createClientError(err error) error {
3534
}
3635
}
3736

38-
return fmt.Errorf("failed to create client: %w", err)
37+
return err
3938
}

internal/platform/terminal/terminal_client.go

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,9 @@ func errIsConfigFileNotFound(err error) bool {
9393
// configErrorDetails generate a detailed error message for an invalid client option.
9494
func configErrorDetails(configKey, varEnv string) string {
9595
// TODO: update the more info link
96-
return fmt.Sprintf(`%s can be initialised using the command "scw init".
96+
return fmt.Sprintf(`%s can be initialized using the command "scw init".
9797
98-
After initialisation, there are three ways to provide %s:
98+
After initialization, there are three ways to provide %s:
9999
- with the Scaleway config file, in the %s key: %s;
100100
- with the %s environement variable;
101101
@@ -110,10 +110,25 @@ More info: https://github.com/scaleway/scaleway-sdk-go/tree/master/scw#scaleway-
110110
)
111111
}
112112

113+
// noConfigErrorDetails prints a message prompting the user to run 'scw login' when both the access key
114+
// and the secret key are missing.
115+
func noConfigErrorDetails() string {
116+
return `You can create a new API keypair using the command "scw login".`
117+
}
118+
113119
// validateClient validate a client configuration and make sure all mandatory setting are present.
114120
// This function is only call for commands that require a valid client.
115121
func validateClient(client *scw.Client) error {
116-
accessKey, _ := client.GetAccessKey()
122+
accessKey, accessKeyExists := client.GetAccessKey()
123+
secretKey, secretKeyExists := client.GetSecretKey()
124+
125+
if !accessKeyExists && !secretKeyExists {
126+
return &platform.ClientError{
127+
Err: errors.New("no credentials provided"),
128+
Details: noConfigErrorDetails(),
129+
}
130+
}
131+
117132
if accessKey == "" {
118133
return &platform.ClientError{
119134
Err: errors.New("access key is required"),
@@ -127,7 +142,6 @@ func validateClient(client *scw.Client) error {
127142
}
128143
}
129144

130-
secretKey, _ := client.GetSecretKey()
131145
if secretKey == "" {
132146
return &platform.ClientError{
133147
Err: errors.New("secret key is required"),

0 commit comments

Comments
 (0)