-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
debugvars.go
75 lines (68 loc) · 1.99 KB
/
debugvars.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
// Copyright 2015, Google Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package framework
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strings"
)
// FetchJSON fetches JSON content from the specified URL path and returns it
// as a map. The function returns an empty map on error.
func FetchJSON(urlPath string) map[string]interface{} {
out := map[string]interface{}{}
response, err := http.Get(fmt.Sprintf("%s%s", ServerAddress, urlPath))
if err != nil {
return out
}
defer response.Body.Close()
_ = json.NewDecoder(response.Body).Decode(&out)
return out
}
// DebugVars parses /debug/vars and returns a map. The function returns
// an empty map on error.
func DebugVars() map[string]interface{} {
return FetchJSON("/debug/vars")
}
// FetchInt fetches the specified slash-separated tag and returns the
// value as an int. It returns 0 on error, or if not found.
func FetchInt(vars map[string]interface{}, tags string) int {
val, _ := FetchVal(vars, tags).(float64)
return int(val)
}
// FetchVal fetches the specified slash-separated tag and returns the
// value as an interface. It returns nil on error, or if not found.
func FetchVal(vars map[string]interface{}, tags string) interface{} {
splitTags := strings.Split(tags, "/")
if len(tags) == 0 {
return nil
}
current := vars
for _, tag := range splitTags[:len(splitTags)-1] {
icur, ok := current[tag]
if !ok {
return nil
}
current, ok = icur.(map[string]interface{})
if !ok {
return nil
}
}
return current[splitTags[len(splitTags)-1]]
}
// FetchURL fetches the content from the specified URL path and returns it
// as a string. The function returns an empty string on error.
func FetchURL(urlPath string) string {
response, err := http.Get(fmt.Sprintf("%s%s", ServerAddress, urlPath))
if err != nil {
return ""
}
defer response.Body.Close()
b, err := ioutil.ReadAll(response.Body)
if err != nil {
return ""
}
return string(b)
}