-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
123 lines (109 loc) · 2.3 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
package main
import (
"fmt"
"os"
"path"
)
// Global Variables
var (
output string
pre bool
block bool
list bool
err error
filePath string
outPath string
customTitle string
)
// Defaults
var templatePath = "html_template.tmpl"
var titleCase = "none"
const noInput = `
meow: You might be missing an argument or entered a wrong option.
Try 'meow --help' for more information.
`
func parseArgs(x uint) {
switch os.Args[x] {
case "--help":
// Print Help
if len(os.Args) > 2 {
fmt.Println("WARNING: --help doesn't expect any more arguments.")
}
fmt.Println(help)
os.Exit(0)
case "--generate":
// Write template and css variable to file.
if len(os.Args) > 2 {
fmt.Println("WARNING: --generate option don't expect any more arguments.")
}
err = os.WriteFile(templatePath, []byte(templateFile), 0644)
if err != nil {
fmt.Println("ERROR: failed to generate template file")
os.Exit(1)
}
err = os.WriteFile("style.css", []byte(cssFile), 0644)
if err != nil {
fmt.Println("ERROR: failed to generate css file")
os.Exit(1)
}
os.Exit(0)
case "-o", "--output":
if len(os.Args) < 3 {
fmt.Println(noInput)
os.Exit(0)
}
outPath = os.Args[x+1]
_, err = os.Stat(outPath)
if os.IsNotExist(err) {
fmt.Println("ERROR: output directory does not exist")
//TODO: Ask user to create the directory ? mkdir -p <path>
os.Exit(1)
}
parseArgs(x + 2)
case "-t", "--template":
if len(os.Args)-int(x) < 3 {
fmt.Println(noInput)
os.Exit(0)
}
templatePath = os.Args[x+1]
parseArgs(x + 2)
case "-c", "--setcase":
if len(os.Args)-int(x) < 3 {
fmt.Println(noInput)
os.Exit(0)
}
titleCase = os.Args[x+1]
parseArgs(x + 2)
default:
filePath = os.Args[x]
}
}
func main() {
if len(os.Args) == 1 {
fmt.Println(noInput)
os.Exit(0)
}
// passing 1st argument to parseArgs to check
parseArgs(1)
f, err := os.Stat(filePath)
if os.IsNotExist(err) {
fmt.Println("ERROR: input file/directory doesn't exist")
os.Exit(1)
}
if !f.IsDir() {
generateOutput()
os.Exit(0)
}
files, err := os.ReadDir(filePath)
if err != nil {
fmt.Println("ERROR: can't read input directory")
}
copyPath := filePath
for _, file := range files {
if !file.IsDir() {
filePath = path.Join(copyPath, file.Name())
generateOutput()
}
}
os.Exit(0)
}