forked from mattn/gom
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgomfile.go
More file actions
160 lines (148 loc) · 3.5 KB
/
gomfile.go
File metadata and controls
160 lines (148 loc) · 3.5 KB
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
package main
import (
"bufio"
"fmt"
"io"
"os"
"regexp"
"runtime"
"strings"
)
var qx = `'[^']*'|"[^"]*"`
var kx = `:[a-z][a-z0-9_]*`
var ax = `(?:\s*` + kx + `\s*|,\s*` + kx + `\s*)`
var re_group = regexp.MustCompile(`\s*group\s+((?:` + kx + `\s*|,\s*` + kx + `\s*)*)\s*do\s*$`)
var re_end = regexp.MustCompile(`\s*end\s*$`)
var re_gom = regexp.MustCompile(`^\s*gom\s+(` + qx + `)\s*((?:,\s*` + kx + `\s*=>\s*(?:` + qx + `|\s*\[\s*` + ax + `*\s*\]\s*))*)$`)
var re_options = regexp.MustCompile(`(,\s*` + kx + `\s*=>\s*(?:` + qx + `|\s*\[\s*` + ax + `*\s*\]\s*)\s*)`)
func unquote(name string) string {
name = strings.TrimSpace(name)
if len(name) > 2 {
if (name[0] == '\'' && name[len(name)-1] == '\'') || (name[0] == '"' && name[len(name)-1] == '"') {
return name[1 : len(name)-1]
}
}
return name
}
func matchOS(any interface{}) bool {
var envs []string
if as, ok := any.([]string); ok {
envs = as
} else if s, ok := any.(string); ok {
envs = []string{s}
} else {
return false
}
if has(envs, runtime.GOOS) {
return true
}
return false
}
func matchEnv(any interface{}) bool {
var envs []string
if as, ok := any.([]string); ok {
envs = as
} else if s, ok := any.(string); ok {
envs = []string{s}
} else {
return false
}
switch {
case has(envs, "production") && *productionEnv:
return true
case has(envs, "development") && *developmentEnv:
return true
case has(envs, "test") && *testEnv:
return true
}
return false
}
func parseOptions(line string, options map[string]interface{}) {
ss := re_options.FindAllStringSubmatch(line, -1)
re_a := regexp.MustCompile(ax)
for _, s := range ss {
kvs := strings.SplitN(strings.TrimSpace(s[0])[1:], "=>", 2)
kvs[0], kvs[1] = strings.TrimSpace(kvs[0]), strings.TrimSpace(kvs[1])
if kvs[1][0] == '[' {
as := re_a.FindAllStringSubmatch(kvs[1][1:len(kvs[1])-1], -1)
a := []string{}
for i := range as {
it := strings.TrimSpace(as[i][0])
if strings.HasPrefix(it, ",") {
it = strings.TrimSpace(it[1:])
}
if strings.HasPrefix(it, ":") {
it = strings.TrimSpace(it[1:])
}
a = append(a, it)
}
options[kvs[0][1:]] = a
} else {
options[kvs[0][1:]] = unquote(kvs[1])
}
}
}
type Gom struct {
name string
options map[string]interface{}
}
func parseGomfile(filename string) ([]Gom, error) {
f, err := os.Open(filename)
if err != nil {
return nil, err
}
br := bufio.NewReader(f)
goms := make([]Gom, 0)
n := 0
skip := 0
valid := true
for {
n++
lb, _, err := br.ReadLine()
if err != nil {
if err == io.EOF {
return goms, nil
}
return nil, err
}
line := strings.TrimSpace(string(lb))
if line == "" || strings.HasPrefix(line, "#") {
continue
}
name := ""
options := make(map[string]interface{})
var items []string
if re_group.MatchString(line) {
envs := strings.Split(re_group.FindStringSubmatch(line)[1], ",")
for i := range envs {
envs[i] = strings.TrimSpace(envs[i])[1:]
}
if matchEnv(envs) {
valid = true
continue
}
valid = false
skip++
continue
} else if re_end.MatchString(line) {
if !valid {
skip--
if skip < 0 {
return nil, fmt.Errorf("Syntax Error at line %d", n)
}
}
valid = false
continue
} else if skip > 0 {
continue
} else if re_gom.MatchString(line) {
items = re_gom.FindStringSubmatch(line)[1:]
name = unquote(items[0])
parseOptions(items[1], options)
} else {
return nil, fmt.Errorf("Syntax Error at line %d", n)
}
goms = append(goms, Gom{name, options})
}
return goms, nil
}