diff options
author | appilon <apilon@hashicorp.com> | 2019-02-27 16:43:31 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-02-27 16:43:31 -0500 |
commit | 844b5a68d8af4791755b8f0ad293cc99f5959183 (patch) | |
tree | 255c250a5c9d4801c74092d33b7337d8c14438ff /vendor/github.com/hashicorp/go-plugin/log_entry.go | |
parent | 303b299eeb6b06e939e35905e4b34cb410dd9dc3 (diff) | |
parent | 15c0b25d011f37e7c20aeca9eaf461f78285b8d9 (diff) | |
download | terraform-provider-statuscake-844b5a68d8af4791755b8f0ad293cc99f5959183.tar.gz terraform-provider-statuscake-844b5a68d8af4791755b8f0ad293cc99f5959183.tar.zst terraform-provider-statuscake-844b5a68d8af4791755b8f0ad293cc99f5959183.zip |
Merge pull request #27 from terraform-providers/go-modules-2019-02-22
[MODULES] Switch to Go Modules
Diffstat (limited to 'vendor/github.com/hashicorp/go-plugin/log_entry.go')
-rw-r--r-- | vendor/github.com/hashicorp/go-plugin/log_entry.go | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/vendor/github.com/hashicorp/go-plugin/log_entry.go b/vendor/github.com/hashicorp/go-plugin/log_entry.go new file mode 100644 index 0000000..2996c14 --- /dev/null +++ b/vendor/github.com/hashicorp/go-plugin/log_entry.go | |||
@@ -0,0 +1,73 @@ | |||
1 | package plugin | ||
2 | |||
3 | import ( | ||
4 | "encoding/json" | ||
5 | "time" | ||
6 | ) | ||
7 | |||
8 | // logEntry is the JSON payload that gets sent to Stderr from the plugin to the host | ||
9 | type logEntry struct { | ||
10 | Message string `json:"@message"` | ||
11 | Level string `json:"@level"` | ||
12 | Timestamp time.Time `json:"timestamp"` | ||
13 | KVPairs []*logEntryKV `json:"kv_pairs"` | ||
14 | } | ||
15 | |||
16 | // logEntryKV is a key value pair within the Output payload | ||
17 | type logEntryKV struct { | ||
18 | Key string `json:"key"` | ||
19 | Value interface{} `json:"value"` | ||
20 | } | ||
21 | |||
22 | // flattenKVPairs is used to flatten KVPair slice into []interface{} | ||
23 | // for hclog consumption. | ||
24 | func flattenKVPairs(kvs []*logEntryKV) []interface{} { | ||
25 | var result []interface{} | ||
26 | for _, kv := range kvs { | ||
27 | result = append(result, kv.Key) | ||
28 | result = append(result, kv.Value) | ||
29 | } | ||
30 | |||
31 | return result | ||
32 | } | ||
33 | |||
34 | // parseJSON handles parsing JSON output | ||
35 | func parseJSON(input string) (*logEntry, error) { | ||
36 | var raw map[string]interface{} | ||
37 | entry := &logEntry{} | ||
38 | |||
39 | err := json.Unmarshal([]byte(input), &raw) | ||
40 | if err != nil { | ||
41 | return nil, err | ||
42 | } | ||
43 | |||
44 | // Parse hclog-specific objects | ||
45 | if v, ok := raw["@message"]; ok { | ||
46 | entry.Message = v.(string) | ||
47 | delete(raw, "@message") | ||
48 | } | ||
49 | |||
50 | if v, ok := raw["@level"]; ok { | ||
51 | entry.Level = v.(string) | ||
52 | delete(raw, "@level") | ||
53 | } | ||
54 | |||
55 | if v, ok := raw["@timestamp"]; ok { | ||
56 | t, err := time.Parse("2006-01-02T15:04:05.000000Z07:00", v.(string)) | ||
57 | if err != nil { | ||
58 | return nil, err | ||
59 | } | ||
60 | entry.Timestamp = t | ||
61 | delete(raw, "@timestamp") | ||
62 | } | ||
63 | |||
64 | // Parse dynamic KV args from the hclog payload. | ||
65 | for k, v := range raw { | ||
66 | entry.KVPairs = append(entry.KVPairs, &logEntryKV{ | ||
67 | Key: k, | ||
68 | Value: v, | ||
69 | }) | ||
70 | } | ||
71 | |||
72 | return entry, nil | ||
73 | } | ||