Skip to content

Commit

Permalink
Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
remyleone committed Jul 17, 2023
1 parent 8203d5d commit f109756
Show file tree
Hide file tree
Showing 7 changed files with 0 additions and 364 deletions.
3 changes: 0 additions & 3 deletions cmd/scw/testdata/test-all-usage-account-usage.golden
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ USAGE:
AVAILABLE COMMANDS:
project Project management commands

WORKFLOW COMMANDS:
ssh-key Initialize SSH key

FLAGS:
-h, --help help for account

Expand Down
93 changes: 0 additions & 93 deletions internal/namespaces/account/v3/custom.go
Original file line number Diff line number Diff line change
@@ -1,104 +1,11 @@
package account

import (
"context"
"os"
"path"
"reflect"
"strings"

"github.com/scaleway/scaleway-cli/v2/internal/args"
"github.com/scaleway/scaleway-cli/v2/internal/core"
"github.com/scaleway/scaleway-cli/v2/internal/interactive"
iam "github.com/scaleway/scaleway-sdk-go/api/iam/v1alpha1"
"github.com/scaleway/scaleway-sdk-go/scw"
)

func GetCommands() *core.Commands {
commands := GetGeneratedCommands()

commands.Merge(core.NewCommands(
initCommand(),
))
return commands
}

func initCommand() *core.Command {
return &core.Command{
Short: `Initialize SSH key`,
Long: `Initialize SSH key.`,
Namespace: "account",
Resource: "ssh-key",
Verb: "init",
Groups: []string{"workflow"},
ArgsType: reflect.TypeOf(args.RawArgs{}),
ArgSpecs: core.ArgSpecs{},
Run: InitRun,
}
}

func InitRun(ctx context.Context, _ interface{}) (i interface{}, e error) {
// Get default local SSH key
var shortenedFilename string
var err error
var localSSHKeyContent []byte
for _, keyName := range [3]string{"id_ecdsa.pub", "id_ed25519.pub", "id_rsa.pub"} {
// element is the element from someSlice for where we are
relativePath := path.Join(".ssh", keyName)
filename := path.Join(core.ExtractUserHomeDir(ctx), relativePath)
shortenedFilename = "~/" + relativePath
localSSHKeyContent, err = os.ReadFile(filename)
// If we managed to load an ssh key, let's stop there
if err == nil {
break
}
}
addKeyInstructions := `scw account ssh-key add name=my-key key="$(cat path/to/my/key.pub)"`
if err != nil && os.IsNotExist(err) {
return nil, sshKeyNotFound(shortenedFilename, addKeyInstructions)
}

// Get all SSH keys from Scaleway
client := core.ExtractClient(ctx)
api := iam.NewAPI(client)
listSSHKeysResponse, err := api.ListSSHKeys(&iam.ListSSHKeysRequest{}, scw.WithAllPages())
if err != nil {
return nil, err
}

// Early exit if the SSH key is present locally and on Scaleway
for _, SSHKey := range listSSHKeysResponse.SSHKeys {
if strings.TrimSpace(SSHKey.PublicKey) == strings.TrimSpace(string(localSSHKeyContent)) {
_, _ = interactive.Println("Looks like your local SSH key " + shortenedFilename + " is already present in your Scaleway account.")
return nil, nil
}
}

// Ask user
_, _ = interactive.Println("An SSH key is required if you want to connect to a server. More info at https://www.scaleway.com/en/docs/configure-new-ssh-key")
addSSHKey, err := interactive.PromptBoolWithConfig(&interactive.PromptBoolConfig{
Ctx: ctx,
Prompt: "We found an SSH key in " + shortenedFilename + ". Do you want to add it to your Scaleway account?",
DefaultValue: true,
})
if err != nil {
return nil, err
}

// Early exit if user doesn't want to add the key
if !addSSHKey {
return nil, installationCanceled(addKeyInstructions)
}

// Add key
_, err = api.CreateSSHKey(&iam.CreateSSHKeyRequest{
PublicKey: string(localSSHKeyContent),
})
if err != nil {
return nil, err
}

return &core.SuccessResult{
Message: "Key " + shortenedFilename + " successfully added",
}, nil
}
38 changes: 0 additions & 38 deletions internal/namespaces/account/v3/custom_test.go

This file was deleted.

21 changes: 0 additions & 21 deletions internal/namespaces/account/v3/error.go

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

0 comments on commit f109756

Please sign in to comment.