-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
170 lines (157 loc) · 4.39 KB
/
Copy pathmain.go
File metadata and controls
170 lines (157 loc) · 4.39 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
package main
import (
"fmt"
"maps"
"os"
"slices"
)
const (
initEnvCmd = "init-env"
initReposCmd = "init-repos"
listReposCmd = "list-repos"
cloneRepoCmd = "clone-repo"
cloneAllReposCmd = "clone-all-repos"
updateDocTagsCmd = "update-doc-tags"
removeSolutionTagsCmd = "remove-solution-tags"
genReadmeCmd = "gen-readme"
genTestsJSONCmd = "gen-tests-json"
renameLegacyTestsCmd = "rename-tests"
addLintCheckersCmd = "add-lint-checkers"
addMainTestsCmd = "add-main-tests"
convertYamlToJSONCmd = "convert-yaml-to-json"
helpCmd = "help"
)
// Command represents a single command with its description and function
type Command struct {
Description string
FlagUsage string
Function func([]string)
}
// getCommands returns all available commands
func getCommands() map[string]Command {
return map[string]Command{
initEnvCmd: {
Description: "Initialize environment variables for the course",
FlagUsage: "-year <year> -name <course-name> [-course <course-code>] [-discord-join-url <url>] [-bot-user <username>]",
Function: initEnv,
},
initReposCmd: {
Description: "Initialize course repositories (assignments, website, tests)",
FlagUsage: "",
Function: initRepos,
},
listReposCmd: {
Description: "List all student and group repositories",
FlagUsage: "[-url]",
Function: listRepos,
},
cloneRepoCmd: {
Description: "Clone a specified repository or current user's repository",
FlagUsage: "[-repo <repo-name>] [-pull]",
Function: cloneRepo,
},
cloneAllReposCmd: {
Description: "Clone all student and group repositories",
FlagUsage: "",
Function: cloneAllRepos,
},
updateDocTagsCmd: {
Description: "Update documentation tags in markdown files",
FlagUsage: "-repo <assignments|tests|website>",
Function: updateDocTags,
},
removeSolutionTagsCmd: {
Description: "Remove solution build tags from Go files",
FlagUsage: "-repo <assignments|tests>",
Function: removeSolutionTags,
},
genReadmeCmd: {
Description: "Generate README.md files from readme_tmpl.md templates",
FlagUsage: "",
Function: genReadme,
},
genTestsJSONCmd: {
Description: "Generate tests.json files for lab assignments",
FlagUsage: "[-view] -labs <lab1>",
Function: genTestsJSON,
},
renameLegacyTestsCmd: {
Description: "Rename legacy *_ag_test.go files to *_qf_test.go",
FlagUsage: "",
Function: renameLegacyTests,
},
addLintCheckersCmd: {
Description: "Add linter test files to specified lab folder",
FlagUsage: "-labs <lab1>",
Function: addLintCheckers,
},
addMainTestsCmd: {
Description: "Add main test files to directories with existing test files",
FlagUsage: "",
Function: addMainTests,
},
convertYamlToJSONCmd: {
Description: "Convert assignment.yml/yaml files to assignment.json",
FlagUsage: "",
Function: convertYamlToJSON,
},
helpCmd: {
Description: "Show help for commands",
FlagUsage: "[command]",
Function: showHelp,
},
}
}
func main() {
if len(os.Args) < 2 {
usageMsg()
return
}
cmd, args := os.Args[1], os.Args[2:]
commands := getCommands()
if command, exists := commands[cmd]; exists {
command.Function(args)
return
}
fmt.Printf("Unknown command: %s\n", cmd)
usageMsg()
os.Exit(1)
}
func usageMsg() {
fmt.Println("Usage: cm <command> [options]")
fmt.Println()
fmt.Println("Available commands:")
commands := getCommands()
commandNames := slices.Sorted(maps.Keys(commands))
// find the longest command name for formatting
maxLen := len(slices.MaxFunc(commandNames, func(a, b string) int {
return len(a) - len(b)
}))
// print commands with descriptions in sorted order
for _, cmd := range commandNames {
command := commands[cmd]
fmt.Printf(" %-*s %s\n", maxLen, cmd, command.Description)
}
fmt.Println()
fmt.Println("Use 'cm help <command>' for detailed help on a specific command.")
}
func showHelp(args []string) {
if len(args) == 0 {
usageMsg()
return
}
cmd := args[0]
commands := getCommands()
command, exists := commands[cmd]
if !exists {
fmt.Printf("Unknown command: %s\n", cmd)
usageMsg()
return
}
fmt.Printf("Description: %s\n", command.Description)
fmt.Printf("Usage: cm %s %s\n", cmd, command.FlagUsage)
}
func exitErr(err error, msg string) {
fmt.Printf("%s: %s\n", msg, err)
os.Exit(1)
}