-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcliCommands.go
407 lines (352 loc) · 10.7 KB
/
cliCommands.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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
//Package gollery - main package of the application with all the logic
package gollery
import (
"fmt"
"io/ioutil"
"log"
"os"
"errors"
"github.com/manifoldco/promptui"
"github.com/urfave/cli"
"gopkg.in/yaml.v2"
"path/filepath"
)
// Function for creating a new root gallery folder
// Checks whether a custom path was specified and uses current path if not.
// Creates a custom_css folder, the config.yaml with a example gallery and the corresponding file structure
func initGollery(path string) error {
//Define where the new gollery should be initialized
if path == "" {
pathSelect := promptui.Select{
Label: "You haven't specified a Path. Should the new gollery be initialized at " + getDir() + "?",
Items: []string{"yep, go!", "nop!"},
}
pathValidate := func(input string) error {
if checkFile(input) {
return errors.New("provided path doesn't exist")
}
return nil
}
enterPath := promptui.Prompt{
Label: "Please enter a custom Path (full or starting at the current location)",
Validate: pathValidate,
}
if _, s, err := pathSelect.Run(); err != nil {
fmt.Printf("Prompt failed %v\n", err)
return err
} else if s == "yep, go!" {
path = getDir()
} else if s == "nop!" {
var err error
if path, err = enterPath.Run(); err != nil {
fmt.Printf("Prompt failed %v\n", err)
return err
}
}
}
if checkFile(path) {
err := os.Mkdir(path, 0600)
check(err)
log.Print("Created new Directory " + path + ".")
}
emptyPath := promptui.Select{
Label: "The provided folder is not empty. Do you want to continue?",
Items: []string{"yep, go!", "nop!"},
}
if len(readDir(path)) != 0 {
if _, s, err := emptyPath.Run(); err != nil {
fmt.Printf("Prompt failed %v\n", err)
return err
} else if s == "nop!" {
return nil
}
}
if !checkFile(path + "/config.yaml") {
overwriteConfig := promptui.Select{
Label: "The config.yaml file already exists. Do you want to overwrite it?",
Items: []string{"yep, go!", "nop!"},
}
if _, s, err := overwriteConfig.Run(); err != nil {
fmt.Printf("Prompt failed %v\n", err)
return err
} else if s == "yep, go!" {
writeConfig(path, initExampleConfig())
log.Print("Successfully overwritten config.yaml.")
} else if s == "nop!" {
log.Print("Old config.yaml wasn't changed.")
return nil
}
} else {
writeConfig(path, initExampleConfig())
log.Print("Created new config.yaml.")
}
createGalleries(path)
genImages := promptui.Select{
Label: "Do you want some example Images?",
Items: []string{"yep, go!", "nop!"},
}
if _, s, err := genImages.Run(); err != nil {
fmt.Printf("Prompt failed %v\n", err)
return err
} else if s == "yep, go!" {
downloadFile(galleryPath+"example/"+origImgDir+"example1.jpg", "https://unsplash.com/photos/H4Sv_zRXBos/download?force=true")
downloadFile(galleryPath+"example/"+origImgDir+"example2.jpg", "https://unsplash.com/photos/bF9kRBJhMpE/download?force=true")
downloadFile(galleryPath+"example/"+origImgDir+"example3.jpg", "https://unsplash.com/photos/XqMjjuQuyZQ/download?force=true")
}
log.Print("New gollery was created successfully 👍🏻")
return nil
}
// Creates an example gallery configuration
func initExampleConfig() Config {
g := make(map[string]*Gallery)
e := Gallery{Title: "example", Description: "This is an example gallery.", Download: false, CustomCss: false}
g["example"] = &e
c := Config{Port: "8080", Galleries: g, Auth: true}
return c
}
// Write a new config to the filesystem.
func writeConfig(path string, c Config) {
for _, gallery := range c.Galleries {
if gallery.Link == "" {
gallery.Link = getCrypto(32)
}
if gallery.Password == "" {
gallery.Password = getCrypto(16)
}
log.Print("Gallery " + gallery.Title + " is reachable via http://localhost:" + c.Port + "/" + gallery.Link + " - gollery/" + gallery.Password)
}
d, err := yaml.Marshal(&c)
check(err)
err = ioutil.WriteFile(path+"/config.yaml", d, 0644)
check(err)
}
// Function creates a new gallery within an existing root folder and config.yaml
// Reads the existing config file, asks for Title (unique), Description and Download (bool)
// Writes new config and generates folder structure for new gallery
func newGallery(path string) error {
var err error
var newData Gallery
c := ReadConfig(path+"/config.yaml", false)
titleValidate := func(input string) error {
if len(input) < 1 {
return errors.New("title must have at least 1 character")
}
if c.Galleries[input] != nil {
return errors.New("gallery is already existing")
}
return nil
}
title := promptui.Prompt{
Label: "Title",
Validate: titleValidate,
}
description := promptui.Prompt{
Label: "Description",
}
password := promptui.Prompt{
Label: "Password (if empty password will be generated)",
}
download := promptui.Select{
Label: "Compress all images automatically and provide a download button?",
Items: []string{"yep, go!", "nop!"},
}
customCss := promptui.Select{
Label: "Create a custom_css file for this gallery?",
Items: []string{"yep, go!", "nop!"},
}
sort := promptui.Select{
Label: "Should the gallery be sorted after creation date of the images? (EXIF Data is necessary.)",
Items: []string{"yep, go!", "nop!"},
}
if newData.Title, err = title.Run(); err != nil {
fmt.Printf("Prompt failed %v\n", err)
return err
}
if newData.Description, err = description.Run(); err != nil {
fmt.Printf("Prompt failed %v\n", err)
return err
}
if newData.Password, err = password.Run(); err != nil {
fmt.Printf("Prompt failed %v\n", err)
return err
}
if _, s, err := download.Run(); err != nil {
fmt.Printf("Prompt failed %v\n", err)
return err
} else if s == "yep, go!" {
newData.Download = true
} else if s == "nop!" {
newData.Download = false
}
if _, s, err := customCss.Run(); err != nil {
fmt.Printf("Prompt failed %v\n", err)
return err
} else if s == "yep, go!" {
newData.CustomCss = true
} else if s == "nop!" {
newData.CustomCss = false
}
if _, s, err := sort.Run(); err != nil {
fmt.Printf("Prompt failed %v\n", err)
return err
} else if s == "yep, go!" {
newData.Sort = true
} else if s == "nop!" {
newData.Sort = false
}
c.Galleries[newData.Title] = &newData
writeConfig(path, c)
createGalleries(path)
return nil
}
// Reads config.yaml from filesystem
// Checks whether new gallery name already has a folder -> aborts if yes
// Create all necessary subfolders for the gallery
func createGalleries(path string) {
c := ReadConfig(path+"/config.yaml", false)
for subsite := range c.Galleries {
if checkFile(path + "/" + subsite) {
err := os.Mkdir(path+"/"+subsite, 0755)
check(err)
log.Print("Created new folder " + subsite + ".")
}
if checkFile(path + "/" + subsite + "/img") {
err := os.Mkdir(path+"/"+subsite+"/img", 0755)
check(err)
log.Print("Created new folder " + subsite + "/img .")
}
if checkFile(path + "/" + subsite + "/featured") {
err := os.Mkdir(path+"/"+subsite+"/featured", 0755)
check(err)
log.Print("Created new folder " + subsite + "/featured .")
}
if checkFile(path + "/" + subsite + "/preview") {
err := os.Mkdir(path+"/"+subsite+"/preview", 0755)
check(err)
log.Print("Created new folder " + subsite + "/preview .")
}
if checkFile(path + "/" + subsite + "/thumbnail") {
err := os.Mkdir(path+"/"+subsite+"/thumbnail", 0755)
check(err)
log.Print("Created new folder " + subsite + "/thumbnail .")
}
createCustomCss(c, subsite)
}
}
func startGollery(c *cli.Context, directory string) error {
if c.Bool("webserver") && c.Bool("filewatcher") {
log.Fatal("flag combination is not allowed")
}
if directory == "" {
galleryPath = getDir() + "/"
configPath = galleryPath + "/config.yaml"
} else {
galleryPath = filepath.Clean(filepath.ToSlash(directory)) + "/"
configPath = galleryPath + "config.yaml"
}
GlobConfig = ReadConfig(configPath, true)
if c.Bool("webserver") && !c.Bool("filewatcher") {
initWebServer(GlobConfig.Port)
}
if !c.Bool("webserver") && c.Bool("filewatcher") {
go checkImageTool()
checkSubSites(GlobConfig.Galleries)
watchFile(GlobConfig.Galleries)
}
if !c.Bool("webserver") && !c.Bool("filewatcher") {
go initWebServer(GlobConfig.Port)
checkImageTool()
checkSubSites(GlobConfig.Galleries)
watchFile(GlobConfig.Galleries)
}
return nil
}
func removeGallery(path string) error {
c := ReadConfig(path+"/config.yaml", false)
log.Print("Please provide the title of the gallery:")
validate := func(input string) error {
if c.Galleries[input] == nil {
return errors.New("gallery is not existing")
}
return nil
}
title := promptui.Prompt{
Label: "Title",
Validate: validate,
}
result, err := title.Run()
check(err)
if !checkFile(path + "/" + result) {
removeFile(path + "/" + result)
}
delete(c.Galleries, result)
writeConfig(path, c)
return nil
}
// CliAccess - Main function for all functionality
// provides all cli arguments via cli plugin - read doc for more information
func CliAccess() {
var customDir string
app := cli.NewApp()
app.Name = "gollery"
app.Version = "0.1.0"
app.Usage = "start, initialize, create and remove new galleries in gollery"
app.Authors = []cli.Author{
{
Name: "Simon Couball", Email: "[email protected]",
},
}
app.Flags = []cli.Flag{
cli.StringFlag{Name: "custom-dir, c", Usage: "custom directory ", Destination: &customDir},
}
app.Commands = []cli.Command{
{
Name: "start",
Aliases: []string{"s"},
Usage: "Start gollery",
Description: "Starts gollery in the current cli",
Action: func(c *cli.Context) error {
return startGollery(c, customDir)
},
Flags: []cli.Flag{
cli.BoolFlag{Name: "webserver, w", Usage: "only start webserver"},
cli.BoolFlag{Name: "filewatcher, f", Usage: "only start filewatcher"},
},
},
{
Name: "init",
Aliases: []string{"i"},
Usage: "init new root directory",
Description: "Creates a new gollery with a config.yaml and an example gallery.",
Action: func(c *cli.Context) error {
return initGollery(customDir)
},
},
{
Name: "new",
Aliases: []string{"n"},
Usage: "create new gallery",
Description: "Creates a new gallery within an existing config.yaml and adds the necessary folders.",
Action: func(c *cli.Context) error {
if customDir == "" {
return newGallery(getDir())
}
return newGallery(customDir)
},
},
{
Name: "remove",
Aliases: []string{"r"},
Usage: "remove a gallery",
Description: "Removes a gallery from an existing config.yaml and deletes all the files. ",
Action: func(c *cli.Context) error {
if customDir == "" {
return removeGallery(getDir())
}
return removeGallery(customDir)
},
},
}
err := app.Run(os.Args)
check(err)
}