-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
181 lines (133 loc) · 3.19 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
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
package main
import (
"os"
"strings"
"github.com/charmbracelet/log"
)
const HELP = `Usage: %s [options]
Options:
help Display this help text
author Include information about the author
build Build the site
serve Serve the site
debug Enable debug mode
Environment Variables:
SITE_ADDRESS The address and port for serve to listen on
(address:port)`
const AUTHOR = `
This program was created by:
Tom Boddaert
https://tomBoddaert.com/`
var logger = log.NewWithOptions(
os.Stdout,
log.Options{
TimeFormat: "15:04:05",
},
)
func check(err error) {
logger.Helper()
if err != nil {
logger.SetOutput(os.Stderr)
logger.Fatalf(err.Error())
}
}
func enableDebug() {
logger.SetLevel(log.DebugLevel)
logger.SetReportCaller(true)
logger.Debug("Debug mode enabled")
}
func main() {
cmd := os.Args[0]
args := os.Args[1:]
doHelp := false
doAuthor := false
doBuild := false
doServe := false
debugMode := false
for _, arg := range args {
switch strings.ToLower(arg) {
case "help":
doHelp = true
case "author":
doAuthor = true
case "build":
doBuild = true
case "serve":
doServe = true
case "debug":
debugMode = true
default:
logger.Fatalf("Unknown option: '%s'! Use '%s help'", arg, cmd)
}
}
if debugMode {
enableDebug()
}
if doHelp {
logger.Printf(HELP, cmd)
if doAuthor {
logger.Print(AUTHOR)
}
if doBuild || doServe {
logger.Warn("Other options used with 'help', ignoring.")
}
return
}
if doAuthor {
logger.Warn("'author' used without 'help', ignoring.")
}
if !doBuild && !doServe {
logger.Fatalf("Nothing to do! Use '%s help'!", cmd)
}
config := getConfig()
if doBuild {
build(config)
}
if doServe {
Serve(config)
}
}
func build(config Config) {
if len(config.PrebuildCmds) != 0 {
logger.SetReportTimestamp(true)
logger.Info("Running prebuild commands...")
logger.SetReportTimestamp(false)
RunCmds(config.PrebuildCmds)
} else {
logger.Debug("No prebuild commands to run")
}
logger.SetReportTimestamp(true)
logger.Info("Creating temp directory...")
logger.SetReportTimestamp(false)
tempDir, err := os.MkdirTemp(".", "site-build-*")
check(err)
// Set the output directory to the temp directory
outDir := config.DstDir
config.DstDir = tempDir
logger.Debugf("Created temp directory (%v)", tempDir)
logger.SetReportTimestamp(true)
logger.Info("Copying raw pages...")
logger.SetReportTimestamp(false)
CopyRaw(&config)
logger.SetReportTimestamp(true)
logger.Info("Building templated pages...")
logger.SetReportTimestamp(false)
BuildTemplated(&config)
logger.SetReportTimestamp(true)
logger.Info("Replacing old build...")
logger.SetReportTimestamp(false)
logger.Debugf("Renaming directory (%v -> %v)", tempDir, outDir)
check(os.RemoveAll(outDir))
check(os.Rename(tempDir, outDir))
if len(config.PostbuildCmds) != 0 {
logger.SetReportTimestamp(true)
logger.Info("Running postbuild commands...")
logger.SetReportTimestamp(false)
RunCmds(config.PostbuildCmds)
} else {
logger.Debug("No postbuild commands to run")
}
logger.SetReportTimestamp(true)
logger.Info("Done")
logger.SetReportTimestamp(false)
}