-
Notifications
You must be signed in to change notification settings - Fork 75
/
ls.go
63 lines (56 loc) · 1.28 KB
/
ls.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
package sshkey
import (
"encoding/json"
"fmt"
"os"
"github.com/cnrancher/autok3s/pkg/common"
"github.com/cnrancher/autok3s/pkg/utils"
"github.com/olekukonko/tablewriter"
"github.com/spf13/cobra"
)
var (
listCmd = &cobra.Command{
Use: "list [name]",
Aliases: []string{"ls"},
Short: "List all stored ssh key pairs.",
Args: cobra.MaximumNArgs(1),
Run: utils.CommandExitWithoutHelpInfo(list),
}
)
func init() {
listCmd.Flags().BoolVarP(&sshKeyFlags.isJSON, "json", "j", sshKeyFlags.isJSON, "json output")
}
func list(cmd *cobra.Command, args []string) error {
var err error
var list []*common.SSHKey
if len(args) == 1 {
list, err = common.DefaultDB.ListSSHKey(&args[0])
} else {
list, err = common.DefaultDB.ListSSHKey(nil)
}
if err != nil {
return err
}
if sshKeyFlags.isJSON {
data, err := json.Marshal(list)
if err != nil {
return err
}
cmd.Printf("%s\n", string(data))
return nil
}
table := tablewriter.NewWriter(os.Stdout)
table.SetBorder(false)
table.SetHeaderLine(false)
table.SetColumnSeparator("")
table.SetAlignment(tablewriter.ALIGN_LEFT)
table.SetHeader([]string{"Name", "Encrypted"})
for _, key := range list {
table.Append([]string{
key.Name,
fmt.Sprintf("%v", key.HasPassword),
})
}
table.Render()
return nil
}