/
core_list.go
69 lines (52 loc) · 1.53 KB
/
core_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
package cmd
import (
"fmt"
"net/url"
"github.com/jedib0t/go-pretty/v6/table"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
// listCoreCmd represents the backup command
var listCoreCmd = &cobra.Command{
Use: "list",
Short: "List all known cores",
Long: "List all known cores.",
RunE: func(cmd *cobra.Command, args []string) error {
selected := viper.GetString("cores.selected")
list := viper.GetStringMapString("cores.list")
if len(list) == 0 {
fmt.Println("No known cores")
return nil
}
t := table.NewWriter()
t.AppendHeader(table.Row{"Name", "Host", "Version", "Name", "ID"})
for name, host := range list {
if name == selected {
name = "*" + name
}
version := ""
corename := ""
coreid := ""
if u, err := url.Parse(host); err == nil {
host = u.Scheme + "://" + u.Host
version = u.Query().Get("version")
corename = u.Query().Get("name")
coreid = u.Query().Get("id")
}
t.AppendRow(table.Row{name, host, version, corename, coreid})
}
t.SetStyle(table.StyleLight)
fmt.Println(t.Render())
return nil
},
}
func init() {
coreCmd.AddCommand(listCoreCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// backupCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// backupCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}