-
Notifications
You must be signed in to change notification settings - Fork 1
/
list.go
74 lines (58 loc) · 1.37 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
65
66
67
68
69
70
71
72
73
74
package profile
import (
"fmt"
"os"
"strings"
"text/tabwriter"
log "github.com/Sirupsen/logrus"
"github.com/juliengk/go-utils"
"github.com/kassisol/metadata/api/storage"
"github.com/kassisol/metadata/pkg/adf"
"github.com/spf13/cobra"
)
var profileListFilter []string
func newListCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "ls",
Aliases: []string{"list"},
Short: "List profiles",
Long: listDescription,
Run: runList,
}
flags := cmd.Flags()
flags.StringSliceVarP(&profileListFilter, "filter", "f", []string{}, "Filter output based on conditions provided")
return cmd
}
func runList(cmd *cobra.Command, args []string) {
defer utils.RecoverFunc()
if len(args) > 0 {
cmd.Usage()
os.Exit(-1)
}
cfg := adf.NewDaemon()
if err := cfg.Init(); err != nil {
log.Fatal(err)
}
s, err := storage.NewDriver("sqlite", cfg.App.Dir.Root)
if err != nil {
log.Fatal(err)
}
defer s.End()
filters := utils.ConvertSliceToMap("=", profileListFilter)
profiles := s.ListProfile(filters)
if len(profiles) > 0 {
w := tabwriter.NewWriter(os.Stdout, 20, 1, 2, ' ', 0)
fmt.Fprintln(w, "NAME\tDATAS")
for profile, datas := range profiles {
if len(datas) > 0 {
fmt.Fprintf(w, "%s\t%s\n", profile, strings.Join(datas, ", "))
} else {
fmt.Fprintf(w, "%s\n", profile)
}
}
w.Flush()
}
}
var listDescription = `
List profiles
`