-
Notifications
You must be signed in to change notification settings - Fork 4
/
utils.go
66 lines (56 loc) · 1.02 KB
/
utils.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
package main
import (
"fmt"
"io/ioutil"
"os"
"testing"
"github.com/Jeffail/gabs/v2"
)
func readInput(f string) (*gabs.Container, error) {
var bytes []byte
if f != "" {
debug.Printf("Attempting to read file %s", f)
b, err := ioutil.ReadFile(f)
if err != nil {
return nil, err
}
bytes = b
} else {
stat, _ := os.Stdin.Stat()
if stat.Mode()&os.ModeNamedPipe != 0 {
debug.Printf("Attempting to read from stdin")
b, err := ioutil.ReadAll(os.Stdin)
if err != nil {
return nil, err
}
bytes = b
}
}
if len(bytes) == 0 {
e := fmt.Errorf("No input received.")
return nil, e
}
jsonParsed, err := gabs.ParseJSON(bytes)
if err != nil {
return nil, err
}
return jsonParsed, nil
}
func getDelimiter(d string) string {
if d != "" {
return d
} else {
return "."
}
}
func errAndExit(e error) {
fmt.Printf("Error: %s\n", e)
os.Exit(1)
}
func readTestFile(t *testing.T, file string) *gabs.Container {
j, err := readInput("test.json")
if err != nil {
t.Fatal(err)
}
return j
}