-
Notifications
You must be signed in to change notification settings - Fork 20
/
sshkey_upload.go
68 lines (55 loc) · 1.56 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package cmd
import (
"fmt"
"os"
"strings"
"github.com/spf13/cobra"
"github.com/exoscale/cli/pkg/globalstate"
"github.com/exoscale/cli/pkg/output"
"github.com/exoscale/egoscale"
)
type sshkeyUploadOutput struct {
Name string `json:"name"`
Fingerprint string `json:"fingerprint"`
}
func (o *sshkeyUploadOutput) Type() string { return "SSH Key" }
func (o *sshkeyUploadOutput) ToJSON() { output.JSON(o) }
func (o *sshkeyUploadOutput) ToText() { output.Text(o) }
func (o *sshkeyUploadOutput) ToTable() { output.Table(o) }
func init() {
sshkeyCmd.AddCommand(&cobra.Command{
Use: "upload NAME PUBLIC-KEY-FILE",
Short: "Upload SSH key",
Long: fmt.Sprintf(`This command uploads a locally existing SSH key.
Supported output template annotations: %s`,
strings.Join(output.TemplateAnnotations(&sshkeyUploadOutput{}), ", ")),
Aliases: gUploadAlias,
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) < 2 {
return cmd.Usage()
}
return printOutput(uploadSSHKey(args[0], args[1]))
},
})
}
func uploadSSHKey(name, publicKeyPath string) (output.Outputter, error) {
pbk, err := os.ReadFile(publicKeyPath)
if err != nil {
return nil, err
}
resp, err := globalstate.EgoscaleClient.RequestWithContext(gContext, &egoscale.RegisterSSHKeyPair{
Name: name,
PublicKey: string(pbk),
})
if err != nil {
return nil, err
}
keyPair := resp.(*egoscale.SSHKeyPair)
if !globalstate.Quiet {
return &sshkeyUploadOutput{
Name: keyPair.Name,
Fingerprint: keyPair.Fingerprint,
}, nil
}
return nil, nil
}