Skip to content
This repository was archived by the owner on Sep 9, 2022. It is now read-only.

Commit c9c0fd8

Browse files
committed
build secman customize help
1 parent aa97054 commit c9c0fd8

File tree

1 file changed

+195
-0
lines changed

1 file changed

+195
-0
lines changed

cmd/secman/help.go

+195
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
package secman
2+
3+
import (
4+
"fmt"
5+
"bytes"
6+
"strings"
7+
8+
"github.com/spf13/cobra"
9+
"github.com/spf13/pflag"
10+
"github.com/scmn-dev/secman/ios"
11+
"github.com/scmn-dev/secman/tools"
12+
)
13+
14+
func rootUsageFunc(command *cobra.Command) error {
15+
command.Printf("Usage: %s", command.UseLine())
16+
17+
subcommands := command.Commands()
18+
19+
if len(subcommands) > 0 {
20+
command.Print("\n\nCommands:\n")
21+
for _, c := range subcommands {
22+
if c.Hidden {
23+
continue
24+
}
25+
26+
command.Printf(" %s\n", c.Name())
27+
}
28+
29+
return nil
30+
}
31+
32+
flagUsages := command.LocalFlags().FlagUsages()
33+
34+
if flagUsages != "" {
35+
command.Println("\n\nFlags:")
36+
command.Print(tools.Indent(dedent(flagUsages), " "))
37+
}
38+
39+
return nil
40+
}
41+
42+
func rootFlagErrorFunc(cmd *cobra.Command, err error) error {
43+
if err == pflag.ErrHelp {
44+
return err
45+
}
46+
47+
return &tools.FlagError{Err: err}
48+
}
49+
50+
var hasFailed bool
51+
52+
func HasFailed() bool {
53+
return hasFailed
54+
}
55+
56+
func nestedSuggestFunc(command *cobra.Command, arg string) {
57+
command.Printf("unknown command %q for %q\n", arg, command.CommandPath())
58+
59+
var candidates []string
60+
if arg == "help" {
61+
candidates = []string{"--help"}
62+
} else {
63+
if command.SuggestionsMinimumDistance <= 0 {
64+
command.SuggestionsMinimumDistance = 2
65+
}
66+
67+
candidates = command.SuggestionsFor(arg)
68+
}
69+
70+
if len(candidates) > 0 {
71+
command.Print("\nDid you mean this?\n")
72+
for _, c := range candidates {
73+
command.Printf("\t%s\n", c)
74+
}
75+
}
76+
77+
command.Print("\n")
78+
_ = rootUsageFunc(command)
79+
}
80+
81+
func isRootCmd(command *cobra.Command) bool {
82+
return command != nil && !command.HasParent()
83+
}
84+
85+
func rootHelpFunc(cs *ios.ColorScheme, command *cobra.Command, args []string) {
86+
if isRootCmd(command.Parent()) && len(args) >= 2 && args[1] != "--help" && args[1] != "-h" {
87+
nestedSuggestFunc(command, args[1])
88+
hasFailed = true
89+
return
90+
}
91+
92+
commands := []string{}
93+
94+
for _, c := range command.Commands() {
95+
if c.Short == "" {
96+
continue
97+
}
98+
99+
if c.Hidden {
100+
continue
101+
}
102+
103+
s := rpad(c.Name()+":", c.NamePadding()) + c.Short
104+
commands = append(commands, s)
105+
}
106+
107+
if len(commands) == 0 {
108+
commands = []string{}
109+
}
110+
111+
type helpEntry struct {
112+
Title string
113+
Body string
114+
}
115+
116+
helpEntries := []helpEntry{}
117+
118+
if command.Long != "" {
119+
helpEntries = append(helpEntries, helpEntry{"", command.Long})
120+
} else if command.Short != "" {
121+
helpEntries = append(helpEntries, helpEntry{"", command.Short})
122+
}
123+
124+
helpEntries = append(helpEntries, helpEntry{"USAGE", command.UseLine()})
125+
126+
if len(commands) > 0 {
127+
helpEntries = append(helpEntries, helpEntry{"COMMANDS", strings.Join(commands, "\n")})
128+
}
129+
130+
flagUsages := command.LocalFlags().FlagUsages()
131+
132+
if flagUsages != "" {
133+
helpEntries = append(helpEntries, helpEntry{"FLAGS", dedent(flagUsages)})
134+
}
135+
136+
if _, ok := command.Annotations["help:arguments"]; ok {
137+
helpEntries = append(helpEntries, helpEntry{"ARGUMENTS", command.Annotations["help:arguments"]})
138+
}
139+
140+
if command.Example != "" {
141+
helpEntries = append(helpEntries, helpEntry{"EXAMPLES", command.Example})
142+
}
143+
144+
helpEntries = append(helpEntries, helpEntry{"LEARN MORE", `
145+
Use 'secman <command> <subcommand> --help' for more information about a command.`})
146+
if _, ok := command.Annotations["help:tellus"]; ok {
147+
helpEntries = append(helpEntries, helpEntry{"TELL US", command.Annotations["help:tellus"]})
148+
}
149+
150+
out := command.OutOrStdout()
151+
152+
for _, e := range helpEntries {
153+
if e.Title != "" {
154+
fmt.Fprintln(out, cs.Bold(e.Title))
155+
fmt.Fprintln(out, tools.Indent(strings.Trim(e.Body, "\r\n"), " "))
156+
} else {
157+
fmt.Fprintln(out, e.Body)
158+
}
159+
160+
fmt.Fprintln(out)
161+
}
162+
}
163+
164+
func rpad(s string, padding int) string {
165+
template := fmt.Sprintf("%%-%ds ", padding)
166+
return fmt.Sprintf(template, s)
167+
}
168+
169+
func dedent(s string) string {
170+
lines := strings.Split(s, "\n")
171+
minIndent := -1
172+
173+
for _, l := range lines {
174+
if len(l) == 0 {
175+
continue
176+
}
177+
178+
indent := len(l) - len(strings.TrimLeft(l, " "))
179+
if minIndent == -1 || indent < minIndent {
180+
minIndent = indent
181+
}
182+
}
183+
184+
if minIndent <= 0 {
185+
return s
186+
}
187+
188+
var buf bytes.Buffer
189+
190+
for _, l := range lines {
191+
fmt.Fprintln(&buf, strings.TrimPrefix(l, strings.Repeat(" ", minIndent)))
192+
}
193+
194+
return strings.TrimSuffix(buf.String(), "\n")
195+
}

0 commit comments

Comments
 (0)