/
changelog.go
126 lines (103 loc) · 2.84 KB
/
changelog.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
// As the loading process for change log
package changelog
import (
"bufio"
"fmt"
"io"
"io/ioutil"
"os"
"strings"
"gopkg.in/yaml.v2"
)
// As the patch configuration
type PatchConfig struct {
// The id of patch
Id string `yaml:"id"`
// The file name of patch
Filename string `yaml:"filename"`
// The comment of patch
Comment string `yaml:"comment"`
}
// Loads configurations of patches from a string
func LoadChangeLog(changeLogOfYaml []byte) (configOfPatches []PatchConfig, err error) {
/**
* Unmarshal the content YAML to PatchConfig
*/
configOfPatches = make([]PatchConfig, 0, 8)
if err = yaml.Unmarshal(changeLogOfYaml, &configOfPatches); err != nil {
configOfPatches = nil
return
}
// :~)
return
}
// Loads configurations of patches from a reader
func LoadChangeLogFromReader(readerOfchangeLog io.Reader) (configOfPatches []PatchConfig, err error) {
var yamlContent []byte
if yamlContent, err = ioutil.ReadAll(bufio.NewReader(readerOfchangeLog)); err != nil {
return
}
return LoadChangeLog(yamlContent)
}
// Loads configurations of patches from a file name(path)
// The file would be auto-closed by this method
func LoadChangeLogFromFile(changeLogFile string) (configOfPatches []PatchConfig, err error) {
var fileOfChangeLog *os.File
/**
* Reads change log of YAML
*/
if fileOfChangeLog, err = os.Open(changeLogFile); err != nil {
return nil, err
}
defer fileOfChangeLog.Close()
// :~)
return LoadChangeLogFromReader(fileOfChangeLog)
}
const ESCAPED_DELIMITER = "!DBPATCH!ESCAPED_DELIMITER!"
// Opens the file of patch and reads the script in it(splitted by delimiter)
func (patchConfig *PatchConfig) loadScripts(folderBase string, delimiter string) (scripts []string, err error) {
/**
* Open file
*/
var patchFile = patchConfig.Filename
if folderBase != "" {
patchFile = fmt.Sprintf("%s/%s", folderBase, patchFile)
}
var targetFile *os.File
if targetFile, err = os.Open(patchFile); err != nil {
return
}
defer targetFile.Close()
// :~)
/**
* Reads content of patch file and spilt the content by delimiter
* ** Replace the escaped delimiter with special string **
*/
var contentOfScript []byte
if contentOfScript, err = ioutil.ReadAll(targetFile); err != nil {
return
}
stringOfScript := string(contentOfScript)
stringOfScript = strings.Replace(stringOfScript, delimiter+delimiter, ESCAPED_DELIMITER, -1)
var rowScripts = strings.Split(
stringOfScript, delimiter,
)
// :~)
/**
* Skip the empty content of script
* e.x. CREATE TABLE xxx;;
*
* ** Replace back the escaped delimiters**
*/
scripts = make([]string, 0, len(rowScripts)-1)
for _, rowScript := range rowScripts {
rowScript = strings.TrimSpace(rowScript)
rowScript = strings.Replace(rowScript, ESCAPED_DELIMITER, delimiter, -1)
if rowScript == "" {
continue
}
scripts = append(scripts, rowScript)
}
// :~)
return
}