This repository was archived by the owner on Aug 9, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.go
More file actions
224 lines (199 loc) · 9.22 KB
/
Copy pathcli.go
File metadata and controls
224 lines (199 loc) · 9.22 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
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package main
import (
"flag"
"fmt"
"rectx/utilities"
)
var (
// rectx new [optional]
newCmd = flag.NewFlagSet("new", flag.ExitOnError)
projectNameFlag string // --name
authorFlag string // --author
templateFlag string // --template
pathFlag string // --path
licenseFlag string // --license
versionFlag string // --version
noPromptFlag bool // ---no-prompt
// rectx build [optional]
buildCmd = flag.NewFlagSet("build", flag.ExitOnError)
buildProfileFlag string // --profile
// rectx run [optional]
runCmd = flag.NewFlagSet("run", flag.ExitOnError)
runProfileFlag string // --profile
// rectx template <subcommand> [optional]
templateCmd = flag.NewFlagSet("template", flag.ExitOnError)
templateSubcommands = []string{"list", "add", "snapshot", "setDefault", "rename"}
templateSubcommandDetails = []string{
"Lists all the templates in the rectx config directory.",
"Adds a new template file (.rectx.template required).",
"Reads the folders/files of a directory and generates a .rectx.template file.",
"Set a default template that will be auto selected for your projects.",
"Change the name of a template.",
}
templateSubcommandArguments = []string{
"(None)", "path/to/file", "path/to/folder", "template-name", "template-name new-template-name",
}
// rectx config <subcommand> [optional]
configCmd = flag.NewFlagSet("config", flag.ExitOnError)
configSubcommands = []string{"validate", "regenerate", "reset", "set"}
configSubcommandDetails = []string{
"Checks rectx global config data does not contain any errors.",
"Downloads any missing rectx config data.",
"Reset a value to it's default in the rectx global config.",
"Change a value in the rectx global config.",
}
configSubcommandArguments = []string{
"(None)", "(None)", "key-name", "key-name new-key-value",
}
configFileFlag bool // --config
templatesFlag bool // --templates
licensesFlag bool // --licenses
allFlag bool // --all
helpFlag bool
CMDS = [...]*flag.FlagSet{newCmd, buildCmd, runCmd, templateCmd, configCmd}
// Used for spacing (7 spaces)
megaSpace = " "
)
// This function initalises all flags and sets the help flag for each command
func initFlags() {
initNewFlags()
initBuildFlags()
initRunFlags()
initConfigFlags()
for _, cmd := range CMDS {
cmd.BoolVar(&helpFlag, "help", false, "Shows a specific help message for the command used.")
cmd.BoolVar(&utilities.DebugFlag, "xx", false, "Shows full error information when there is a error.")
}
}
// This function intitalises all of the rectx new command flags variables in declared above.
func initNewFlags() {
newCmd.StringVar(&projectNameFlag, "name", "Untitled", "Specify what you want your project to be called.")
newCmd.StringVar(&authorFlag, "author", "", "Specify who is creating the project.")
newCmd.StringVar(&templateFlag, "template", "default", "Specify how you want the project structure to look.")
newCmd.StringVar(&pathFlag, "path", "Untitled", "Specify where to put the project.")
newCmd.StringVar(&licenseFlag, "license", "", "Specify which license you want your project to use.")
newCmd.StringVar(&versionFlag, "version", "0.1.0", "Specify what version to start the project at.")
newCmd.BoolVar(&noPromptFlag, "noPrompt", false, "Don't show the project prompt (generate based off defaults and provided flags).")
}
// This function intitalises all of the rectx config command flags variables in declared above.
func initConfigFlags() {
configCmd.BoolVar(&configFileFlag, "config", false, "Specifies the rectx config file specifically.")
configCmd.BoolVar(&templatesFlag, "templates", false, "Specifies the rectx templates specifically.")
configCmd.BoolVar(&licensesFlag, "licenses", false, "Specifies the rectx licenses specifically.")
configCmd.BoolVar(&allFlag, "all", false, "Specifically validate/regenerate the entire rectx config directory.")
}
// This function intitalises all of the rectx build command flags variables in declared above.
func initBuildFlags() {
buildCmd.StringVar(&buildProfileFlag, "profile", "", "Specify a custom build profile for the project (must be declared in the project.rectx).")
}
// This function intitalises all of the rectx run command flags variables in declared above.
func initRunFlags() {
runCmd.StringVar(&runProfileFlag, "profile", "", "Specify a custom run profile for the project (must be declared in the project.rectx).")
}
// Shows the usage for a certain command.
// Not all commands have subcommands, so to show subcommands use the `showSubcommands` parameter.
func ShowUsage(command string, showSubcommands bool) {
if showSubcommands {
command += " <subcommand>"
}
fmt.Printf("\n Usage: rectx %s [flags] <arguments>\n", command)
}
// Shows the general help menu. This is very large so I try to avoid using it.
// There are command specific help menus which I think are better.
func ShowHelpMenu() {
ShowUsage("<command>", true)
for _, command := range CMDS {
name := command.Name()
if name == "template" || name == "config" {
name += " [subcommand]"
}
fmt.Printf("\n> rectx %s [flags] [arguments]\n\n", name)
if name == "template [subcommand]" {
fmt.Printf(" [subcommands]\n")
for i, c := range templateSubcommands {
fmt.Printf(" %s\n", c)
fmt.Printf(" %s\n", templateSubcommandDetails[i])
}
fmt.Println()
} else if name == "config [subcommand]" {
fmt.Printf(" [subcommands]\n")
for i, c := range configSubcommands {
fmt.Printf(" %s\n", c)
fmt.Printf(" %s\n", configSubcommandDetails[i])
}
fmt.Println()
}
fmt.Println(" [flags]")
command.PrintDefaults()
}
}
// The new command help menu. This help menu is specific to the new command.
// This means it will only show new command flags.
func ShowNewHelpMenu() {
ShowUsage("new", false)
fmt.Printf(
"\n [details]\n Used to create a new project! \n This command will prompt you questions about your project" +
"and then generate all the project files you need to get started." +
"\n Optionally, you can pass flags such as --name=\"borgor\" to quickly assign values without the prompt!" +
"\n You can also add default values for authorFlag, license, template, which will make the prompt much faster to fill out!\n\n")
fmt.Println(" [flags]")
newCmd.PrintDefaults()
}
// The run command help menu. This help menu is specific to the run command.
// This means it will only show run command flags.
func ShowRunHelpMenu() {
ShowUsage("run", false)
fmt.Printf("\n [details]\n Runs the project's source code. \n This function will run the executable found in the project." +
"\n If no executable exists, or if edits have been made to the project's source code since the last build, " +
"\n then this command will automatically build/re-build the executable.\n\n")
fmt.Println(" [flags]")
runCmd.PrintDefaults()
}
// The build command help menu. This help menu is specific to the build command.
// This means it will only show build command flags.
func ShowBuildHelpMenu() {
ShowUsage("build", false)
fmt.Printf("\n [details]\n Builds the project's source code. \n This function will build an executable but will not run the executable." +
"\n If you wish to run the executable then please check out \"rectx run --help\".\n\n")
fmt.Println(" [flags]")
buildCmd.PrintDefaults()
}
// The template command help menu. This help menu is specific to the template command.
// This means it will only show template command flags and subcommands.
func ShowTemplateHelpMenu() {
ShowUsage("template", true)
fmt.Printf("\n [details]\n Manage project generation templates." +
"\n These templates describe how a project is to be generated." +
"\n You can easily create you own template using a simple syntax." +
"\n Defining your own template allows you to work on projects with a structure you enjoy and are familiar with!\n\n")
PrintSubcommands(templateSubcommands, templateSubcommandDetails, templateSubcommandArguments)
fmt.Printf("\n [flags]\n")
templateCmd.PrintDefaults()
}
// The config command help menu. This help menu is specific to the config command.
// This means it will only show config command flags and subcommands.
func ShowConfigHelpMenu() {
ShowUsage("config", true)
fmt.Printf("\n [details]\n Manage the rectx global config." +
"\n This subcommands has little to do with project.rectx files." +
"\n This is for the global rectx config used to configure rectx itself." +
"\n Most importantly, the config subcommands can be used to fix rectx issues with templates, licenses, and more.\n\n")
PrintSubcommands(configSubcommands, configSubcommandDetails, configSubcommandArguments)
fmt.Println("\n [flags]")
configCmd.PrintDefaults()
}
func PrintSubcommands(subcommands, details []string, arguments []string) {
fmt.Println(" [subcommands]")
for i, command := range subcommands {
fmt.Printf(" %s\n%sArguments: %s\n%s%s\n", command, megaSpace, arguments[i], megaSpace, details[i])
}
}
func handleParseErrorAndHelpFlag(command *flag.FlagSet, err error, helpMenu func()) {
if err != nil {
name := command.Name()
fmt.Println("An unexpected error occurred during flag parsing!")
fmt.Printf("Please try \"rectx %s --help\" for more information on the %s command!\n", name, name)
} else if helpFlag {
helpMenu()
}
}