-
Notifications
You must be signed in to change notification settings - Fork 58
/
list.go
64 lines (53 loc) · 1.5 KB
/
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
// Copyright (C) 2022, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package cmd
import (
"io/ioutil"
"os"
"sort"
"strings"
"github.com/olekukonko/tablewriter"
"github.com/spf13/cobra"
)
// listCmd represents the list command
var listCmd = &cobra.Command{
Use: "list",
Short: "List all created subnet configurations",
Long: `The subnet list command prints the names of all created subnet
configurations.`,
RunE: listGenesis,
SilenceUsage: true,
}
type subnetMatrix [][]string
func (c subnetMatrix) Len() int { return len(c) }
func (c subnetMatrix) Swap(i, j int) { c[i], c[j] = c[j], c[i] }
// Compare strings by first key of the sub-slice
func (c subnetMatrix) Less(i, j int) bool { return strings.Compare(c[i][0], c[j][0]) == -1 }
func listGenesis(cmd *cobra.Command, args []string) error {
header := []string{"subnet", "chain", "type"}
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader(header)
table.SetAutoMergeCellsByColumnIndex([]int{0})
table.SetRowLine(true)
files, err := ioutil.ReadDir(baseDir)
if err != nil {
return err
}
rows := subnetMatrix{}
for _, f := range files {
if strings.Contains(f.Name(), sidecar_suffix) {
// read in sidecar file
sc, err := loadSidecar(strings.TrimSuffix(f.Name(), sidecar_suffix))
if err != nil {
return err
}
rows = append(rows, []string{sc.Subnet, sc.Name, string(sc.Vm)})
}
}
sort.Sort(rows)
for _, row := range rows {
table.Append(row)
}
table.Render()
return nil
}