-
Notifications
You must be signed in to change notification settings - Fork 20
/
ssh_key_register.go
81 lines (63 loc) · 1.87 KB
/
ssh_key_register.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
69
70
71
72
73
74
75
76
77
78
79
80
81
package cmd
import (
"fmt"
"os"
"strings"
"time"
egoscale "github.com/exoscale/egoscale/v2"
exoapi "github.com/exoscale/egoscale/v2/api"
"github.com/spf13/cobra"
)
type computeSSHKeyRegisterCmd struct {
cliCommandSettings `cli-cmd:"-"`
_ bool `cli-cmd:"register"`
Name string `cli-arg:"#"`
PublicKeyFile string `cli-arg:"#"`
}
func (c *computeSSHKeyRegisterCmd) cmdAliases() []string { return gCreateAlias }
func (c *computeSSHKeyRegisterCmd) cmdShort() string {
return "Register an SSH key"
}
func (c *computeSSHKeyRegisterCmd) cmdLong() string {
return fmt.Sprintf(`This command registers a new SSH key.
Supported output template annotations: %s`,
strings.Join(outputterTemplateAnnotations(&computeSSHKeyShowOutput{}), ", "))
}
func (c *computeSSHKeyRegisterCmd) cmdPreRun(cmd *cobra.Command, args []string) error {
return cliCommandDefaultPreRun(c, cmd, args)
}
func (c *computeSSHKeyRegisterCmd) cmdRun(cmd *cobra.Command, _ []string) error {
var (
sshKey *egoscale.SSHKey
err error
)
// Template registration can take a _long time_, raising
// the Exoscale API client timeout as a precaution.
cs.Client.SetTimeout(30 * time.Minute)
ctx := exoapi.WithEndpoint(
gContext,
exoapi.NewReqEndpoint(gCurrentAccount.Environment, gCurrentAccount.DefaultZone),
)
publicKey, err := os.ReadFile(c.PublicKeyFile)
if err != nil {
return err
}
decorateAsyncOperation(fmt.Sprintf("Registering SSH key %q...", c.Name), func() {
sshKey, err = cs.RegisterSSHKey(ctx, gCurrentAccount.DefaultZone, c.Name, string(publicKey))
})
if err != nil {
return err
}
if !gQuiet {
return output(&computeSSHKeyShowOutput{
Fingerprint: *sshKey.Fingerprint,
Name: *sshKey.Name,
}, nil)
}
return nil
}
func init() {
cobra.CheckErr(registerCLICommand(computeSSHKeyCmd, &computeSSHKeyRegisterCmd{
cliCommandSettings: defaultCLICmdSettings(),
}))
}