/
cmd.go
96 lines (75 loc) 路 1.7 KB
/
cmd.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package bucket
import (
"fmt"
"os"
"github.com/jjzcru/hog/pkg/hog"
"github.com/jjzcru/hog/pkg/utils"
"github.com/olekukonko/tablewriter"
"github.com/spf13/cobra"
)
// Command returns a cobra command for `bucket` sub command
func Command() *cobra.Command {
cmd := &cobra.Command{
Use: "bucket",
Short: "Display the buckets and their files",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
err := run()
if err != nil {
utils.PrintError(err)
return
}
},
}
return cmd
}
func run() error {
hogPath, err := hog.Path()
if err != nil {
return err
}
h, err := getHog(hogPath)
if err != nil {
return err
}
data := transform(h)
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Bucket", "Files"})
table.SetRowLine(true)
table.SetAutoMergeCells(true)
table.SetAutoWrapText(false)
table.SetHeaderColor(
tablewriter.Colors{tablewriter.Bold, tablewriter.FgHiWhiteColor},
tablewriter.Colors{tablewriter.Bold, tablewriter.FgHiWhiteColor},
)
table.SetColumnColor(
tablewriter.Colors{tablewriter.FgCyanColor},
tablewriter.Colors{tablewriter.FgHiWhiteColor},
)
table.AppendBulk(data)
table.Render()
return nil
}
func getHog(hogPath string) (hog.Hog, error) {
var h hog.Hog
if !utils.IsPathExist(hogPath) {
return h, fmt.Errorf("hog path '%s' do not exist", hogPath)
}
h, err := hog.FromPath(hogPath)
if err != nil {
return h, err
}
return h, nil
}
func transform(h hog.Hog) [][]string {
var table [][]string
for id, files := range h.Buckets {
if len(files) == 0 {
table = append(table, []string{id, "EMPTY"})
}
for _, file := range files {
table = append(table, []string{id, file})
}
}
return table
}