/
depth.go
89 lines (62 loc) · 1.32 KB
/
depth.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
package cmd
import (
"fmt"
"os"
"github.com/spf13/cobra"
"github.com/hofstadter-io/cuetils/cmd/cuetils/flags"
"github.com/hofstadter-io/cuetils/structural"
)
var depthLong = `calculate the depth of file(s)`
func DepthRun(globs []string) (err error) {
// you can safely comment this print out
// fmt.Println("not implemented")
depths, err := structural.DepthGlobs(globs, &flags.RootPflags)
if err != nil {
return err
}
for _, d := range depths {
fmt.Println(d.Filename, d.Depth)
}
return err
}
var DepthCmd = &cobra.Command{
Use: "depth [files...]",
Short: "calculate the depth of file(s)",
Long: depthLong,
PreRun: func(cmd *cobra.Command, args []string) {
},
Run: func(cmd *cobra.Command, args []string) {
var err error
// Argument Parsing
var globs []string
if 0 < len(args) {
globs = args[0:]
}
err = DepthRun(globs)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
},
}
func init() {
extra := func(cmd *cobra.Command) bool {
return false
}
ohelp := DepthCmd.HelpFunc()
ousage := DepthCmd.UsageFunc()
help := func(cmd *cobra.Command, args []string) {
if extra(cmd) {
return
}
ohelp(cmd, args)
}
usage := func(cmd *cobra.Command) error {
if extra(cmd) {
return nil
}
return ousage(cmd)
}
DepthCmd.SetHelpFunc(help)
DepthCmd.SetUsageFunc(usage)
}