This repository has been archived by the owner on Jun 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
help.go
157 lines (136 loc) · 4.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
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package cli
import (
"fmt"
"io"
"os"
"strings"
"text/tabwriter"
"text/template"
"github.com/hortonworks/cb-cli/cli/utils"
"github.com/urfave/cli"
)
var StackTemplateDescription = `Template parameters to fill in the generated template:
userName: Name of the Ambari user
password: Password of the Ambari user
name: Name of the cluster
region: Region of the cluster
availabilityZone: Availability zone of the cluster, on AZURE it is the same as the region
blueprintName: Name of the selected blueprint
credentialName: Name of the selected credential
instanceGroups.group: Name of the instance group
instanceGroups.nodeCount: Number of nodes in the group
instanceGroups.template.instanceType: Name of the selected template
instanceGroups.template.volumeCount: Number of volumes
instanceGroups.template.volumeSize: Size of Volumes in Gb
stackAuthentication.publicKey: Public key
`
var AppHelpTemplate = `NAME:
Cloudbreak command line tool
USAGE:
{{if .UsageText}}{{.UsageText}}{{else}}{{.Name}} {{if .VisibleFlags}}[global options]{{end}}{{if .Commands}} command [command options]{{end}} {{if .ArgsUsage}}{{.ArgsUsage}}{{end}}{{end}}
{{if .Version}}{{if not .HideVersion}}
VERSION:
{{.Version}}
{{end}}{{end}}{{if len .Authors}}
AUTHOR(S):
{{range .Authors}}{{.}}{{end}}
{{end}}{{if .VisibleCommands}}
COMMANDS:{{range .VisibleCategories}}{{if .Name}}
{{.Name}}:{{end}}{{range .VisibleCommands}}
{{join .Names ", "}}{{"\t"}}{{.Usage}}{{end}}
{{end}}{{end}}{{if .VisibleFlags}}
GLOBAL OPTIONS:
{{range .VisibleFlags}}{{.}}
{{end}}{{end}}{{if .Copyright}}
COPYRIGHT:
{{.Copyright}}
{{end}}
`
var CommandHelpTemplate = `NAME:
Cloudbreak command line tool
USAGE:
{{.HelpName}}{{if .VisibleFlags}} [command options]{{end}} {{if .ArgsUsage}}{{.ArgsUsage}}{{end}}{{if .Category}}
CATEGORY:
{{.Category}}{{end}}{{if .Description}}
DESCRIPTION:
{{.Description}}{{end}}{{if .VisibleFlags}}{{if requiredFlags .Flags}}
REQUIRED OPTIONS:{{range requiredFlags .Flags}}
{{.}}{{end}}{{end}}{{if optionalFlags .Flags}}
OPTIONS:
{{range optionalFlags .Flags}}{{.}}
{{end}}{{end}}{{end}}
`
var SubCommandHelpTemplate = `NAME:
Cloudbreak command line tool
USAGE:
{{.HelpName}} command{{if .VisibleFlags}} [command options]{{end}} {{if .ArgsUsage}}{{.ArgsUsage}}{{end}} {{if .Description}}
DESCRIPTION:
{{.Description}}{{end}}
COMMANDS:{{range .VisibleCategories}}{{if .Name}}
{{.Name}}:{{end}}{{range .VisibleCommands}}
{{join .Names ", "}}{{"\t"}}{{.Usage}}{{end}}
{{end}}{{if .VisibleFlags}}{{if requiredFlags .Flags}}
REQUIRED OPTIONS:{{range requiredFlags .Flags}}
{{.}}{{end}}{{end}}{{if optionalFlags .Flags}}
OPTIONS:
{{range optionalFlags .Flags}}{{.}}
{{end}}{{end}}{{end}}
`
var HiddenAppHelpTemplate = `NAME:
Cloudbreak command line tool
USAGE:
{{if .UsageText}}{{.UsageText}}{{else}}{{.HelpName}} {{if .VisibleFlags}}[global options]{{end}}{{if .Commands}} command [command options]{{end}} {{if .ArgsUsage}}{{.ArgsUsage}}{{end}}{{end}}
{{if .Version}}{{if not .HideVersion}}
VERSION:
{{.Version}}
{{end}}{{end}}{{if len .Authors}}
AUTHOR(S):
{{range .Authors}}{{.}}{{end}}
{{end}}
COMMANDS:{{range hiddenCommands .}}
{{join .Names ", "}}{{"\t"}}{{.Usage}}{{end}}
{{if .VisibleFlags}}
GLOBAL OPTIONS:
{{range .VisibleFlags}}{{.}}
{{end}}{{end}}{{if .Copyright}}
COPYRIGHT:
{{.Copyright}}
{{end}}
`
func HiddenCommands(app cli.App) []cli.Command {
var commands []cli.Command
for _, command := range app.Commands {
if command.Hidden {
commands = append(commands, command)
}
}
return commands
}
func ShowHiddenCommands(c *cli.Context) error {
cli.AppHelpTemplate = HiddenAppHelpTemplate
if err := cli.ShowAppHelp(c); err != nil {
utils.LogErrorAndExit(err)
}
return nil
}
func PrintHelp(out io.Writer, templ string, data interface{}) {
funcMap := template.FuncMap{
"join": strings.Join,
"requiredFlags": requiredFlags,
"optionalFlags": optionalFlags,
"hiddenCommands": HiddenCommands,
}
w := tabwriter.NewWriter(out, 1, 8, 2, ' ', 0)
t := template.Must(template.New("help").Funcs(funcMap).Parse(templ))
if err := t.Execute(w, data); err != nil {
// If the writer is closed, t.Execute will fail, and there's nothing
// we can do to recover.
if os.Getenv("CLI_TEMPLATE_ERROR_DEBUG") != "" {
fmt.Fprintf(cli.ErrWriter, "CLI TEMPLATE ERROR: %#v\n", err)
}
return
}
if err := w.Flush(); err != nil {
utils.LogErrorAndExit(err)
}
}