-
Notifications
You must be signed in to change notification settings - Fork 18
/
ssh_key_register.go
85 lines (66 loc) · 2.07 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
82
83
84
85
package cmd
import (
"fmt"
"os"
"strings"
"time"
"github.com/spf13/cobra"
"github.com/exoscale/cli/pkg/account"
"github.com/exoscale/cli/pkg/globalstate"
"github.com/exoscale/cli/pkg/output"
egoscale "github.com/exoscale/egoscale/v2"
exoapi "github.com/exoscale/egoscale/v2/api"
)
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(output.TemplateAnnotations(&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.
globalstate.EgoscaleClient.Client.SetTimeout(30 * time.Minute)
ctx := exoapi.WithEndpoint(
gContext,
exoapi.NewReqEndpoint(account.CurrentAccount.Environment, account.CurrentAccount.DefaultZone),
)
publicKey, err := os.ReadFile(c.PublicKeyFile)
if err != nil {
return err
}
decorateAsyncOperation(fmt.Sprintf("Registering SSH key %q...", c.Name), func() {
sshKey, err = globalstate.EgoscaleClient.RegisterSSHKey(ctx, account.CurrentAccount.DefaultZone, c.Name, string(publicKey))
})
if err != nil {
return err
}
if !globalstate.Quiet {
return c.outputFunc(&computeSSHKeyShowOutput{
Fingerprint: *sshKey.Fingerprint,
Name: *sshKey.Name,
}, nil)
}
return nil
}
func init() {
cobra.CheckErr(registerCLICommand(computeSSHKeyCmd, &computeSSHKeyRegisterCmd{
cliCommandSettings: defaultCLICmdSettings(),
}))
}