-
Notifications
You must be signed in to change notification settings - Fork 199
/
main.go
103 lines (85 loc) · 2.41 KB
/
main.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
package main
import (
"os"
"github.com/gobuffalo/packr/v2"
"github.com/deislabs/porter/pkg/porter"
"github.com/spf13/cobra"
)
func main() {
cmd := buildRootCommand()
if err := cmd.Execute(); err != nil {
os.Exit(1)
}
}
func buildRootCommand() *cobra.Command {
p := porter.New()
cmd := &cobra.Command{
Use: "porter",
Short: "I am porter 👩🏽✈️, the friendly neighborhood CNAB authoring tool",
Example: ` porter create
porter build
porter install
porter uninstall`,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
// Enable swapping out stdout/stderr for testing
p.Out = cmd.OutOrStdout()
p.Err = cmd.OutOrStderr()
},
SilenceUsage: true,
}
cmd.PersistentFlags().BoolVar(&p.Debug, "debug", false, "Enable debug logging")
cmd.AddCommand(buildVersionCommand(p))
cmd.AddCommand(buildSchemaCommand(p))
cmd.AddCommand(buildRunCommand(p))
cmd.AddCommand(buildBundlesCommand(p))
cmd.AddCommand(buildMixinsCommand(p))
cmd.AddCommand(buildCredentialsCommand(p))
for _, alias := range buildBundleAliasCommands(p) {
cmd.AddCommand(alias)
}
// Hide the help command from the help text
cmd.SetHelpCommand(&cobra.Command{
Use: "help",
Hidden: true,
})
help := newHelptextBox()
usage, _ := help.FindString("usage.txt")
cmd.SetUsageTemplate(usage)
cobra.AddTemplateFunc("ShouldShowGroupCommands", ShouldShowGroupCommands)
cobra.AddTemplateFunc("ShouldShowGroupCommand", ShouldShowGroupCommand)
cobra.AddTemplateFunc("ShouldShowUngroupedCommands", ShouldShowUngroupedCommands)
cobra.AddTemplateFunc("ShouldShowUngroupedCommand", ShouldShowUngroupedCommand)
return cmd
}
func newHelptextBox() *packr.Box {
return packr.New("github.com/deislabs/porter/cmd/porter/helptext", "./helptext")
}
func ShouldShowGroupCommands(cmd *cobra.Command, group string) bool {
for _, child := range cmd.Commands() {
if ShouldShowGroupCommand(child, group) {
return true
}
}
return false
}
func ShouldShowGroupCommand(cmd *cobra.Command, group string) bool {
if cmd.Annotations["group"] == group {
return true
}
return false
}
func ShouldShowUngroupedCommands(cmd *cobra.Command) bool {
for _, child := range cmd.Commands() {
if ShouldShowUngroupedCommand(child) {
return true
}
}
return false
}
func ShouldShowUngroupedCommand(cmd *cobra.Command) bool {
if !cmd.IsAvailableCommand() {
return false
}
_, hasGroup := cmd.Annotations["group"]
return !hasGroup
}