-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathdist.go
308 lines (260 loc) · 6.96 KB
/
dist.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
package cmd
import (
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"runtime"
"strings"
"github.com/BurntSushi/toml"
"github.com/pierrre/archivefile/zip"
"github.com/spf13/cobra"
)
var distCmdConfig distConfig
// distCmd represents the dist command
var distCmd = &cobra.Command{
Use: "dist",
Short: "Dist creates a distribution bundle for easy deployment",
Long: `Dist compiles your binary, builds your assets, and copies
the binary, assets, templates and migrations into a dist folder
for easy deployment to your production server. It can also
optionally zip your dist bundle for a single file deploy, and copy
over or generate new config files.`,
Example: "abcweb dist",
RunE: distCmdRun,
}
func init() {
distCmd.Flags().BoolP("config", "c", false, "Generate fresh config files in dist package")
distCmd.Flags().BoolP("copy-config", "", false, "Copy all .toml files from app root to dist package")
distCmd.Flags().BoolP("zip", "z", false, "Zip dist package once completed")
distCmd.Flags().BoolP("no-migrations", "", false, "Skip inclusion of migrations folder")
distCmd.Flags().BoolP("no-assets", "", false, "Skip inclusion of public assets folder")
distCmd.Flags().BoolP("no-templates", "", false, "Skip inclusion of templates folder")
distCmd.Flags().BoolP("alpine", "a", false, "Create a statically linked binary for compatibility with Alpine systems")
RootCmd.AddCommand(distCmd)
}
func distCmdRun(cmd *cobra.Command, args []string) error {
cnf.ModeViper.BindPFlags(cmd.Flags())
// Bare minimum requires git and go dependencies
checkDep("git", "go")
if cnf.ModeViper.GetBool("config") && cnf.ModeViper.GetBool("copy-config") {
return errors.New("config and copy-config cannot both be set at the same time")
}
// Delete the dist dir and recreate it so it's fresh
if err := os.RemoveAll(filepath.Join(cnf.AppPath, "dist")); err != nil {
return err
}
if err := os.Mkdir(filepath.Join(cnf.AppPath, "dist"), 0755); err != nil {
return err
}
fmt.Println("Building assets...")
if err := buildAssets(); err != nil {
return err
}
fmt.Println("Building Go app...")
if err := buildApp(); err != nil {
return err
}
fmt.Println("Copying binary to dist...")
if err := copyBinary(); err != nil {
return err
}
fmt.Println("Copying folders to dist...")
if err := copyFolders(); err != nil {
return err
}
if cnf.ModeViper.GetBool("config") {
fmt.Println("Generating fresh config files in dist...")
if err := freshConfig(); err != nil {
return err
}
}
if cnf.ModeViper.GetBool("copy-config") {
fmt.Println("Copying all .toml files from app root into dist...")
if err := copyConfig(); err != nil {
return err
}
}
if cnf.ModeViper.GetBool("zip") {
fmt.Println("Zipping dist folder into dist.zip...")
if err := zipDist(); err != nil {
return err
}
}
return nil
}
func zipDist() error {
// remove zip if it already exists
os.Remove(filepath.Join(cnf.AppPath, cnf.AppName+".zip"))
file, err := os.Create(filepath.Join(cnf.AppPath, cnf.AppName+".zip"))
defer file.Close()
if err != nil {
return err
}
distSrc := filepath.Join(cnf.AppPath, "dist") + string(filepath.Separator)
zip.Archive(distSrc, file, nil)
if err != nil {
return err
}
fmt.Printf("SUCCESS.\n\n")
return nil
}
func copyConfig() error {
rootDir, err := os.Open(filepath.Join(cnf.AppPath))
if err != nil {
return err
}
files, err := rootDir.Readdir(0)
if err != nil {
return err
}
for _, file := range files {
name := file.Name()
if strings.HasSuffix(name, ".toml") && name != ".abcweb.toml" && name != "watch.toml" {
err = copyFile(
filepath.Join(cnf.AppPath, name),
filepath.Join(cnf.AppPath, "dist", name),
file.Mode(),
)
if err != nil {
return err
}
}
}
fmt.Printf("SUCCESS.\n\n")
return nil
}
func freshConfig() error {
cfg := &newConfig{}
_, err := toml.DecodeFile(filepath.Join(cnf.AppPath, ".abcweb.toml"), cfg)
if err != nil {
fmt.Println("warning, unable to find .abcweb.toml, so your config may need tweaking")
}
// Overwrite default env to prod
cfg.DefaultEnv = "prod"
cfg.AppName = cnf.AppName
cfg.AppEnvName = cnf.AppEnvName
cfg.AppPath = cnf.AppPath
err = genConfigFiles(filepath.Join(cnf.AppPath, "dist"), cfg, true, true)
if err != nil {
return err
}
fmt.Printf("SUCCESS.\n\n")
return nil
}
func copyBinary() error {
var appName string
if runtime.GOOS == "windows" {
appName = cnf.AppName + ".exe"
} else {
appName = cnf.AppName
}
f, err := os.Stat(filepath.Join(cnf.AppPath, appName))
if err != nil {
return err
}
err = copyFile(
filepath.Join(cnf.AppPath, appName),
filepath.Join(cnf.AppPath, "dist", appName),
f.Mode(),
)
if err != nil {
return err
}
fmt.Printf("SUCCESS.\n\n")
return nil
}
func copyFolders() error {
var err error
if !cnf.ModeViper.GetBool("no-assets") {
// copy public folder
err = os.MkdirAll(filepath.Join(cnf.AppPath, "dist", "public"), 0755)
if err != nil {
return err
}
err = copyFolder(
filepath.Join(cnf.AppPath, "public"),
filepath.Join(cnf.AppPath, "dist", "public"),
)
if err != nil {
return err
}
}
if !cnf.ModeViper.GetBool("no-templates") {
// copy templates folder
err = os.Mkdir(filepath.Join(cnf.AppPath, "dist", "templates"), 0755)
if err != nil {
return err
}
err = copyFolder(
filepath.Join(cnf.AppPath, "templates"),
filepath.Join(cnf.AppPath, "dist", "templates"),
)
if err != nil {
return err
}
}
if !cnf.ModeViper.GetBool("no-migrations") {
// copy db/migrations folder if it exists
f, err := os.Stat(filepath.Join(cnf.AppPath, "db", "migrations"))
if err == nil && f.Size() > 0 {
migDir, err := os.Open(filepath.Join(cnf.AppPath, "db", "migrations"))
defer migDir.Close()
if err != nil {
return err
}
_, err = migDir.Readdirnames(1)
if err != nil && err != io.EOF {
return err
}
// Only copy db/migrations if folder has at least 1 file
if err != io.EOF {
err := os.MkdirAll(filepath.Join(cnf.AppPath, "dist", "db", "migrations"), 0755)
if err != nil {
return err
}
err = copyFolder(
filepath.Join(cnf.AppPath, "db", "migrations"),
filepath.Join(cnf.AppPath, "dist", "db", "migrations"),
)
if err != nil {
return err
}
}
}
}
fmt.Printf("SUCCESS.\n\n")
return nil
}
func copyFile(src, dst string, perm os.FileMode) error {
contents, err := ioutil.ReadFile(src)
if err != nil {
return err
}
return ioutil.WriteFile(dst, contents, perm)
}
func copyFolder(src, dst string) error {
return filepath.Walk(src, filepath.WalkFunc(func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
dstPath := filepath.Join(dst, strings.TrimPrefix(path, src))
if info.IsDir() {
err := os.MkdirAll(dstPath, info.Mode())
if err != nil {
return err
}
} else {
contents, err := ioutil.ReadFile(path)
if err != nil {
return err
}
if err := ioutil.WriteFile(dstPath, contents, info.Mode()); err != nil {
return err
}
}
return nil
}))
}