/
source.go
244 lines (221 loc) · 6.48 KB
/
source.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
package source
//go:generate go run generate_help.go
//go:generate go run generate_version.go
//go:generate go run generate_languages.go
import (
"fmt"
"github.com/GeertJohan/go.rice"
"github.com/pkg/errors"
"github.com/statping/statping/utils"
"os"
"os/exec"
"path/filepath"
"strings"
)
var (
log = utils.Log.WithField("type", "source")
TmplBox *rice.Box // HTML and other small files from the 'source/dist' directory, this will be loaded into '/assets'
RequiredFiles = []string{
"css/style.css",
"css/style.css.gz",
"css/index.css",
"scss/index.scss",
"scss/base.scss",
"scss/forms.scss",
"scss/layout.scss",
"scss/mixin.scss",
"scss/mobile.scss",
"scss/variables.scss",
"banner.png",
"favicon.ico",
"robots.txt",
}
)
// Assets will load the Rice boxes containing the CSS, SCSS, favicon, and HTML files.
func Assets() error {
if utils.Params.GetBool("DISABLE_HTTP") {
return nil
}
var err error
TmplBox, err = rice.FindBox("dist")
if err != nil {
return err
}
return err
}
func scssRendered(name string) string {
spl := strings.Split(name, "/")
path := spl[:len(spl)-2]
file := spl[len(spl)-1]
splFile := strings.Split(file, ".")
return filepath.Join(strings.Join(path, "/"), "css", splFile[len(splFile)-2]+".css")
}
// CompileSASS will attempt to compile the SASS files into CSS
func CompileSASS() error {
sassBin := utils.Params.GetString("SASS")
if sassBin == "" {
bin, err := exec.LookPath("sass")
if err != nil {
log.Warnf("could not find sass executable in PATH: %s", err)
return err
}
sassBin = bin
}
scssFile := filepath.Join(utils.Params.GetString("STATPING_DIR"), "assets", "scss", "index.scss")
log.Infoln(fmt.Sprintf("Compiling SASS %v into %v", scssFile, scssRendered(scssFile)))
stdout, stderr, err := utils.Command(sassBin, scssFile, scssRendered(scssFile))
if err != nil {
log.Errorln(fmt.Sprintf("Failed to compile assets with SASS %v", err))
log.Errorln(fmt.Sprintf("%s %s %s", sassBin, scssFile, scssRendered(scssFile)))
return errors.Wrapf(err, "failed to compile assets, %s %s %s", err, stdout, stderr)
}
if stdout != "" || stderr != "" {
log.Infoln(fmt.Sprintf("out: %v | error: %v", stdout, stderr))
}
log.Infoln("SASS Compiling is complete!")
return nil
}
// UsingAssets returns true if the '/assets' folder is found in the directory
func UsingAssets(folder string) bool {
if _, err := os.Stat(folder + "/assets"); err == nil {
return true
} else {
if utils.Params.GetBool("USE_ASSETS") {
log.Infoln("Environment variable USE_ASSETS was found.")
if err := CreateAllAssets(folder); err != nil {
log.Warnln(err)
}
if err := CompileSASS(); err != nil {
//CopyToPublic(CssBox, folder+"/css", "base.css")
log.Warn(errors.Wrap(err, "Default 'base.css' was insert because SASS did not work."))
return true
}
return true
}
}
return false
}
// SaveAsset will save an asset to the '/assets/' folder.
func SaveAsset(data []byte, path string) error {
path = filepath.Join(utils.Directory, "assets", path)
err := utils.SaveFile(path, data)
if err != nil {
log.Errorln(fmt.Sprintf("Failed to save %v, %v", path, err))
return err
}
return nil
}
// OpenAsset returns a file's contents as a string
func OpenAsset(path string) string {
path = filepath.Join(utils.Directory, "assets", path)
data, err := utils.OpenFile(path)
if err != nil {
log.Errorln(fmt.Sprintf("Failed to open %s, %v", path, err))
return ""
}
return data
}
// CreateAllAssets will dump HTML, CSS, SCSS, and favicon assets into the '/assets' directory
func CreateAllAssets(folder string) error {
log.Infoln(fmt.Sprintf("Dump Statping assets into %s/assets", folder))
fp := filepath.Join
if err := MakePublicFolder(fp(folder, "/assets")); err != nil {
return err
}
if err := MakePublicFolder(fp(folder, "assets", "css")); err != nil {
return err
}
if err := MakePublicFolder(fp(folder, "assets", "favicon")); err != nil {
return err
}
if err := MakePublicFolder(fp(folder, "assets", "scss")); err != nil {
return err
}
log.Infoln("Inserting scss, css, and favicon files into assets folder")
if err := CopyAllToPublic(TmplBox); err != nil {
log.Errorln(err)
return errors.Wrap(err, "copying all to public")
}
if err := CopyToPublic(TmplBox, "", "robots.txt"); err != nil {
return err
}
if err := CopyToPublic(TmplBox, "", "banner.png"); err != nil {
return err
}
if err := CopyToPublic(TmplBox, "", "favicon.ico"); err != nil {
return err
}
log.Infoln("Compiling CSS from SCSS style...")
if err := CompileSASS(); err != nil {
return err
}
log.Infoln("Statping assets have been inserted")
return nil
}
// DeleteAllAssets will delete the '/assets' folder
func DeleteAllAssets(folder string) error {
err := utils.DeleteDirectory(folder + "/assets")
if err != nil {
log.Infoln(fmt.Sprintf("There was an issue deleting Statping Assets, %v", err))
return err
}
log.Infoln("Statping assets have been deleted")
return err
}
// CopyAllToPublic will copy all the files in a rice box into a local folder
func CopyAllToPublic(box *rice.Box) error {
exclude := map[string]bool{
"base.gohtml": true,
"index.html": true,
}
return box.Walk("/", func(path string, info os.FileInfo, err error) error {
if info.Name() == "" {
return nil
}
if exclude[info.Name()] {
return nil
}
if strings.Contains(path, "/js") {
return nil
}
if info.IsDir() {
return nil
}
file, err := box.Bytes(path)
if err != nil {
return err
}
return SaveAsset(file, path)
})
}
// CopyToPublic will create a file from a rice Box to the '/assets' directory
func CopyToPublic(box *rice.Box, path, file string) error {
assetPath := fmt.Sprintf("%v/assets/%v/%v", utils.Directory, path, file)
if path == "" {
assetPath = fmt.Sprintf("%v/assets/%v", utils.Directory, file)
}
log.Infoln(fmt.Sprintf("Copying %v to %v", file, assetPath))
base, err := box.String(file)
if err != nil {
log.Errorln(fmt.Sprintf("Failed to copy %v to %v, %v.", file, assetPath, err))
return err
}
err = utils.SaveFile(assetPath, []byte(base))
if err != nil {
log.Errorln(fmt.Sprintf("Failed to write file %v to %v, %v.", file, assetPath, err))
return err
}
return nil
}
// MakePublicFolder will create a new folder
func MakePublicFolder(folder string) error {
log.Infoln(fmt.Sprintf("Creating folder '%s'", folder))
if !utils.FolderExists(folder) {
err := utils.CreateDirectory(folder)
if err != nil {
log.Errorln(fmt.Sprintf("Failed to created %s directory, %v", folder, err))
return err
}
}
return nil
}