-
Notifications
You must be signed in to change notification settings - Fork 1
/
io.go
69 lines (61 loc) · 1.48 KB
/
io.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
package cli
import (
"fmt"
"sort"
"strings"
)
type (
InputFuncMap map[string]func([]byte, interface{}) error
OutputFuncMap map[string]func(v interface{}) ([]byte, error)
)
var (
InputFuncs = InputFuncMap{
"json": JSONInput,
"yaml": YAMLInput,
"toml": TOMLInput,
"hcl1": HCL1Input,
"hcl2": HCL2Input,
"hcl": HCL2Input,
}
OutputFuncs = OutputFuncMap{
"json": JSONOutput,
"yaml": YAMLOutput,
"toml": TOMLOutput,
"go-template": GoTemplateOutput,
"jsonpath": JSONPathOutput,
}
)
// Input decodes raw bytes into the specified format and returns an object.
func Input(in []byte, format string) (interface{}, error) {
input, found := InputFuncs[strings.ToLower(format)]
if !found {
return nil, fmt.Errorf("unsupported input format: %s", format)
}
var v interface{}
err := input(in, &v)
return v, err
}
// Output encodes an object into the specified format and returns raw bytes.
func Output(obj interface{}, format string) ([]byte, error) {
output, found := OutputFuncs[strings.ToLower(format)]
if !found {
return nil, fmt.Errorf("unsupported output format: %s", format)
}
return output(obj)
}
func (m *InputFuncMap) Options() string {
keys := make(sort.StringSlice, 0)
for k := range *m {
keys = append(keys, k)
}
keys.Sort()
return strings.Join(keys, "|")
}
func (m *OutputFuncMap) Options() string {
keys := make(sort.StringSlice, 0)
for k := range *m {
keys = append(keys, k)
}
keys.Sort()
return strings.Join(keys, "|")
}