-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile.go
463 lines (369 loc) · 9.31 KB
/
file.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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
package files_util
import (
"bufio"
"bytes"
"encoding/csv"
"encoding/json"
"errors"
"fmt"
"github.com/jszwec/csvutil"
"gopkg.in/yaml.v3"
"io"
"log"
"math/rand"
"os"
"reflect"
"strings"
"unicode"
)
const (
folderPerm = 0777
filePerm = 0777
DefaultCSVSep = ","
)
func CreateFolder(folderPath string) error {
if _, err := os.Stat(folderPath); errors.Is(err, os.ErrNotExist) {
return os.MkdirAll(folderPath, folderPerm)
}
return nil
}
func CreateFile(filePath string) error {
if _, err := os.Stat(filePath); err != nil {
var file *os.File
file, err = os.Create(filePath)
if err != nil {
return err
}
return file.Close()
}
return nil
}
// CreateCSV creates a new CSV file at the given filePath and writes the keys to it.
// It returns an error if there was a problem creating or writing to the file.
func CreateCSV(filePath string, keys [][]string) error {
if _, err := os.Stat(filePath); errors.Is(err, os.ErrNotExist) {
var file *os.File
file, err = os.OpenFile(filePath, os.O_CREATE|os.O_WRONLY, 0666)
if err != nil {
return err
}
if err = csv.NewWriter(file).WriteAll(keys); err != nil {
return err
}
return file.Close()
}
return nil
}
// ReadCSV reads a CSV file located at the given filePath and unmarshal its contents into a slice of type T.
// The function returns the unmarshalled rows and any error encountered during the operation.
func ReadCSV[T any](filePath string) ([]T, error) {
var err error
var rows []T
body, err := os.ReadFile(filePath)
if err != nil {
return nil, err
}
err = csvutil.Unmarshal(body, &rows)
return rows, err
}
func WriteCSV(filePath string, rows [][]string) error {
for _, row := range rows {
if err := AppendCSV(filePath, row); err != nil {
return err
}
}
return nil
}
// AppendCSV appends records to a CSV file located at the given filePath.
func AppendCSV(filePath string, records []string) error {
f, err := os.OpenFile(filePath, os.O_RDWR|os.O_APPEND|os.O_CREATE, 0666)
if err != nil {
return err
}
w := csv.NewWriter(f)
if err = w.Write(records); err != nil {
return err
}
w.Flush()
return f.Close()
}
func tmp(filePath, fieldValue string) error {
// Open CSV file
f, err := os.Open(filePath)
if err != nil {
return err
}
defer f.Close()
// Read File into a variable
lines, err := csv.NewReader(f).ReadAll()
if err != nil {
return err
}
// Loop through lines & turn them into object
for i, line := range lines {
if line[0] == fieldValue {
lines = append(lines[:i], lines[i+1:]...)
break
}
}
// Open CSV file in WRITE mode
f, err = os.Create(filePath)
if err != nil {
return err
}
defer f.Close()
// Write new datafile
err = csv.NewWriter(f).WriteAll(lines)
if err != nil {
return err
}
fmt.Println("Removed row from CSV for ", fieldValue)
return nil
}
func RemoveCSVRow[T any](filePath string, key any) error {
rows, err := ReadCSV[T](filePath)
if err != nil {
return err
}
log.Println("les rows", rows)
for i, row := range rows {
if strings.Contains(fmt.Sprint(row), fmt.Sprint(key)) {
rows = append(rows[:i], rows[i+1:]...)
}
}
log.Println("new rows", rows)
var rowsStr [][]string
for _, row := range rows {
rowsStr = append(rowsStr, strings.Split(fmt.Sprint(row), ","))
}
log.Println("rowsStr", rowsStr)
if err = WriteCSV(filePath, rowsStr); err != nil {
log.Fatal(err)
}
fmt.Println("before nrows")
nrows, err := ReadCSV[T](filePath)
if err != nil {
log.Fatal(err)
}
log.Println("nrows", nrows)
return nil
}
/*
func RemoveCSVRowN[T any](filePath string, index int) error {
rows, err := ReadCSV[T](filePath)
if err != nil {
return err
}
for i, row := range rows {
_ = row
if i == index {
}
}
return nil
}
*/
// func RemoveCSVRowContains(key string) {}
/*
func RemoveCSVRowsContains[T any](filePath, key string) error {
rows, err := ReadCSV[T](filePath)
if err != nil {
return err
}
for _, row := range rows {
_ = row
}
return nil
}
*/
// ValidateCSV validates the fields of a CSV file against the fields of a struct.
func ValidateCSV[T any](filePath, sep string) error {
var data T
file, err := os.Open(filePath)
if err != nil {
return err
}
fileScanner := bufio.NewScanner(file)
fileScanner.Split(bufio.ScanLines)
if !fileScanner.Scan() {
return fmt.Errorf("error empty file: %s", filePath)
}
fields := strings.Split(fileScanner.Text(), sep)
for i, field := range fields {
r := []rune(field)
r[0] = unicode.ToUpper(r[0])
fields[i] = string(r)
}
valueOf := reflect.ValueOf(data)
var valueFields []string
for i := 0; i < valueOf.NumField(); i++ {
valueFields = append(valueFields, valueOf.Type().Field(i).Name)
}
for i, field := range fields {
if field != valueFields[i] {
return fmt.Errorf("field name mismatch")
}
}
return nil
}
// ReadJSON reads a JSON file located at the given filePath and unmarshals its contents into a value of type T.
// The function returns the unmarshaled data and any error encountered during the operation.
func ReadJSON[T any](filePath string) (T, error) {
var data T
file, err := os.ReadFile(filePath)
if err != nil {
return data, err
}
err = json.Unmarshal(file, &data)
return data, err
}
func AppendJSON[T any](filePath string, data T) error {
buf, err := json.Marshal(data)
if err != nil {
return err
}
return WriteJSON(filePath, buf)
}
func ValidateJSON[T any](filePath string) error {
var data T
file, err := os.Open(filePath)
if err != nil {
return err
}
defer file.Close()
decoder := json.NewDecoder(file)
if err = decoder.Decode(&data); err != nil {
return fmt.Errorf("error while decoding JSON: %w", err)
}
jsonFields := reflect.ValueOf(data).Elem().Type()
structFields := reflect.TypeOf(new(T)).Elem()
for i := 0; i < structFields.NumField(); i++ {
structField := structFields.Field(i)
structFieldName := structField.Name
jsonFieldName := structFieldName
if tag, ok := structField.Tag.Lookup("json"); ok {
jsonFieldName = tag
}
jsonFieldNameRunes := []rune(jsonFieldName)
if len(jsonFieldNameRunes) > 0 {
jsonFieldNameRunes[0] = unicode.ToLower(jsonFieldNameRunes[0])
}
jsonField, ok := jsonFields.FieldByName(string(jsonFieldNameRunes))
if !ok {
return fmt.Errorf("json field '%s' not found", structFieldName)
}
if jsonField.Type != structField.Type {
return fmt.Errorf("field type mismatch '%s': JSON has '%s', struct has '%s'",
structFieldName, jsonField.Type, structField.Type)
}
}
return nil
}
func WriteJSON(filePath string, data any) error {
var buf bytes.Buffer
if err := json.NewEncoder(&buf).Encode(data); err != nil {
return err
}
return os.WriteFile(filePath, buf.Bytes(), filePerm)
}
// RemoveJSONLine removes a specific line in JSON
func RemoveJSONLine(filePath, key string) error {
content, err := ReadJSON[map[string]any](filePath)
if err != nil {
return err
}
for k := range content {
if k == key {
delete(content, k)
}
}
return WriteJSON(filePath, content)
}
func CreateYAML[T any](filePath string, dataEncoded T) error {
file, err := os.OpenFile(filePath, os.O_WRONLY|os.O_TRUNC|os.O_CREATE, 0644)
if err != nil {
return err
}
defer file.Close()
err = yaml.NewEncoder(file).Encode(&dataEncoded)
return err
}
func ReadYAML[T any](filePath string) (T, error) {
var data T
file, err := os.OpenFile(filePath, os.O_RDWR, 0644)
if err != nil {
return data, err
}
err = yaml.NewDecoder(file).Decode(&data)
return data, err
}
func AppendYAML[T any](filePath string) error {
file, err := os.OpenFile(filePath, os.O_RDWR|os.O_APPEND|os.O_CREATE, 0666)
if err != nil {
return err
}
_ = file
return nil
}
func ValidateYAML[T any](filePath string) error {
var data T
file, err := os.Open(filePath)
if err != nil {
return err
}
defer file.Close()
decoder := yaml.NewDecoder(file)
if err = decoder.Decode(&data); err != nil {
return fmt.Errorf("error while decoding YAML: %w", err)
}
yamlFields := reflect.ValueOf(data).Elem().Type()
structFields := reflect.TypeOf(new(T)).Elem()
for i := 0; i < structFields.NumField(); i++ {
structField := structFields.Field(i)
structFieldName := structField.Name
yamlFieldName := structFieldName
if tag, ok := structField.Tag.Lookup("yaml"); ok {
yamlFieldName = tag
}
yamlFieldNameRunes := []rune(yamlFieldName)
if len(yamlFieldNameRunes) > 0 {
yamlFieldNameRunes[0] = unicode.ToLower(yamlFieldNameRunes[0])
}
yamlField, ok := yamlFields.FieldByName(string(yamlFieldNameRunes))
if !ok {
return fmt.Errorf("yaml field '%s' not found", structFieldName)
}
if yamlField.Type != structField.Type {
return fmt.Errorf("field type mismatch '%s': YAML has '%s', struct has '%s'",
structFieldName, yamlField.Type, structField.Type)
}
}
return nil
}
func UnmarshalJSONToStruct[T any](data io.ReadCloser) (T, error) {
var t T
b, err := io.ReadAll(data)
if err != nil {
return t, err
}
err = json.Unmarshal(b, &t)
return t, err
}
func ReadProxyFile(path string) (proxies []string, err error) {
f, err := os.Open(path)
if err != nil {
return nil, err
}
fileScanner := bufio.NewScanner(f)
fileScanner.Split(bufio.ScanLines)
for fileScanner.Scan() {
proxies = append(proxies, fileScanner.Text())
}
for i := range proxies {
r := rand.Intn(i + 1)
proxies[i], proxies[r] = proxies[r], proxies[i]
}
if len(proxies) == 0 {
return nil, errors.New("empty proxy list")
}
return proxies, nil
}