-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse.go
81 lines (71 loc) · 1.41 KB
/
parse.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
package dbtmock
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
)
// returns a Manifest structure out of a .json file
func ParseManifest(path string) Manifest {
jsonFile, err := os.Open(path)
if err != nil {
panic(err)
}
bytes, _ := ioutil.ReadAll(jsonFile)
manifest := Manifest{}
if err := json.Unmarshal(bytes, &manifest); err != nil {
panic(err)
}
return manifest
}
/*
returns a Test structure given a filepath
*/
func ParseTest(path string) (Test, error) {
jsonFile, err := os.Open(path)
if err != nil {
return Test{}, err
}
bytes, err := ioutil.ReadAll(jsonFile)
if err != nil {
return Test{}, err
}
test := Test{}
if err := json.Unmarshal(bytes, &test); err != nil {
return Test{}, err
}
return test, nil
}
/*
Given a folder returns a list of Test structs
*/
func ParseFolder(path string) ([]Test, error) {
files, err := ioutil.ReadDir(path)
tests := []Test{}
if err != nil {
return []Test{}, err
}
for _, f := range files {
fullPath := filepath.Join(path, f.Name())
fmt.Println(fullPath)
test, err := ParseTest(fullPath)
if err != nil {
return []Test{}, err
}
tests = append(tests, test)
}
return tests, nil
}
func SaveSQL(path string, sql string) error {
err := os.MkdirAll(filepath.Dir(path), os.ModePerm)
if err != nil {
return err
}
data := []byte(sql)
err = ioutil.WriteFile(path, data, 0644)
if err != nil {
return err
}
return nil
}