Skip to content

feat(instance): redirect to scw login when no credentials are provided #4617

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

Merged
merged 1 commit into from
Mar 26, 2025
Merged
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
5 changes: 2 additions & 3 deletions core/client.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package core

import (
"fmt"
"net/http"

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

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

if clientError, isClientError := err.(*platform.ClientError); isClientError {
err = &CliError{
Expand All @@ -35,5 +34,5 @@ func createClientError(err error) error {
}
}

return fmt.Errorf("failed to create client: %w", err)
return err
}
22 changes: 18 additions & 4 deletions internal/platform/terminal/terminal_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,9 @@ func errIsConfigFileNotFound(err error) bool {
// configErrorDetails generate a detailed error message for an invalid client option.
func configErrorDetails(configKey, varEnv string) string {
// TODO: update the more info link
return fmt.Sprintf(`%s can be initialised using the command "scw init".
return fmt.Sprintf(`%s can be initialized using the command "scw init".

After initialisation, there are three ways to provide %s:
After initialization, there are three ways to provide %s:
- with the Scaleway config file, in the %s key: %s;
- with the %s environement variable;

Expand All @@ -110,10 +110,25 @@ More info: https://github.com/scaleway/scaleway-sdk-go/tree/master/scw#scaleway-
)
}

// noConfigErrorDetails prints a message prompting the user to run 'scw login' when both the access key
// and the secret key are missing.
func noConfigErrorDetails() string {
return `You can create a new API keypair using the command "scw login".`
}

// validateClient validate a client configuration and make sure all mandatory setting are present.
// This function is only call for commands that require a valid client.
func validateClient(client *scw.Client) error {
accessKey, _ := client.GetAccessKey()
accessKey, accessKeyExists := client.GetAccessKey()
secretKey, secretKeyExists := client.GetSecretKey()

if !accessKeyExists && !secretKeyExists {
return &platform.ClientError{
Err: errors.New("no credentials provided"),
Details: noConfigErrorDetails(),
}
}

if accessKey == "" {
return &platform.ClientError{
Err: errors.New("access key is required"),
Expand All @@ -127,7 +142,6 @@ func validateClient(client *scw.Client) error {
}
}

secretKey, _ := client.GetSecretKey()
if secretKey == "" {
return &platform.ClientError{
Err: errors.New("secret key is required"),
Expand Down
Loading