forked from gardener/dependency-watchdog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelp.go
66 lines (57 loc) · 1.44 KB
/
help.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
// SPDX-FileCopyrightText: 2024 SAP SE or an SAP affiliate company and Gardener contributors
//
// SPDX-License-Identifier: Apache-2.0
package cmd
import (
"bufio"
"html/template"
"io"
"strings"
)
var (
cliHelpTemplate = `
NAME:
{{printf "%s - %s" .Name .ShortDesc}}
USAGE:
{{printf "\t%s" .UsageLine}}
{{if .LongDesc}}
DESCRIPTION:
{{printf "\t%s" .LongDesc}}
{{end}}
`
cliUsageTemplate = `dwd is a watch-dog which keeps an eye on kubernetes resources and uses a pre-defined configuration
to scale up, scale down or stop pods (forcing a restart) based on watches/probes which monitor the health/reachability of defined
kubernetes resources.
Usage:
<command> [arguments]
Supported commands:
{{range .}}
{{printf "\t%s: " .Name}} {{.ShortDesc}}
{{end}}
`
)
// PrintHelp prints out the help text for the passed in command
func PrintHelp(cmdName string, w io.Writer) {
if strings.TrimSpace(cmdName) == "" {
PrintCliUsage(w)
return
}
for _, cmd := range Commands {
if cmdName == cmd.Name {
executeTemplate(w, cliHelpTemplate, cmd)
return
}
}
}
// PrintCliUsage prints the CLI usage text to the passed io.Writer
func PrintCliUsage(w io.Writer) {
bufW := bufio.NewWriter(w)
executeTemplate(w, cliUsageTemplate, Commands)
_ = bufW.Flush()
}
func executeTemplate(w io.Writer, tmplText string, tmplData interface{}) {
tmpl := template.Must(template.New("usage").Parse(tmplText))
if err := tmpl.Execute(w, tmplData); err != nil {
panic(err)
}
}