forked from GuiaBolso/darwin
-
Notifications
You must be signed in to change notification settings - Fork 11
/
parse.go
63 lines (50 loc) · 1.28 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
package darwin
import (
"bufio"
"strconv"
"strings"
)
// ParseMigrations takes a string that represents a text formatted set
// of migrations and parse them for use.
func ParseMigrations(s string) []Migration {
var migs []Migration
scanner := bufio.NewScanner(strings.NewReader(s))
scanner.Split(bufio.ScanLines)
var mig Migration
var script string
for scanner.Scan() {
v := scanner.Text()
lower := strings.ToLower(v)
switch {
case (len(v) >= 5 && lower[:5] == "--ver") || (len(v) >= 6 && lower[:6] == "-- ver"):
script = strings.TrimSpace(script)
script = strings.TrimRight(script, "\n")
mig.Script = script
migs = append(migs, mig)
mig = Migration{}
script = ""
parts := strings.Split(v, ":")
if len(parts) != 2 {
return nil
}
f, err := strconv.ParseFloat(strings.TrimSpace(parts[1]), 64)
if err != nil {
return nil
}
mig.Version = f
case (len(v) >= 5 && lower[:5] == "--des") || (len(v) >= 6 && lower[:6] == "-- des"):
parts := strings.Split(v, ":")
if len(parts) != 2 {
return nil
}
mig.Description = strings.TrimSpace(parts[1])
default:
script += v + "\n"
}
}
script = strings.TrimSpace(script)
script = strings.TrimRight(script, "\n")
mig.Script = script
migs = append(migs, mig)
return migs[1:]
}