-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
69 lines (63 loc) · 2.01 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
package main
import (
"flag"
"fmt"
"os"
"path/filepath"
"slices"
"strings"
)
func main() {
var commandType, directory, fileName string
flag.StringVar(&commandType, "command", "generate", "Choices: generate or restore. It specifies whether to generate a template for the specified file path or to restore the previous solution. Default choice is generate.")
flag.StringVar(&directory, "directory", "", "(Required). A list of comma separed directories to perform the specified command. Examples bfs or bfs,dfs,prim. A shorthand notation in case of the whole project is to use a period (.) The shorthand notation for the whole project has a higher priority")
flag.StringVar(&fileName, "file", "", "(Optional). The name of the target file in case of a single directory. Examples bfs.go, dfs.go")
flag.Parse()
if commandType != "restore" && commandType != "generate" {
flag.Usage()
os.Exit(1)
}
directories := strings.Split(directory, ",")
if len(directories) == 0 {
flag.Usage()
os.Exit(1)
}
if slices.Index(directories, ".") != -1 {
directories = make([]string, 0, len(packageTemplates))
for k := range packageTemplates {
directories = append(directories, k)
}
} else {
for _, dir := range directories {
if _, ok := packageTemplates[dir]; !ok {
fmt.Println("could not locate the directory: ", dir)
os.Exit(1)
}
}
}
if fileName != "" && len(directories) > 1 {
flag.Usage()
os.Exit(1)
}
if fileName != "" {
tempName := strings.TrimSuffix(fileName, filepath.Ext(fileName))
if _, ok := packageTemplates[directories[0]][tempName]; !ok {
fmt.Println("could not locate the file ", fileName, "in the directory ", directories[0])
os.Exit(1)
}
fileName = tempName
}
if commandType == "generate" {
if fileName != "" {
writeToFile(directories[0], fileName, packageTemplates[directories[0]][fileName])
} else {
generateTemplates(directories)
}
} else {
if fileName != "" {
fetchFileContent(directories[0], fileName)
} else {
restoreSolutions(directories)
}
}
}