-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
280 lines (234 loc) · 6.41 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
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
package stubfilegenerator
import (
"path/filepath"
"fmt"
"strings"
"os"
"image"
"image/png"
"image/gif"
"image/jpeg"
"image/color"
"image/draw"
"golang.org/x/image/font"
"golang.org/x/image/font/inconsolata"
"golang.org/x/image/math/fixed"
"golang.org/x/image/font/basicfont"
"errors"
)
type StubGenerator struct {
// Variable map for search&replace inside Text fields or files
TemplateVariables map[string]string
Image struct {
// Background color of generated image
BackgroundColor color.RGBA
// Font color (for inserted text)
FontColor color.RGBA
// Font (for inserted text)
Font *basicfont.Face
// Font line height
FontLineHeight int
// Number of colors for palette (GIF)
NumColors int
// Quality (JPG)
Quality int
// Width of image
Width int
// Height of image
Height int
// Lines of text which will be written inside the image
Text []string
}
// Overwrite existing files
Overwrite bool
rootPath string
}
// Constructor
// Will init sane defaults
func NewStubGenerator() StubGenerator {
stubGen := StubGenerator{}
stubGen.TemplateVariables = map[string]string{}
stubGen.Image.BackgroundColor = color.RGBA{255, 255, 170, 255}
stubGen.Image.FontColor = color.RGBA{0, 0, 0, 255}
stubGen.Image.Font = inconsolata.Regular8x16
stubGen.Image.FontLineHeight = 15
stubGen.Image.NumColors = 256
stubGen.Image.Quality = 80
stubGen.Image.Width = 600
stubGen.Image.Height = 200
stubGen.Image.Text = []string{
"Path: %PATH%",
}
stubGen.Overwrite = false
stubGen.rootPath = ""
return stubGen
}
// Clone stub genetator
func (config *StubGenerator) Clone() (StubGenerator) {
clone := *config
return clone
}
// Init generate stub run
func (config *StubGenerator) init(path string) (string, error) {
var err error
if _, ok := config.TemplateVariables["PATH"]; !ok {
config.TemplateVariables["PATH"] = path
}
if config.rootPath == "" {
return "", errors.New(fmt.Sprintf("Root path is empty, please use SetRootPath before generating files", path))
}
// get absolute path with root prefix
path, err = filepath.Abs(filepath.Join(config.rootPath, path))
// check if root path is still included
if err != nil || !strings.HasPrefix(path, config.rootPath) {
return "", errors.New(fmt.Sprintf("Invalid path \"%s\": outside of root path (used .. inside path?)", path))
}
if config.Overwrite == false {
if _, err = os.Stat(path); !os.IsNotExist(err) {
// file eixsts
return "", errors.New(fmt.Sprintf("File \"%s\" already exists", path))
}
}
return path, nil
}
// Set root path of file generation
// This root path also makes sure that no files are generated outside
// of this directory
func (config *StubGenerator) SetRootPath(path string) error {
rootPath, err := filepath.Abs(path)
if err != nil {
return errors.New(fmt.Sprintf("Unable to get root path: %v", err))
}
config.rootPath = rootPath
return nil
}
// generate one stub file, path will specify type and destination
func (config *StubGenerator) Generate(path string) error {
path, err := config.init(path)
if err != nil {
return err
}
if config.generateStubAuto(path) {
return nil
}
if config.generateStubImage(path) {
return nil
}
if config.generateStubText(path) {
return nil
}
config.generateStubFallback(path)
return nil
}
// Generate stub by using automatic stubs (simple deployments)
func (config *StubGenerator) generateStubAuto(path string) bool {
assetFile := fmt.Sprintf("res/auto/stub%s", filepath.Ext(path))
data, err := Asset(assetFile)
if err == nil {
createFile(path, func(f *os.File) {
f.Write(data)
})
return true
}
return false
}
// Generate image stub
func (config *StubGenerator) generateStubImage(path string) bool {
fileExt := filepath.Ext(path)
switch fileExt {
case ".gif":
createFile(path, func(f *os.File) {
gif.Encode(f, config.createImage(), &gif.Options{config.Image.NumColors, nil, nil})
})
return true
case ".png":
createFile(path, func(f *os.File) {
png.Encode(f, config.createImage())
})
return true
case ".jpg":
fallthrough
case ".jpeg":
options := jpeg.Options{
Quality: config.Image.Quality,
}
createFile(path, func(f *os.File) {
jpeg.Encode(f, config.createImage(), &options)
})
return true
}
return false
}
// Create go image resource
func (config *StubGenerator) createImage() *image.RGBA {
img := image.NewRGBA(image.Rect(0, 0, config.Image.Width, config.Image.Height))
draw.Draw(img, img.Bounds(), &image.Uniform{config.Image.BackgroundColor}, image.ZP, draw.Src)
for num, line := range config.Image.Text {
for key, value := range config.TemplateVariables {
replaceKey := fmt.Sprintf("%%%s%%", key)
line = strings.Replace(line, replaceKey, value, -1)
}
posX := 10
posY := 10 + (config.Image.FontLineHeight * (num + 1))
config.imageWriteTextLine(img, posX, posY, line)
}
return img
}
// Create text stub like txt, cvs and other text based files
// TemplateVariables will be used to replace the content
func (config *StubGenerator) generateStubText(path string) bool {
config.init(path)
assetFile := fmt.Sprintf("res/templates/stub%s", filepath.Ext(path))
data, err := Asset(assetFile)
if err == nil {
content := string(data)
for key, value := range config.TemplateVariables {
replaceKey := fmt.Sprintf("%%%s%%", key)
content = strings.Replace(content, replaceKey, value, -1)
}
createFile(path, func(f *os.File) {
f.WriteString(content)
})
return true
}
return false
}
// Create fallback stub, file will only contain TemplateVariables
func (config *StubGenerator) generateStubFallback(path string) bool {
createFile(path, func(f *os.File) {
for key, value := range config.TemplateVariables {
f.WriteString(fmt.Sprintf("%s: %s", key, value))
}
})
return true
}
// Create path and file with content using a callback function
func createFile(path string, callback func(f *os.File)) *os.File {
directory := filepath.Dir(path)
os.MkdirAll(directory, os.ModePerm)
f, err := os.Create(path)
check(err)
defer func() {
f.Close()
f.Sync()
}()
callback(f)
return f
}
// check for error
func check(e error) {
if e != nil {
panic(e)
}
}
// Write text line into image resource
func (config *StubGenerator) imageWriteTextLine(img *image.RGBA, x, y int, label string) {
point := fixed.Point26_6{fixed.Int26_6(x * 64), fixed.Int26_6(y * 64)}
d := &font.Drawer{
Dst: img,
Src: image.NewUniform(config.Image.FontColor),
Face: config.Image.Font,
Dot: point,
}
d.DrawString(label)
}