/
ls.go
60 lines (52 loc) · 1.25 KB
/
ls.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
package cmd
import (
"os"
"github.com/lunarway/shuttle/pkg/config"
"github.com/lunarway/shuttle/pkg/ui"
"github.com/spf13/cobra"
)
const lsDefaultTempl = `
{{- $max := .Max -}}
Available Scripts:
{{- range $key, $value := .Scripts}}
{{rightPad $key $max }} {{upperFirst $value.Description}}
{{- end}}
`
var (
lsFlagTemplate string
)
type templData struct {
Scripts map[string]config.ShuttlePlanScript
Max int
}
var lsCmd = &cobra.Command{
Use: "ls [command]",
Short: "List possible commands",
Run: func(cmd *cobra.Command, args []string) {
context := getProjectContext()
var templ string
if lsFlagTemplate != "" {
templ = lsFlagTemplate
} else {
templ = lsDefaultTempl
}
err := ui.Template(os.Stdout, "ls", templ, templData{
Scripts: context.Scripts,
Max: calculateRightPadForKeys(context.Scripts),
})
context.UI.CheckIfError(err)
},
}
func init() {
lsCmd.Flags().StringVar(&lsFlagTemplate, "template", "", "Template string to use. The template format is golang templates [http://golang.org/pkg/text/template/#pkg-overview].")
rootCmd.AddCommand(lsCmd)
}
func calculateRightPadForKeys(m map[string]config.ShuttlePlanScript) int {
max := 10
for k := range m {
if max < len(k) {
max = len(k)
}
}
return max + 2
}