This repository has been archived by the owner on Dec 21, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
fs.go
288 lines (185 loc) · 4.7 KB
/
fs.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
/*
maryo/fs.go
utilities involving files and the filesystem
written by Superwhiskers, licensed under gnu gplv3.
if you want a copy, go to http://www.gnu.org/licenses/
*/
package main
import (
// internals
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
)
// create directory
func makeDirectory(directory string) {
// make the directory
err := os.MkdirAll(directory, 0755)
// error handling
if err != nil {
// show error message
fmt.Printf("[err]: error while creating directory (does it already exist?)")
// show traceback
panic(err)
}
}
func doesDirExist(dir string) bool {
// check if directory exists
if _, err := os.Stat(dir); os.IsNotExist(err) {
// if it doesn't
return false
}
// if it does
return true
}
// check if file exists
func doesFileExist(file string) bool {
// this gets the absolute path of the file
dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
// this tells the program if it exists
_, err = os.Stat(strings.Join([]string{dir, "/", file}, ""))
// handle any errors
if err != nil {
// return false if it doesn't exist
return false
}
// then return true if it exists
return true
}
// create a file
func createFile(file string) {
// detect if file already exists
if doesFileExist(file) == true {
// display something if it already does
fmt.Printf("[err] : %s already exists..", file)
// then exit the program because you should already know if it does
os.Exit(1)
}
// create the file
oput, err := os.Create(file)
// handle any errors
if err != nil {
// display an error message
fmt.Printf("[err] : error creating file %s.. (does it already exist?)\n", file)
// close the file stream
defer oput.Close()
// show a traceback
panic(err)
}
// close it if done writing
defer oput.Close()
}
// read a file, output a byte
func readFileByte(file string) []byte {
// read file
b, err := ioutil.ReadFile(file)
// handle errors
if err != nil {
// display error message
fmt.Printf("[err] : error reading file %s.. (does it exist?)\n", file)
// show traceback
panic(err)
}
// return the byte array
return b
}
// read a file, output data as string
func readFile(file string) string {
// read the file with my other function
b := readFileByte(file)
// convert byte array to string
str := string(b)
// return the string
return str
}
// delete a file
func deleteFile(file string) {
// delete the file
err := os.Remove(file)
// handle errors
if err != nil {
// show error message
fmt.Printf("[err] : error deleting file %s..", file)
// show traceback
panic(err)
}
}
// write to file
func writeFile(file string, data string) {
// convert string to byte array
bdata := []byte(data)
// write to file
err := ioutil.WriteFile(file, bdata, 0644)
// handle errors
if err != nil {
// show error message
fmt.Printf("[err] : error writing to file %s.. (does it exist?)\n", file)
// show traceback
panic(err)
}
}
// write byte to file
func writeByteToFile(file string, data []byte) {
// write to file
err := ioutil.WriteFile(file, data, 0644)
// handle errors
if err != nil {
// show error message
fmt.Printf("[err] : error writing to file %s.. (does it exist?)\n", file)
// show traceback
panic(err)
}
}
// check if file is valid JSON
func checkJSONValidity(file string) bool {
// get JSON from file
filedata := []byte(readFile(file))
// this only exists because it's required to unmarshal the file
var data map[string]interface{}
// unmarshal the file
err := json.Unmarshal(filedata, &data)
// check for errors
if err != nil {
// return false if there is one
return false
}
// return true if there isn't one
return true
}
// read a JSON file
func readJSONFile(file string) map[string]interface{} {
// get json from file, and turn into byte array
jsonObj := []byte(readFile(file))
// initialize an interface
var data map[string]interface{}
// turn json into a valid golang item
err := json.Unmarshal(jsonObj, &data)
// handle errors
if err != nil {
// show error message
fmt.Printf("[err] : error converting raw JSON to valid golang item from %s.. (is this valid JSON?)\n", file)
// show traceback
panic(err)
}
// return the golang item
return data
}
// write to a json file
func writeJSONFile(file string, data map[string]interface{}) {
// turn go map into valid JSON
fileData, err := json.MarshalIndent(data, "", " ")
// handle errors
if err != nil {
// show error message
fmt.Printf("[err] : error while converting a golang map into JSON. (how did this even happen)\n")
// show traceback
panic(err)
}
// convert to string
sFileData := string(fileData)
// write it to a file
writeFile(file, sFileData)
}