/
register.go
186 lines (152 loc) · 5.33 KB
/
register.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package cmd
import (
"encoding/json"
"fmt"
"os"
"github.com/nikhilsbhat/terragen/pkg/gen"
"github.com/nikhilsbhat/terragen/version"
"github.com/spf13/cobra"
)
var (
createCommands map[string]*cobra.Command
editCommands map[string]*cobra.Command
generateInput gen.Input
)
type terragenCommands struct {
commands []*cobra.Command
}
// SetTerragenCommands helps in gathering all the subcommands so that it can be used while registering it with main command.
func SetTerragenCommands() *cobra.Command {
return getTerragenCommands()
}
// Add an entry in below function to register new command.
func getTerragenCommands() *cobra.Command {
command := new(terragenCommands)
command.commands = append(command.commands, getCreateCommand())
command.commands = append(command.commands, getVersionCommand())
command.commands = append(command.commands, getEditCommand())
return command.prepareCommands()
}
func (c *terragenCommands) prepareCommands() *cobra.Command {
rootCmd := getRootCommand()
for _, cmnd := range c.commands {
rootCmd.AddCommand(cmnd)
}
rootCmd.SilenceErrors = true
registerFlags("terragen", rootCmd)
return rootCmd
}
func getRootCommand() *cobra.Command {
rootCommand := &cobra.Command{
Use: "terragen [command]",
Short: "Utility that helps in generating scaffolds for terraform provider",
Long: `Terragen helps user to create custom terraform provider and its components by generating scaffolds.`,
PreRunE: setCLI,
RunE: echoTerragen,
}
rootCommand.SetUsageTemplate(getUsageTemplate())
return rootCommand
}
func getCreateCommand() *cobra.Command {
createCommand := &cobra.Command{
Use: "create [command] [flags]",
Short: "Command to scaffold provider and other components of terraform provider",
Long: `This will help user to generate the initial components of terraform provider.`,
PreRunE: setCLI,
RunE: echoTerragen,
}
registerFlags("create", createCommand)
createCommand.SetUsageTemplate(getUsageTemplate())
for _, command := range createCommands {
command.SilenceUsage = true
createCommand.AddCommand(command)
}
return createCommand
}
func getEditCommand() *cobra.Command {
editCommand := &cobra.Command{
Use: "edit [command] [flags]",
Short: "Command to edit the scaffold created for a provider",
Long: `This will help user to edit the scaffolds generated for terraform provider and other components of them.`,
PreRunE: setCLI,
RunE: echoTerragen,
}
registerFlags("edit", editCommand)
editCommand.SetUsageTemplate(getUsageTemplate())
for _, command := range editCommands {
command.SilenceUsage = true
editCommand.AddCommand(command)
}
return editCommand
}
func getVersionCommand() *cobra.Command {
return &cobra.Command{
Use: "version [flags]",
Short: "Command to fetch the version of terragen installed",
Long: `This will help user to find what version of terragen he/she installed in her machine.`,
RunE: versionConfig,
}
}
func echoTerragen(cmd *cobra.Command, _ []string) error {
return cmd.Usage()
}
func setCLI(_ *cobra.Command, _ []string) error {
InitLogger(cliLogLevel)
return nil
}
func versionConfig(_ *cobra.Command, _ []string) error {
buildInfo, err := json.Marshal(version.GetBuildInfo())
if err != nil {
cliLogger.Errorf("fetching version information of terragen errored with: %s", err.Error())
os.Exit(1)
}
fmt.Printf("terragen version: %s\n", string(buildInfo))
return nil
}
// The function that helps in registering the subcommands with the respective main command.
// Make sure you call this, and this is the only way to register the subcommands.
func createRegister(name string, flagsRequired bool, fn *cobra.Command) {
if createCommands == nil {
createCommands = make(map[string]*cobra.Command)
}
if createCommands[name] != nil {
panic(fmt.Sprintf("Command %s is already registered", name))
}
if flagsRequired {
registerFlags(name, fn)
}
createCommands[name] = fn
}
func editRegister(name string, flagsRequired bool, fn *cobra.Command) {
if editCommands == nil {
editCommands = make(map[string]*cobra.Command)
}
if editCommands[name] != nil {
panic(fmt.Sprintf("Command %s is already registered", name))
}
if flagsRequired {
registerFlags(name, fn)
}
editCommands[name] = fn
}
// This function will return the custom template for usage function,
// only functions/methods inside this package can call this.
func getUsageTemplate() string {
return `Usage:{{if .Runnable}}
{{.UseLine}}{{end}}{{if gt (len .Aliases) 0}}{{printf "\n" }}
Aliases:
{{.NameAndAliases}}{{end}}{{if .HasExample}}{{printf "\n" }}
Examples:
{{.Example}}{{end}}{{if .HasAvailableSubCommands}}{{printf "\n"}}
Available Commands:{{range .Commands}}{{if (or .IsAvailableCommand (eq .Name "help"))}}
{{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{end}}{{if .HasAvailableLocalFlags}}{{printf "\n"}}
Flags:
{{.LocalFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{if .HasAvailableInheritedFlags}}{{printf "\n"}}
Global Flags:
{{.InheritedFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{if .HasHelpSubCommands}}{{printf "\n"}}
Additional help topics:{{range .Commands}}{{if .IsAdditionalHelpTopicCommand}}
{{rpad .CommandPath .CommandPathPadding}} {{.Short}}{{end}}{{end}}{{end}}
{{if .HasAvailableSubCommands}}{{printf "\n"}}
Use "{{.CommandPath}} [command] --help" for more information about a command.{{end}}
{{printf "\n"}}`
}