/
usage.go
113 lines (87 loc) · 2.21 KB
/
usage.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
// Copyright 2022 Jetpack Technologies Inc and contributors. All rights reserved.
// Use of this source code is governed by the license in the LICENSE file.
package envcli
import (
"github.com/MakeNowJust/heredoc"
"github.com/spf13/cobra"
"go.jetpack.io/envsec/internal/tux"
)
// TODO: move to file
var usageTmpl = heredoc.Doc(`
{{ "Usage:" | style "h2" }}
{{if .Runnable}}{{.UseLine | style "command" }}{{end}}
{{- if .HasAvailableSubCommands}} {{"<command>" | style "subcommand"}}{{end}}
{{- if gt (len .Aliases) 0}}
{{ "Aliases:" | style "h2" }}
{{.NameAndAliases}}
{{- end}}
{{- if .HasExample}}
{{ "Examples:" | style "h2" }}
{{.Example}}
{{- end}}
{{- if .HasAvailableSubCommands}}
{{ "Available Commands:" | style "h2" }}
{{- range .Commands}}
{{- if (or .IsAvailableCommand (eq .Name "help"))}}
{{rpad .Name .NamePadding | style "subcommand"}} {{.Short}}
{{- end}}
{{- end}}
{{- end}}
{{- if .HasAvailableLocalFlags}}
{{ "Flags:" | style "h2" }}
{{ .LocalFlags.FlagUsages | trimTrailingWhitespaces}}
{{- end}}
{{- if .HasAvailableInheritedFlags}}
{{ "Global Flags:" | style "h2" }}
{{.InheritedFlags.FlagUsages | trimTrailingWhitespaces}}
{{- end}}
{{- if .HasHelpSubCommands}}
{{ "Additional help topics:" | style "h2" }}
{{- range .Commands}}
{{- if .IsAdditionalHelpTopicCommand}}
{{rpad .CommandPath .CommandPathPadding}} {{.Short}}
{{- end}}
{{- end}}
{{- end}}
{{- if .HasAvailableSubCommands}}
Use "{{.CommandPath}} [command] --help" for more information about a command.
{{- end}}
`)
var baseStyle = tux.StyleSheet{
Styles: map[string]tux.StyleRule{
"h1": {
Bold: true,
Foreground: "$purple",
},
"h2": {
Bold: true,
// Foreground: "$purple",
},
"command": {
Foreground: "$cyan",
},
"subcommand": {
Foreground: "$magenta",
},
"flag": {
Bold: true,
Foreground: "$purple",
},
},
Tokens: map[string]string{
"$purple": "#bd93f9",
"$yellow": "#ffb86c",
"$cyan": "51",
"$magenta": "#ff79c6",
"$green": "#50fa7b",
},
}
func UsageFunc(cmd *cobra.Command) error {
t := tux.New()
t.SetIn(cmd.InOrStdin())
t.SetOut(cmd.OutOrStdout())
t.SetErr(cmd.ErrOrStderr())
t.SetStyleSheet(baseStyle)
t.PrintT(usageTmpl, cmd)
return nil
}