Skip to content

Commit 8f70a8f

Browse files
committed
Support jsonc
1 parent 21abf9a commit 8f70a8f

File tree

6 files changed

+82
-32
lines changed

6 files changed

+82
-32
lines changed

README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,9 @@ go install github.com/script-development/rtcv_scraper_client/[email protected]
5555
5656
Use [gen-env](https://bitbucket.org/teamscript/cli-tools/src/main/gen-env/) to generate a `env.json` file.
5757
58-
#### `2.1.` Mocking RTCV
58+
The env.json parser supports jsonc (json with comments) so you can comment out sections if you so desire and even rename the `env.json` to `env.jsonc`.
59+
60+
#### *2.1.* Mocking RTCV
5961
6062
When developing a scraper you might want to mock RT-CV so you don't have to relay on another service.
6163

env.go

+70
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,80 @@
11
package main
22

33
import (
4+
"encoding/json"
45
"errors"
56
"fmt"
7+
"io/ioutil"
8+
"log"
9+
"os"
10+
11+
"muzzammil.xyz/jsonc"
612
)
713

14+
var customEnvFileVarName = "RTCV_SCRAPER_CLIENT_ENV_FILE"
15+
var envAsVarName = "RTCV_SCRAPER_CLIENT_ENV"
16+
17+
func mustReadEnv() Env {
18+
envFilename := "env.json"
19+
alternativeFileName := os.Getenv(customEnvFileVarName)
20+
if alternativeFileName != "" {
21+
envFilename = alternativeFileName
22+
}
23+
24+
notFoundErr := func() string {
25+
if alternativeFileName != "" {
26+
return "no " + alternativeFileName + " file or $" + envAsVarName + " environment variable found, cannot continue"
27+
}
28+
return "no env.json(c) file or $" + envAsVarName + " environment variable found, cannot continue"
29+
}
30+
31+
envFileBytes, err := ioutil.ReadFile(envFilename)
32+
if err == nil {
33+
return mustParseEnv(envFileBytes)
34+
}
35+
36+
envFileBytes = []byte(os.Getenv(envAsVarName))
37+
if len(envFileBytes) != 0 {
38+
return mustParseEnv(envFileBytes)
39+
}
40+
41+
if !os.IsNotExist(err) {
42+
log.Fatal("unable to read env file, error: " + err.Error())
43+
}
44+
45+
if alternativeFileName != "" {
46+
log.Fatalln(notFoundErr())
47+
}
48+
49+
envFilename = "env.jsonc"
50+
envFileBytes, err = ioutil.ReadFile(envFilename)
51+
if err != nil {
52+
log.Fatalln(notFoundErr())
53+
}
54+
55+
return mustParseEnv(envFileBytes)
56+
}
57+
58+
func mustParseEnv(b []byte) Env {
59+
envJSON := jsonc.ToJSON(b)
60+
if !json.Valid(envJSON) {
61+
log.Fatal("env file is not valid json or jsonc")
62+
}
63+
64+
env := Env{}
65+
err := json.Unmarshal(envJSON, &env)
66+
if err != nil {
67+
log.Fatal("unable to parse env file, error: " + err.Error())
68+
}
69+
70+
err = env.validate()
71+
if err != nil {
72+
log.Fatal("validating env failed, error: " + err.Error())
73+
}
74+
75+
return env
76+
}
77+
878
// Env contains the structure of an env.json file
979
type Env struct {
1080
PrivateKey string `json:"private_key"`

examples/deno/env.json renamed to examples/deno/env.jsonc

+1
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@
77
"api_key": "bbb"
88
},
99
"alternative_servers": []
10+
// "mock_mode": true
1011
}

go.mod

+4-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ go 1.18
44

55
require github.com/valyala/fasthttp v1.40.0
66

7-
require github.com/gorilla/websocket v1.5.0 // indirect
7+
require (
8+
github.com/gorilla/websocket v1.5.0 // indirect
9+
muzzammil.xyz/jsonc v1.0.0 // indirect
10+
)
811

912
require (
1013
github.com/andybalholm/brotli v1.0.4 // indirect

go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,5 @@ golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuX
3333
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
3434
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
3535
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
36+
muzzammil.xyz/jsonc v1.0.0 h1:B6kaT3wHueZ87mPz3q1nFuM1BlL32IG0wcq0/uOsQ18=
37+
muzzammil.xyz/jsonc v1.0.0/go.mod h1:rFv8tUUKe+QLh7v02BhfxXEf4ZHhYD7unR93HL/1Uvo=

main.go

+2-30
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
package main
22

33
import (
4-
"encoding/json"
54
"fmt"
6-
"io/ioutil"
75
"log"
86
"os"
97
"os/exec"
@@ -14,34 +12,7 @@ import (
1412
)
1513

1614
func main() {
17-
envFilename := "env.json"
18-
alternativeFileName := os.Getenv("RTCV_SCRAPER_CLIENT_ENV_FILE")
19-
if alternativeFileName != "" {
20-
envFilename = alternativeFileName
21-
}
22-
23-
envEnvName := "RTCV_SCRAPER_CLIENT_ENV"
24-
envFile, err := ioutil.ReadFile(envFilename)
25-
if err != nil {
26-
if !os.IsNotExist(err) {
27-
log.Fatal("unable to read env file, error: " + err.Error())
28-
}
29-
envFile = []byte(os.Getenv(envEnvName))
30-
if len(envFile) == 0 {
31-
log.Fatalf("no %s file or %s environment variable found, cannot continue", envFilename, envEnvName)
32-
}
33-
}
34-
35-
env := Env{}
36-
err = json.Unmarshal(envFile, &env)
37-
if err != nil {
38-
log.Fatal("unable to parse env file, error: " + err.Error())
39-
}
40-
41-
err = env.validate()
42-
if err != nil {
43-
log.Fatal("validating env failed, error: " + err.Error())
44-
}
15+
env := mustReadEnv()
4516

4617
api := NewAPI()
4718

@@ -50,6 +21,7 @@ func main() {
5021
credentials = append(credentials, server.toCredArg(false))
5122
}
5223

24+
var err error
5325
var loginUsers []EnvUser
5426
if !env.MockMode {
5527
err = api.SetCredentials(credentials)

0 commit comments

Comments
 (0)