forked from aybabtme/brog
/
main.go
169 lines (144 loc) · 4.33 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
package main
import (
"fmt"
"github.com/aybabtme/brog/brogger"
"github.com/aybabtme/color/brush"
"os"
"strings"
)
const (
// Init a brog structure, but don't run the brog.
Init = "init"
// Create a new blank post in the post folder
Create = "create"
// Create a new blank page in the page folder
Page = "page"
// Server starts brog at the current path
Server = "server"
// Help shows the usage string
Help = "help"
// Version shows the current version of brog
Version = "version"
usage = `usage: brog {init | server [prod] | create [new post name] | page [new page name] | version}
'brog' is a tool to initialize brog structures, serve the content
of brog structures and create new posts in a brog structure.
The following are brog's valid commands with the arguments they take :
brog init Takes no argument, creates a new brog struc-
ture at the current working directory.
brog server [prod] Starts serving the brog structure at the
current location and watch for changes in the
template and post folders specified by the
config file. If [prod], use the production
port number specified in the config file. By
default, brog runs in development mode.
brog create [name] Creates a blank post in file [name], in the
location specified by the config file.
brog page [name] Creates a blank page in file [name], in the
location specified by the config file.
brog help Shows this message.
brog version Prints the current version of brog.
`
)
var (
errPfx = fmt.Sprintf("%s%s%s ",
brush.DarkGray("["),
brush.Red("ERROR"),
brush.DarkGray("]"))
)
func main() {
commands := os.Args[1:]
for i, arg := range commands {
switch arg {
case Init:
doInit()
return
case Server:
if len(commands) > i+1 {
doServer(commands[i+1] == "prod")
} else {
doServer(false)
}
return
case Create:
followingWords := strings.Join(commands[i+1:], "_")
doCreate(followingWords, "post")
return
case Page:
followingWords := strings.Join(commands[i+1:], "_")
doCreate(followingWords, "page")
return
case Version:
fmt.Println(brogger.Version)
return
case Help:
default:
printPreBrogError("Unknown command: %s.\n", arg)
}
}
fmt.Println(usage)
}
func doInit() {
fmt.Println(brush.DarkGray("A dark geometric shape is approaching..."))
errs := brogger.CopyBrogBinaries()
if len(errs) != 0 {
printPreBrogError("Couldn't inject brog nanoprobes.\n")
for _, err := range errs {
printPreBrogError("Message : %v.\n", err)
}
return
}
brog, err := brogger.PrepareBrog(false)
if len(errs) != 0 {
printPreBrogError("Couldn't prepare brog structure.\n")
printPreBrogError("Message : %v.\n", err)
return
}
brog.Ok("Initiliazing a brog. Resistance is futile.")
defer closeOrPanic(brog)
brog.Ok("Brog nanoprobes implanted.")
}
func doServer(isProd bool) {
brog, err := brogger.PrepareBrog(isProd)
if err != nil {
printPreBrogError("Couldn't start brog server.\n")
printPreBrogError("Message : %v.\n", err)
printTryInitMessage()
return
}
defer closeOrPanic(brog)
err = brog.ListenAndServe()
brog.Err("Whoops! %v.", err)
}
func doCreate(newPostFilename string, creationType string) {
brog, err := brogger.PrepareBrog(false)
if err != nil {
printPreBrogError("Couldn't create new post.\n")
printPreBrogError("Message : %v.\n", err)
printTryInitMessage()
return
}
defer closeOrPanic(brog)
if creationType == "page" {
err = brogger.CopyBlankToFilename(brog.Config, newPostFilename, brog.Config.PagePath)
} else {
err = brogger.CopyBlankToFilename(brog.Config, newPostFilename, brog.Config.PostPath)
}
if err != nil {
brog.Err("Brog %s creation failed, %v.", creationType, err)
brog.Err("Why do you resist?")
return
}
brog.Ok("'%s' will become one with the Brog.", newPostFilename)
}
func printPreBrogError(format string, args ...interface{}) {
errMsg := fmt.Sprintf("%s%s", errPfx, format)
fmt.Fprintf(os.Stderr, errMsg, args...)
}
func printTryInitMessage() {
fmt.Printf("Try initializing a brog here, run : brog %s.\n", Init)
}
func closeOrPanic(brog *brogger.Brog) {
if err := brog.Close(); err != nil {
panic(err)
}
}