-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.go
More file actions
178 lines (128 loc) · 3.32 KB
/
parser.go
File metadata and controls
178 lines (128 loc) · 3.32 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package goenvirement
import (
"errors"
"fmt"
"io/fs"
"os"
"regexp"
"strings"
)
// parse one or more files , if files not provided is empty we default to .env
func parseOrDefault(files ...string) (map[string]string, error) {
// if no files provided we default to .env
if len(files) < 1 {
wd, err := os.Getwd()
if err != nil {
return nil, err
}
return parseFile(fmt.Sprintf("%v/.env", wd))
}
// if only one file provided no need to go through more steps
if len(files) == 1 {
return parseFile(files[0])
}
return parseMultipleFiles(files)
}
// parse the given files and merge them to a single map
func parseMultipleFiles(files []string) (map[string]string, error) {
env := make(map[string]string)
for _, file := range files {
// we parse each file at once
fileEnvs, err := parseFile(file)
if err != nil {
return nil, err
}
// we merge the envs from each file to a single map
for key, value := range fileEnvs {
env[key] = value
}
}
return env, nil
}
// parse the given file and return it as a map
func parseFile(file string) (map[string]string, error) {
if !fileExists(file) {
return nil, &fs.PathError{
Op: "parsing the env file",
Path: file,
Err: errors.New("file doesn't exists"),
}
}
content, err := getFileContent(file)
if err != nil {
return nil, err
}
return buildKeyValuePairs(content)
}
// check if a file exists or not
func fileExists(file string) bool {
_, err := os.Stat(file)
return err == nil
}
// get the given file content , if something goes wrong
// the error will be returned with empty string
func getFileContent(file string) (string, error) {
content, err := os.ReadFile(file)
if err != nil {
return "", err
}
return string(content), nil
}
func buildKeyValuePairs(s string) (map[string]string, error) {
lines := strings.Split(s, "\n")
pairs := make(map[string]string)
for _, line := range lines {
line = strings.TrimSpace(line)
if isComment(line) || line == "" {
continue
}
key, value, err := splitKeyValue(line)
if err != nil {
return nil, err
}
pairs[key] = value
}
return pairs, nil
}
func splitKeyValue(l string) (key string, value string, err error) {
pair := strings.SplitN(l, "=", 2)
if len(pair) != 2 {
return "", "", fmt.Errorf("invalid syntax [%s] ", l)
}
key = strings.TrimSpace(pair[0])
value = strings.TrimSpace(strings.Split(pair[1], "#")[0])
if err = validateKeyValuePair(key, value); err != nil {
return "", "", err
}
if strings.HasPrefix(value, `"`) && strings.HasSuffix(value, `"`) {
value = strings.Trim(value, "\"")
}
return key, value, nil
}
func validateKeyValuePair(key, value string) error {
if !isValidKey(key) {
return fmt.Errorf("invalid key [%s] , the key should only contains letters, numbers and underscore", key)
}
if !isValidValue(value) {
return fmt.Errorf("invalid value [%s] for key [%s]", value, key)
}
return nil
}
func isValidKey(key string) bool {
rx := regexp.MustCompile(`^[A-z_0-9]+$`)
return rx.MatchString(key)
}
func isValidValue(value string) bool {
var rx *regexp.Regexp
if strings.HasPrefix(value, `"`) {
rx = regexp.MustCompile(`^"[^"]+"$`)
} else {
rx = regexp.MustCompile(`^.*$`)
}
return rx.MatchString(value)
}
func isComment(l string) bool {
rx := regexp.MustCompile(`^(\s)*#[^\n]*$`)
return rx.MatchString(l)
}
// func marshal()