-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboa.go
125 lines (104 loc) · 3.72 KB
/
boa.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package boa
import (
"os"
"text/tabwriter"
"github.com/spf13/cobra"
)
type (
// Option is used to define multiple positional args in which the positional
// args can have a description. Aliases for the args can be added to the Args
// slice.
Option struct {
Args []string
Desc string
}
// Profile is used to bundle multiple options as a single option
Profile struct {
Args []string
Opts []string
Desc string
}
// Command is a wrapper for the cobra Command that adds additional fields to
// support better usage, help, etc.
Command struct {
*cobra.Command
Opts []Option
Profiles []Profile
}
)
// Build returns a boa Command from a BoaCmdBuilder
func (b Command) ToBuilder() *BoaCmdBuilder {
return &BoaCmdBuilder{
NewCobraCmd(b.Use),
&b,
}
}
// UsageFunc overrides the default UsageFunc used by boa to facilitate showing
// a custom usage template
func (c Command) UsageFunc(template string) func(*cobra.Command) error {
return func(cmd *cobra.Command) error {
w := tabwriter.NewWriter(os.Stdout, 8, 8, 8, ' ', 0)
err := tmpl(w, template, c)
if err != nil {
cmd.PrintErrln(err)
}
return err
}
}
// HelpFunc overrides the default HelpFunc used by cobra to facilitate showing
// a custom help template
func (c Command) HelpFunc(template string) func(*cobra.Command, []string) {
return func(cmd *cobra.Command, s []string) {
w := tabwriter.NewWriter(os.Stdout, 3, 3, 3, ' ', 0)
err := tmpl(w, template, c)
if err != nil {
cmd.PrintErrln(err)
}
}
}
// OptionsTemplate is used to override the cobra UsageTemplate to facilitate
// options and other CLI parameters
func (c Command) OptionsTemplate() string {
return `Usage:{{if .Runnable}}
{{.UseLine}}{{end}}{{if .HasOptions}} [options]{{end}}{{if .HasAvailableSubCommands}}
{{.CommandPath}} [command]{{end}}{{if gt (len .Aliases) 0}}
Aliases:
{{.NameAndAliases}}{{end}}{{if .HasExample}}
Examples:
{{.Example}}{{end}}{{if .HasAvailableSubCommands}}{{$cmds := .Commands}}{{if eq (len .Groups) 0}}
Available Commands:{{range $cmds}}{{if (or .IsAvailableCommand (eq .Name "help"))}}
{{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{else}}{{range $group := .Groups}}
{{.Title}}{{range $cmds}}{{if (and (eq .GroupID $group.ID) (or .IsAvailableCommand (eq .Name "help")))}}
{{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{end}}{{if not .AllChildCommandsHaveGroup}}
Additional Commands:{{range $cmds}}{{if (and (eq .GroupID "") (or .IsAvailableCommand (eq .Name "help")))}}
{{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{end}}{{end}}{{end}}{{if .HasOptions}}
Options:{{range .Opts }}
{{.Args | sliceToCsv}} {{.Desc}}{{end}}{{end}}{{if .HasProfiles}}
Profiles:{{range .Profiles }}
{{.Args | sliceToCsv}} {{.Desc}}
↳ Options: {{.Opts | sliceToCsv}}{{end}}{{end}}{{if .HasAvailableLocalFlags}}
Flags:
{{.LocalFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{if .HasAvailableInheritedFlags}}
Global Flags:
{{.InheritedFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{if .HasHelpSubCommands}}
Additional help topics:{{range .Commands}}{{if .IsAdditionalHelpTopicCommand}}
{{rpad .CommandPath .CommandPathPadding}} {{.Short}}{{end}}{{end}}{{end}}{{if .HasAvailableSubCommands}}
Use "{{.CommandPath}} [command] --help" for more information about a command.{{end}}
`
}
// HasOptions returns whether the boa Command has any options defined; this is
// primary used for templating purposes.
func (c Command) HasOptions() bool {
if c.Opts == nil || len(c.Opts) == 0 {
return false
}
return true
}
// HasProfiles returns whether the boa Command has any profiles defined; this is
// primary used for templating purposes.
func (c Command) HasProfiles() bool {
if c.Profiles == nil || len(c.Profiles) == 0 {
return false
}
return true
}