forked from casualjim/yaml2json
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
118 lines (103 loc) · 2.68 KB
/
main.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package main
import (
"encoding/json"
"flag"
"fmt"
"github.com/casualjim/yaml2json/src"
"io/ioutil"
"log"
"net/http"
"os"
"strings"
"time"
)
// LoadHTTPTimeout the default timeout for load requests
const LoadHTTPTimeout = 30 * time.Second
var (
output string
flagSet = flag.NewFlagSet("", flag.ExitOnError)
)
func init() {
flagSet.StringVar(&output, "output", "", "Write to the file instead of to stdout")
}
func main() {
flagSet.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage of %s:\n\n", os.Args[0])
fmt.Fprintf(os.Stderr, " %s [YAML FILE OR URL]\n\n", os.Args[0])
flagSet.PrintDefaults()
}
if len(os.Args) < 2 {
fmt.Fprintln(os.Stderr, "you need to provide the file path or url to load")
os.Exit(1)
}
flagSet.Parse(os.Args[2:])
json, err := YAMLDoc(os.Args[1])
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
}
if output == "" {
fmt.Println(string(json))
} else {
err := ioutil.WriteFile(output, json, 0600) // umask settings will be respected this way
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
}
}
}
// LoadFromFileOrHTTP loads the bytes from a file or a remote http server based on the path passed in
func LoadFromFileOrHTTP(path string) ([]byte, error) {
return LoadStrategy(path, ioutil.ReadFile, loadHTTPBytes(LoadHTTPTimeout))(path)
}
// LoadStrategy returns a loader function for a given path or uri
func LoadStrategy(path string, local, remote func(string) ([]byte, error)) func(string) ([]byte, error) {
if strings.HasPrefix(path, "http") {
return remote
}
return local
}
func loadHTTPBytes(timeout time.Duration) func(path string) ([]byte, error) {
return func(path string) ([]byte, error) {
client := &http.Client{Timeout: timeout}
req, err := http.NewRequest("GET", path, nil)
if err != nil {
return nil, err
}
resp, err := client.Do(req)
defer func() {
if resp != nil {
if e := resp.Body.Close(); e != nil {
log.Println(e)
}
}
}()
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("could not access document at %q [%s] ", path, resp.Status)
}
return ioutil.ReadAll(resp.Body)
}
}
// YAMLDoc loads a yaml document from either http or a file and converts it to json
func YAMLDoc(path string) (json.RawMessage, error) {
yamlDoc, err := YAMLData(path)
if err != nil {
return nil, err
}
data, err := yaml2json.YAMLToJSON(yamlDoc)
if err != nil {
return nil, err
}
return data, nil
}
// YAMLData loads a yaml document from either http or a file
func YAMLData(path string) (interface{}, error) {
data, err := LoadFromFileOrHTTP(path)
if err != nil {
return nil, err
}
return yaml2json.BytesToYAMLDoc(data)
}