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 1
/
Copy pathmain.go
133 lines (113 loc) · 4.85 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
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
package main
import (
"fmt"
"log"
"os"
"rectx/config"
"rectx/project_manager"
"rectx/templates"
"strings"
)
func main() {
initFlags()
if len(os.Args) < 2 {
ShowHelpMenu()
os.Exit(0)
}
switch os.Args[1] {
// rectx new|build|run
// These commands have no subcommands and their flags are handled in the project_manager module.
case "new":
handleParseErrorAndHelpFlag(newCmd, newCmd.Parse(os.Args[2:]), ShowNewHelpMenu)
project_manager.New()
case "build":
handleParseErrorAndHelpFlag(buildCmd, buildCmd.Parse(os.Args[2:]), ShowBuildHelpMenu)
project_manager.Build()
case "run":
handleParseErrorAndHelpFlag(runCmd, runCmd.Parse(os.Args[2:]), ShowRunHelpMenu)
project_manager.Run()
// rectx template|templates
// I allow both "templates" and "template" because both go well with the subcommands and I kept putting the wrong one.
case "templates":
fallthrough
case "template":
// TODO debug flag not working for template subcommands - Issue should probably be created.
handleParseErrorAndHelpFlag(templateCmd, templateCmd.Parse(os.Args[2:]), ShowTemplateHelpMenu)
EnsureArguments(3, "template")
switch os.Args[2] {
// `rectx template list` lists all the templates in the rectx template directory
case "list":
fmt.Println("Listing all templates found:")
for i, files := range templates.ListTemplates() {
fmt.Printf("%d. %s\n", i+1, files)
}
// `rectx template add <path/to/template>` copies a .rectx.template into the rectx template directory
// Although this is a template subcommand, it uses the config package because it manages the ~/.rectx config directory where the templates are stored.
case "add":
EnsureArguments(4, "template add")
config.AddTemplate(os.Args[3])
// `rectx template test <template>` parses and generates using a template file in a temporary directory.
// This command was designed to allow you to test for errors in a template file. Errors are reported to the command line.
case "test":
EnsureArguments(4, "template test")
templates.Test(os.Args[3])
// `rectx template default <template>` sets the provided template as the default template for project generation.
// This means if you don't select a template this template will be used. The default template is `default.rect.template`.
// Although this is a template subcommand, it uses the config package because that package manages the rectx config where the default template is stored.
case "default":
EnsureArguments(4, "template default")
config.SetDefaultTemplate(os.Args[3])
// `rectx template snapshot <path>` generates a .rectx.template file from the information provided in the directory provided.
// NOTE: the template name will be taken from the directory name, commands should be held within a file called "commands"
case "snapshot":
// TODO maybe an exclude flag should be added so people can avoid files named "commands" that have other purposes?
EnsureArguments(4, "template snapshot")
templates.Snapshot(os.Args[3])
// `rectx template rename <templateName> <newTemplateName>`
case "rename":
EnsureArguments(5, "template rename")
config.RenameTemplate(os.Args[3], os.Args[4])
default:
fmt.Printf("Unknown subcommand \"%s\"! Maybe try rectx templates --help for a list of subcommands...\n", os.Args[2])
}
// rectx config
// TODO I should probably start this before it's too late - Tokorv
case "config":
handleParseErrorAndHelpFlag(configCmd, configCmd.Parse(os.Args[2:]), ShowConfigHelpMenu)
case "help":
fallthrough
case "--help":
fallthrough
case "-h":
ShowHelpMenu()
default:
log.Fatalf("Unknown command \"%s\"!\nIf you're looking for a certain command try \"rectx --help\"!\n", os.Args[1])
}
}
// This function is used to check if the required number of arguments is met.
// If so, the function will do nothing, otherwise it will display an error message, and a usage for the command provided.
// If only a command that requires a subcommand is supplied (via command argument) then a <subcommand> will be displayed
// in the usage message. Otherwise, only <arg> symbols will be displayed.
func EnsureArguments(requiredArgumentCount int, command string) {
if requiredArgumentCount > len(os.Args) {
argumentCount := requiredArgumentCount - len(os.Args)
displayUsageWithSubcommand := false
fmt.Printf("Not enough arguments to run \"rectx %s\"!\n", command)
fmt.Print("Usage: rectx ")
// We print a <subcommand> only if command is a command that requires subcommands.
// Otheriwse, we can just display the actual command + subcommand used.
for _, COMMAND := range []string{"template", "templates", "config"} {
if command == COMMAND {
fmt.Print(command + " <subcommand> ")
displayUsageWithSubcommand = true
}
}
if displayUsageWithSubcommand {
argumentCount--
} else {
fmt.Print(command)
}
fmt.Printf(" %s\n", strings.Repeat("<arg> ", argumentCount))
os.Exit(1)
}
}