/
ls.go
56 lines (50 loc) · 1.13 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
package airgap
import (
"encoding/json"
"os"
"strings"
"github.com/cnrancher/autok3s/pkg/common"
"github.com/olekukonko/tablewriter"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
var (
listCmd = &cobra.Command{
Use: "ls",
Short: "List all stored airgap packages.",
RunE: list,
}
)
func init() {
listCmd.Flags().BoolVarP(&airgapFlags.isJSON, "json", "j", airgapFlags.isJSON, "json output")
}
func list(cmd *cobra.Command, _ []string) error {
pkgs, err := common.DefaultDB.ListPackages(nil)
if err != nil {
return errors.Wrap(err, "failed to list airgap packages")
}
if airgapFlags.isJSON {
data, err := json.Marshal(pkgs)
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", "K3sVersion", "Archs", "State"})
for _, pkg := range pkgs {
table.Append([]string{
pkg.Name,
pkg.K3sVersion,
strings.Join(pkg.Archs, ","),
string(pkg.State),
})
}
table.Render()
return nil
}