-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
156 lines (132 loc) · 3.8 KB
/
main.go
File metadata and controls
156 lines (132 loc) · 3.8 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
package main
import (
"fmt"
"os"
"strings"
"github.com/joeshaw/envdecode"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"golang.org/x/term"
"coolstercodes/modules/modulir"
)
//////////////////////////////////////////////////////////////////////////////
//
//
//
// Main
//
//
//
//////////////////////////////////////////////////////////////////////////////
func main() {
rootCmd := &cobra.Command{
Use: "coolstercodes",
Short: "Coolster Codes is my blog",
Long: "Coolster Codes is my blog, see https://coolstercodes.com",
}
buildCommand := &cobra.Command{
Use: "build",
Short: "Run a single build loop",
Long: strings.TrimSpace(`
Starts the build loop that watches for local changes and runs
when they're detected. A webserver is started on PORT (default
5002).`),
Run: func(_ *cobra.Command, _ []string) {
modulir.Build(getModulirConfig(), build)
},
}
rootCmd.AddCommand(buildCommand)
loopCommand := &cobra.Command{
Use: "loop",
Short: "Start build and serve loop",
Long: strings.TrimSpace(`
Runs the build loop one time and places the result in TARGET_DIR
(default ./public/).`),
Run: func(_ *cobra.Command, _ []string) {
modulir.BuildLoop(getModulirConfig(), build)
},
}
rootCmd.AddCommand(loopCommand)
if err := envdecode.Decode(&conf); err != nil {
fmt.Fprintf(os.Stderr, "Error decoding conf from env: %v", err)
os.Exit(1)
}
if err := rootCmd.Execute(); err != nil {
fmt.Fprintf(os.Stderr, "Error executing command: %v", err)
os.Exit(1)
}
}
//////////////////////////////////////////////////////////////////////////////
//
//
//
// Variables
//
//
//
//////////////////////////////////////////////////////////////////////////////
// Left as a global for now for the sake of convenience, but it's not used in
// very many places and can probably be refactored as a local if desired.
var conf Conf
//////////////////////////////////////////////////////////////////////////////
//
//
//
// Types
//
//
//
//////////////////////////////////////////////////////////////////////////////
// Conf contains configuration information for the command. It's extracted from
// environment variables.
type Conf struct {
// AbsoluteURL is the absolute URL where the compiled site will be hosted.
// It's used for things like Atom feeds and sending email.
AbsoluteURL string `env:"ABSOLUTE_URL,default=https://coolstercodes.com"`
// Concurrency is the number of build Goroutines that will be used to
// perform build work items.
Concurrency int `env:"CONCURRENCY,default=10"`
// Port is the port on which to serve HTTP when looping in development.
Port int `env:"PORT,default=5002"`
// CCEnv is the environment to run the app with. Use "development" to
// activate development features.
CCEnv string `env:"CC_ENV,default=development"`
// TargetDir is the target location where the site will be built to.
TargetDir string `env:"TARGET_DIR,default=./public"`
// Verbose is whether the program will print debug output as it's running.
Verbose bool `env:"VERBOSE,default=false"`
}
//////////////////////////////////////////////////////////////////////////////
//
//
//
// Private
//
//
//
//////////////////////////////////////////////////////////////////////////////
const (
ccEnvDevelopment = "development"
)
func getLog() *logrus.Logger {
log := logrus.New()
if conf.Verbose {
log.SetLevel(logrus.DebugLevel)
} else {
log.SetLevel(logrus.InfoLevel)
}
return log
}
// getModulirConfig interprets Conf to produce a configuration suitable to pass
// to a Modulir build loop.
func getModulirConfig() *modulir.Config {
return &modulir.Config{
Concurrency: conf.Concurrency,
Log: getLog(),
LogColor: term.IsTerminal(int(os.Stdout.Fd())),
Port: conf.Port,
SourceDir: ".",
TargetDir: conf.TargetDir,
Websocket: conf.CCEnv == ccEnvDevelopment,
}
}