-
Notifications
You must be signed in to change notification settings - Fork 0
/
files.go
98 lines (70 loc) · 1.86 KB
/
files.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
package lib
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
)
// Files contains functionality pertaining to files
type Files struct {
}
// FetchLocalChangesetList gets the contents of the changesets.json file
// and returns a collection of paths ([]string)
func (f *Files) FetchLocalChangesetList(rootPath string) (sqlPaths []string, e error) {
changesetJSONPath := rootPath + "/changesets.json"
// fmt.Printf("Looking for changeset file at path %s\n", changesetJSONPath)
if _, e = os.Stat(changesetJSONPath); os.IsNotExist(e) {
return
}
var raw []byte
raw, e = ioutil.ReadFile(changesetJSONPath)
if e != nil {
return
}
sqlPaths = []string{}
json.Unmarshal(raw, &sqlPaths)
for idx, sqlPath := range sqlPaths {
sqlPaths[idx] = rootPath + "/" + sqlPath
}
return
}
// BuildChangeFiles returns a collection of ChangeFile objects based on
// a collection of sql paths
func (f *Files) BuildChangeFiles(sqlPaths []string) (changeFiles []ChangeFile, e error) {
if len(sqlPaths) == 0 {
return
}
changeFiles = []ChangeFile{}
ordinal := 0
for _, sqlPath := range sqlPaths {
ordinal = ordinal + 1
var changeFile *ChangeFile
if changeFile, e = f.BuildChangeFile(sqlPath, ordinal); e != nil {
return
}
changeFiles = append(changeFiles, *changeFile)
}
return
}
// BuildChangeFile builds a changeFile object based on a path and an ordinal
func (f *Files) BuildChangeFile(sqlPath string, ordinal int) (changeFile *ChangeFile, e error) {
var content []byte
if _, e = os.Stat(sqlPath); os.IsNotExist(e) {
e = fmt.Errorf("Missing file in changeset: `%s`", sqlPath)
return
}
if content, e = ioutil.ReadFile(sqlPath); e != nil {
return
}
var hash string
if hash, e = HashFileMd5(sqlPath); e != nil {
return
}
changeFile = &ChangeFile{
Name: sqlPath,
Content: string(content),
Ordinal: ordinal,
Hash: hash,
}
return
}