-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.go
78 lines (65 loc) · 1.69 KB
/
helpers.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"github.com/fatih/color"
)
const CONFIG_READ_ERROR = "Error occured while reading the config file: "
// Reading files requires checking most calls for errors.
// This helper will streamline our error checks below.
func check(e error) {
if e != nil {
panic(e)
}
}
// pp prints the given string, built to make code more readable.
func pp(s interface{}) {
fmt.Println(s)
}
// remove removes an element from a given array.
func remove(slice []string, s int) []string {
return append(slice[:s], slice[s+1:]...)
}
// dirExists returns whether the given file or directory exists or not
func dirExists(path string) bool {
found, err := os.Stat(path)
if err == nil && found.IsDir() {
return true
}
return false
}
// fileExists returns whether the given file exists or not
func fileExists(path string) bool {
if _, err := os.Stat(path); os.IsNotExist(err) {
return false
}
return true
}
// readConfig reads the config file from the static folder to a Config object.
func readConfig() (config Config) {
configJson, _ := Asset("config.json")
err := json.Unmarshal(configJson, &config)
if err != nil {
printError(CONFIG_READ_ERROR + err.Error())
}
return config
}
func copyFile(directoryPath string, filePath string) {
file, _ := Asset(filePath)
os.Create(directoryPath + "/" + filePath)
err := ioutil.WriteFile(directoryPath+"/"+filePath, file, 0755)
check(err)
}
func printSuccess(s string) {
color.Green(wrapStars(s))
}
func printError(s string) {
color.Red(wrapStars(s))
}
func wrapStars(s string) string {
stars := "\n************************************************************\n"
s = stars + "\n" + s + "\n" + stars
return s
}