-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile.go
More file actions
53 lines (42 loc) · 922 Bytes
/
file.go
File metadata and controls
53 lines (42 loc) · 922 Bytes
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
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
)
// PrintJSON pretty print JSON string
func printJSON(str string) {
var prettyJSON bytes.Buffer
error := json.Indent(&prettyJSON, []byte(str), "", " ")
if error != nil {
log.Fatal("JSON parse error: ", error)
}
fmt.Println(string(prettyJSON.Bytes()))
return
}
// OutputJSON displays output of query for alerts in JSON format
func outputJSON(input interface{}) {
b, err := json.Marshal(input)
if err != nil {
log.Fatal(err)
}
printJSON(string(b))
}
// readFileBytes
func readFileBytes(fpath string) []byte {
log.Printf("Openning file %s", fpath)
jsonFile, err := os.Open(fpath)
if err != nil {
log.Fatal(err)
}
// defer the closing of our jsonFile so that we can parse it later on
defer jsonFile.Close()
byteValue, err := ioutil.ReadAll(jsonFile)
if err != nil {
log.Fatal(err)
}
return byteValue
}