-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathtemplates.go
More file actions
107 lines (86 loc) · 3.04 KB
/
Copy pathtemplates.go
File metadata and controls
107 lines (86 loc) · 3.04 KB
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
package cmd
import (
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"golang.org/x/term"
)
func getUsageTemplate() string {
return `Usage:{{if .Runnable}}
{{.UseLine}}{{end}}{{if .HasAvailableSubCommands}}
{{.CommandPath}} [command]{{end}}
{{if not .HasParent}}
Commands:
{{rpad "setup" 29}} Create your first feature flag using a step-by-step guide
{{rpad "config" 29}} View and modify specific configuration values
{{rpad "completion" 29}} Enable command autocompletion within supported shells
{{rpad "login" 29}} Log in to your LaunchDarkly account
{{rpad "signup" 29}} Create a new LaunchDarkly account
{{rpad "dev-server" 29}} Run a development server to serve flags locally
Common resource commands:
{{rpad "flags" 29}} List, create, and modify feature flags and their targeting
{{rpad "environments" 29}} List, create, and manage environments
{{rpad "projects" 29}} List, create, and manage projects
{{rpad "members" 29}} Invite new members to an account
{{rpad "segments" 29}} List, create, modify, and delete segments
{{rpad "sourcemaps" 29}} Manage sourcemaps for error monitoring
{{rpad "symbols" 29}} Manage symbol files for error monitoring
{{rpad "..." 29}} To see more resource commands, run 'ldcli resources'
Flags:
{{.LocalFlags.FlagUsages | trimTrailingWhitespaces}}
{{else}}{{if .HasAvailableSubCommands}}{{$cmds := .Commands}}
Available Commands:{{range $cmds}}{{if (or .IsAvailableCommand (eq .Name "help"))}}
{{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{end}}{{if .HasAvailableLocalFlags}}
Flags:
{{.LocalFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{if .HasInheritedFlags}}
Global flags:
{{.InheritedFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{if .HasAvailableSubCommands}}
Use "{{.CommandPath}} [command] --help" for more information about a command.{{end}}
{{end}}`
}
func HasRequiredFlags(cmd *cobra.Command) bool {
var numFlags int
cmd.LocalFlags().VisitAll(func(flag *pflag.Flag) {
_, ok := flag.Annotations["required"]
if ok {
numFlags += 1
}
})
return numFlags > 0
}
func HasOptionalFlags(cmd *cobra.Command) bool {
var numFlags int
cmd.LocalFlags().VisitAll(func(flag *pflag.Flag) {
_, ok := flag.Annotations["required"]
if !ok && flag.Name != "help" {
numFlags += 1
}
})
return numFlags > 0
}
func WrappedRequiredFlagUsages(cmd *cobra.Command) string {
flagSet := pflag.NewFlagSet("request", pflag.ExitOnError)
cmd.LocalFlags().VisitAll(func(flag *pflag.Flag) {
if _, ok := flag.Annotations["required"]; ok {
flagSet.AddFlag(flag)
}
})
return flagSet.FlagUsagesWrapped(getTerminalWidth())
}
func WrappedOptionalFlagUsages(cmd *cobra.Command) string {
flagSet := pflag.NewFlagSet("request", pflag.ExitOnError)
cmd.LocalFlags().VisitAll(func(flag *pflag.Flag) {
_, ok := flag.Annotations["required"]
if !ok && flag.Name != "help" {
flagSet.AddFlag(flag)
}
})
return flagSet.FlagUsagesWrapped(getTerminalWidth())
}
func getTerminalWidth() int {
var width int
width, _, err := term.GetSize(0)
if err != nil {
width = 80
}
return width
}