-
Notifications
You must be signed in to change notification settings - Fork 20
/
sshkey_create.go
57 lines (48 loc) · 1.19 KB
/
sshkey_create.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package cmd
import (
"fmt"
"os"
"github.com/exoscale/cli/table"
"github.com/exoscale/egoscale"
"github.com/spf13/cobra"
)
// sshCreateCmd represents the create command
var sshCreateCmd = &cobra.Command{
Use: "create <name>",
Short: "Create SSH key pair",
Aliases: gCreateAlias,
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return cmd.Usage()
}
keyPair, err := createSSHKey(args[0])
if err != nil {
return err
}
displayResult(keyPair)
return nil
},
}
func createSSHKey(name string) (*egoscale.SSHKeyPair, error) {
resp, err := cs.RequestWithContext(gContext, &egoscale.CreateSSHKeyPair{
Name: name,
})
if err != nil {
return nil, err
}
sshKeyPair, ok := resp.(*egoscale.SSHKeyPair)
if !ok {
return nil, fmt.Errorf("wrong type expected %q, got %T", "egoscale.CreateSSHKeyPairResponse", resp)
}
return sshKeyPair, nil
}
func displayResult(sshKeyPair *egoscale.SSHKeyPair) {
table := table.NewTable(os.Stdout)
table.SetHeader([]string{"Name", "Fingerprint"})
table.Append([]string{sshKeyPair.Name, sshKeyPair.Fingerprint})
table.Render()
fmt.Println(sshKeyPair.PrivateKey)
}
func init() {
sshkeyCmd.AddCommand(sshCreateCmd)
}