-
Notifications
You must be signed in to change notification settings - Fork 18
/
ssh_key_list.go
74 lines (55 loc) · 1.88 KB
/
ssh_key_list.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
package cmd
import (
"fmt"
"strings"
"github.com/spf13/cobra"
"github.com/exoscale/cli/pkg/account"
"github.com/exoscale/cli/pkg/globalstate"
"github.com/exoscale/cli/pkg/output"
exoapi "github.com/exoscale/egoscale/v2/api"
)
type computeSSHKeyListItemOutput struct {
Name string `json:"name"`
Fingerprint string `json:"fingerprint"`
}
type computeSSHKeyListOutput []computeSSHKeyListItemOutput
func (o *computeSSHKeyListOutput) ToJSON() { output.JSON(o) }
func (o *computeSSHKeyListOutput) ToText() { output.Text(o) }
func (o *computeSSHKeyListOutput) ToTable() { output.Table(o) }
type computeSSHKeyListCmd struct {
cliCommandSettings `cli-cmd:"-"`
_ bool `cli-cmd:"list"`
}
func (c *computeSSHKeyListCmd) cmdAliases() []string { return nil }
func (c *computeSSHKeyListCmd) cmdShort() string { return "List SSH keys" }
func (c *computeSSHKeyListCmd) cmdLong() string {
return fmt.Sprintf(`This command lists SSH keys.
Supported output template annotations: %s`,
strings.Join(output.TemplateAnnotations(&computeSSHKeyListItemOutput{}), ", "))
}
func (c *computeSSHKeyListCmd) cmdPreRun(cmd *cobra.Command, args []string) error {
return cliCommandDefaultPreRun(c, cmd, args)
}
func (c *computeSSHKeyListCmd) cmdRun(_ *cobra.Command, _ []string) error {
ctx := exoapi.WithEndpoint(
gContext,
exoapi.NewReqEndpoint(account.CurrentAccount.Environment, account.CurrentAccount.DefaultZone),
)
sshKeys, err := globalstate.EgoscaleClient.ListSSHKeys(ctx, account.CurrentAccount.DefaultZone)
if err != nil {
return err
}
out := make(computeSSHKeyListOutput, 0)
for _, k := range sshKeys {
out = append(out, computeSSHKeyListItemOutput{
Name: *k.Name,
Fingerprint: *k.Fingerprint,
})
}
return c.outputFunc(&out, nil)
}
func init() {
cobra.CheckErr(registerCLICommand(computeSSHKeyCmd, &computeSSHKeyListCmd{
cliCommandSettings: defaultCLICmdSettings(),
}))
}