Skip to content

Commit f7d2c5e

Browse files
committed
feat(instance): redirect to scw login when no credentials are provided
1 parent 030ab65 commit f7d2c5e

2 files changed

Lines changed: 28 additions & 8 deletions

File tree

core/client.go

Lines changed: 1 addition & 2 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"
@@ -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: 27 additions & 6 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,12 +110,23 @@ More info: https://github.com/scaleway/scaleway-sdk-go/tree/master/scw#scaleway-
110110
)
111111
}
112112

113+
// noConfigErrorDetails prints a message for when both the access key and the secret key are missing.
114+
func noConfigErrorDetails() string {
115+
return `No credentials were provided.
116+
117+
You can create new credentials using the command "scw login".
118+
119+
Once created, you can check them here: https://console.scaleway.com/iam/api-keys`
120+
}
121+
113122
// validateClient validate a client configuration and make sure all mandatory setting are present.
114123
// This function is only call for commands that require a valid client.
115124
func validateClient(client *scw.Client) error {
116-
accessKey, _ := client.GetAccessKey()
125+
var configError *platform.ClientError
126+
127+
accessKey, accessKeyExists := client.GetAccessKey()
117128
if accessKey == "" {
118-
return &platform.ClientError{
129+
configError = &platform.ClientError{
119130
Err: errors.New("access key is required"),
120131
Details: configErrorDetails("access_key", "SCW_ACCESS_KEY"),
121132
}
@@ -127,9 +138,9 @@ func validateClient(client *scw.Client) error {
127138
}
128139
}
129140

130-
secretKey, _ := client.GetSecretKey()
141+
secretKey, secretKeyExists := client.GetSecretKey()
131142
if secretKey == "" {
132-
return &platform.ClientError{
143+
configError = &platform.ClientError{
133144
Err: errors.New("secret key is required"),
134145
Details: configErrorDetails("secret_key", "SCW_SECRET_KEY"),
135146
}
@@ -141,6 +152,16 @@ func validateClient(client *scw.Client) error {
141152
}
142153
}
143154

155+
switch {
156+
case !accessKeyExists && !secretKeyExists:
157+
return &platform.ClientError{
158+
Err: errors.New("no credentials provided"),
159+
Details: noConfigErrorDetails(),
160+
}
161+
case !accessKeyExists || !secretKeyExists:
162+
return configError
163+
}
164+
144165
defaultOrganizationID, _ := client.GetDefaultOrganizationID()
145166
if defaultOrganizationID == "" {
146167
return &platform.ClientError{

0 commit comments

Comments
 (0)