-
Notifications
You must be signed in to change notification settings - Fork 39
/
envfile.go
114 lines (94 loc) · 2.77 KB
/
envfile.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
// Copyright (C) 2022 Sneller, Inc.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
// Package helpers provides .env file parser. It is used
// by integration tests.
package helpers
import (
"bufio"
"fmt"
"os"
"strings"
)
type Settings struct {
Sneller SnellerSettings
Elasticsearch ElasticsearchSettings
}
type SnellerSettings struct {
Endpoint string
Token string
Database string
TableFlight, TableNews string
}
type ElasticsearchSettings struct {
Endpoint string
Username string
Password string
IndexFlight, IndexNews string
}
const path = "../docker/.env"
// ParseEnvFile parses .env files used in tests.
func ParseEnvFile() (*Settings, error) {
m, err := ReadEnvFile(path)
if err != nil {
return nil, err
}
env := &Settings{}
fields := []struct {
key string
val *string
}{
{"SNELLER_TOKEN", &env.Sneller.Token},
{"SNELLER_DATABASE", &env.Sneller.Database},
{"SNELLER_TABLE1", &env.Sneller.TableFlight},
{"SNELLER_TABLE2", &env.Sneller.TableNews},
{"ELASTIC_ENDPOINT", &env.Elasticsearch.Endpoint},
{"ELASTIC_PASSWORD", &env.Elasticsearch.Password},
{"ELASTIC_INDEX1", &env.Elasticsearch.IndexFlight},
{"ELASTIC_INDEX2", &env.Elasticsearch.IndexNews},
}
for i := range fields {
value, ok := m[fields[i].key]
if !ok {
return nil, fmt.Errorf("%s: field %q not found", path, fields[i].key)
}
*fields[i].val = value
}
// some hardcoded constants
env.Sneller.Endpoint = "http://localhost:9180"
const DefaultElasticUser = "elastic" // keep in sync with elastic-proxy/const.go
env.Elasticsearch.Username = DefaultElasticUser
return env, nil
}
func ReadEnvFile(path string) (map[string]string, error) {
env := make(map[string]string)
f, err := os.Open(path)
if err != nil {
return env, err
}
defer f.Close()
s := bufio.NewScanner(f)
n := 1
for s.Scan() {
line := s.Text()
key, val, ok := strings.Cut(line, "=")
if !ok {
return env, fmt.Errorf("%s:%d wrong syntax, expected 'key=value', got %q", path, n, line)
}
key = strings.TrimSpace(key)
val = strings.TrimSpace(val)
env[key] = val
n += 1
}
return env, s.Err()
}