-
Notifications
You must be signed in to change notification settings - Fork 0
/
env.go
159 lines (133 loc) · 4.03 KB
/
env.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
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
package main
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
"os"
"muzzammil.xyz/jsonc"
)
var customEnvFileVarName = "RTCV_SCRAPER_CLIENT_ENV_FILE"
var envAsVarName = "RTCV_SCRAPER_CLIENT_ENV"
func mustReadEnv() Env {
envFilename := "env.json"
alternativeFileName := os.Getenv(customEnvFileVarName)
if alternativeFileName != "" {
envFilename = alternativeFileName
}
notFoundErr := func() string {
if alternativeFileName != "" {
return "no " + alternativeFileName + " file or $" + envAsVarName + " environment variable found, cannot continue"
}
return "no env.json(c) file or $" + envAsVarName + " environment variable found, cannot continue"
}
envFileBytes, err := ioutil.ReadFile(envFilename)
if err == nil {
return mustParseEnv(envFileBytes)
}
envFileBytes = []byte(os.Getenv(envAsVarName))
if len(envFileBytes) != 0 {
return mustParseEnv(envFileBytes)
}
if !os.IsNotExist(err) {
log.Fatal("unable to read env file, error: " + err.Error())
}
if alternativeFileName != "" {
log.Fatalln(notFoundErr())
}
envFilename = "env.jsonc"
envFileBytes, err = ioutil.ReadFile(envFilename)
if err != nil {
log.Fatalln(notFoundErr())
}
return mustParseEnv(envFileBytes)
}
func mustParseEnv(b []byte) Env {
envJSON := jsonc.ToJSON(b)
if !json.Valid(envJSON) {
log.Fatal("env file is not valid json or jsonc")
}
env := Env{}
err := json.Unmarshal(envJSON, &env)
if err != nil {
log.Fatal("unable to parse env file, error: " + err.Error())
}
err = env.validate()
if err != nil {
log.Fatal("validating env failed, error: " + err.Error())
}
return env
}
// Env contains the structure of an env.json file
type Env struct {
PrivateKey string `json:"private_key"`
PublicKey string `json:"public_key"`
PrimaryServer EnvServer `json:"primary_server"`
AlternativeServers []EnvServer `json:"alternative_servers"`
MockMode bool `json:"mock_mode"`
MockUsers []EnvUser `json:"mock_users"`
}
func (e *Env) validate() error {
if e.MockMode {
if len(e.MockUsers) == 0 {
fmt.Println(`"mock_users" is empty in env.json, most scrapers require at least one user to login.`)
fmt.Println(`For documentation about mocking see https://github.com/script-development/rtcv_scraper_client`)
if e.MockUsers == nil {
e.MockUsers = []EnvUser{}
}
}
return nil
}
err := e.PrimaryServer.validate()
if err != nil {
return fmt.Errorf("primary_server.%s", err.Error())
}
for idx, server := range e.AlternativeServers {
err := server.validate()
if err != nil {
return fmt.Errorf("%s[%d].%s", "alternative_servers", idx, err.Error())
}
}
keyPairHelpMsg := `, use the go program inside the "gen_key" folder to generate a key pair`
if e.PrivateKey == "" && e.PublicKey == "" {
return errors.New(`"public_key" and "private_key" are required` + keyPairHelpMsg)
} else if e.PrivateKey == "" {
return errors.New(`"private_key" required` + keyPairHelpMsg)
} else if e.PublicKey == "" {
return errors.New(`"public_key" required` + keyPairHelpMsg)
}
return nil
}
// EnvServer contains the structure of the primary_server and alternative_servers inside the .env file
type EnvServer struct {
ServerLocation string `json:"server_location"`
APIKeyID string `json:"api_key_id"`
APIKey string `json:"api_key"`
}
func (e *EnvServer) validate() error {
if e.ServerLocation == "" {
return errors.New("server_location is required")
}
if e.APIKeyID == "" {
return errors.New("api_key_id is required")
}
if e.APIKey == "" {
return errors.New("api_key is required")
}
return nil
}
func (e *EnvServer) toCredArg(isPrimary bool) SetCredentialsArg {
return SetCredentialsArg{
ServerLocation: e.ServerLocation,
APIKeyID: e.APIKeyID,
APIKey: e.APIKey,
Primary: isPrimary,
}
}
// EnvUser contains the structure of the login_users inside the .env file
type EnvUser struct {
Username string `json:"username"`
Password string `json:"password"`
EncryptedPassword string `json:"encryptedPassword,omitempty"`
}