-
Notifications
You must be signed in to change notification settings - Fork 20
/
sshkey_upload.go
50 lines (42 loc) · 1.02 KB
/
sshkey_upload.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
package cmd
import (
"io/ioutil"
"os"
"github.com/exoscale/cli/table"
"github.com/exoscale/egoscale"
"github.com/spf13/cobra"
)
// uploadCmd represents the upload command
var uploadCmd = &cobra.Command{
Use: "upload <name> <public key file>",
Short: "Upload SSH key pair from given public key",
Aliases: gUploadAlias,
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) < 2 {
return cmd.Usage()
}
return uploadSSHKey(args[0], args[1])
},
}
func uploadSSHKey(name, publicKeyPath string) error {
pbk, err := ioutil.ReadFile(publicKeyPath)
if err != nil {
return err
}
resp, err := cs.RequestWithContext(gContext, &egoscale.RegisterSSHKeyPair{
Name: name,
PublicKey: string(pbk),
})
if err != nil {
return err
}
keyPair := resp.(*egoscale.SSHKeyPair)
table := table.NewTable(os.Stdout)
table.SetHeader([]string{"Name", "Fingerprint"})
table.Append([]string{keyPair.Name, keyPair.Fingerprint})
table.Render()
return nil
}
func init() {
sshkeyCmd.AddCommand(uploadCmd)
}