/
helpers.go
72 lines (61 loc) · 1.52 KB
/
helpers.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
// nmon2influxdb
// import nmon data in InfluxDB
// author = adejoux@djouxtech.net
package nmon2influxdblib
import (
"bytes"
"encoding/json"
"fmt"
"log"
"net/http"
"net/http/httputil"
"strings"
)
//
//helper functions
//CheckError check error message and display it
func CheckError(e error) {
if e != nil {
log.Fatal(e)
}
}
// CheckInfo wrap info message
func CheckInfo(e error) {
if e != nil {
log.Printf("info: %s", e)
}
}
//ReplaceComma replaces comma by html tabs tag
func ReplaceComma(s string) string {
return "<tr><td>" + strings.Replace(s, ",", "</td><td>", 1) + "</td></tr>"
}
//SPrintHTTPResponse print raw http response for debugging purpose
func SPrintHTTPResponse(response *http.Response) string {
responseDump, err := httputil.DumpResponse(response, true)
if err != nil {
fmt.Println(err)
}
return string(responseDump)
}
//SPrintHTTPRequest print raw http request for debugging purpose
func SPrintHTTPRequest(request *http.Request) string {
requestDump, err := httputil.DumpRequest(request, true)
if err != nil {
log.Println(err)
}
return string(requestDump)
}
//SPrintPrettyJSON helper used to display JSON output in a nicer way
func SPrintPrettyJSON(contents []byte) string {
text := GetPrettyJSON(contents)
return string(text.Bytes())
}
//GetPrettyJSON returns pretty json string
func GetPrettyJSON(contents []byte) bytes.Buffer {
var prettyJSON bytes.Buffer
error := json.Indent(&prettyJSON, contents, "", "\t")
if error != nil {
log.Println("JSON parse error: ", error)
}
return prettyJSON
}