This repository has been archived by the owner on Jan 8, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
166 lines (145 loc) · 3.7 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package main
import (
"bufio"
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
"github.com/BurntSushi/toml"
"github.com/clbanning/mxj"
"github.com/docopt/docopt-go"
"github.com/ghodss/yaml"
"gopkg.in/ini.v1"
)
var (
version = "dev"
)
func readData(filename string) (bytes []byte, err error) {
if filename == "-" {
bytes, err = ioutil.ReadAll(os.Stdin)
} else {
bytes, err = ioutil.ReadFile(filename)
}
return bytes, err
}
func loadIni(input_data []byte) interface{} {
cfg, err := ini.Load(input_data)
if err != nil {
panic(err)
}
output := map[string]interface{}{}
for k, v := range cfg.Section("").KeysHash() {
output[k] = v
}
for _, section := range cfg.Sections() {
if section.Name() == ini.DefaultSection {
continue
}
output[section.Name()] = section.KeysHash()
}
return output
}
func saveIni(input interface{}) ([]byte, error) {
i := ini.Empty()
err := i.Append(map[string]string{"foo": "bar"})
if err != nil {
panic(err)
}
w := new(bufio.Writer)
r := new(bufio.Reader)
b := bufio.NewReadWriter(r, w)
_, err = i.WriteTo(b)
if err != nil {
panic(err)
}
return ioutil.ReadAll(b)
}
func parseData(input_data []byte, format string) (parsed_data interface{}, err error) {
if format == "json" {
err = json.Unmarshal(input_data, &parsed_data)
} else if format == "yaml" || format == "yml" {
err = yaml.Unmarshal(input_data, &parsed_data)
} else if format == "toml" {
err = toml.Unmarshal(input_data, &parsed_data)
} else if format == "xml" {
// TODO: Is this the right way to handle xml?
m, err := mxj.NewMapXml(input_data)
return m, err
} else if format == "ini" {
parsed_data = loadIni(input_data)
} else {
panic("Non support inport format")
}
return parsed_data, err
}
func outputData(input_data interface{}, format string) (output_data []byte, err error) {
if format == "json" {
output_data, err = json.MarshalIndent(input_data, "", " ")
} else if format == "yaml" || format == "yml" {
output_data, err = yaml.Marshal(input_data)
} else if format == "toml" {
var buf bytes.Buffer
toml_enc := toml.NewEncoder(&buf)
err = toml_enc.Encode(input_data)
output_data = buf.Bytes()
} else if format == "xml" {
output_data, err = mxj.AnyXmlIndent(input_data, "", "\t")
} else if format == "ini" {
output_data, err = saveIni(input_data)
} else {
panic("Non supported output format")
}
return output_data, err
}
func detectInputFormat(data []byte) (detected_format string) {
// TODO: Some sort of input detection
return "yaml"
}
func parseArgs() map[string]interface{} {
usage := `uq - Universal (de)serializer
Usage:
uq [-v] [-s FORMAT] [-t FORMAT] [FILE]
Options:
-s <FORMAT>, --source <FORMAT> Specify input format. [default: auto]
-t <FORMAT>, --target <FORMAT> Specify output format. [default: json]
-v, --verbose Be more verbose [default: false]
-h, --help Show this screen
--version Show version
Formats:
* json
* yaml|yml
* toml
* ini
* xml (Note: xml won't be a perfect conversion)
`
arguments, _ := docopt.Parse(usage, nil, true, version, false)
return arguments
}
func main() {
args := parseArgs()
verbose := args["--verbose"].(bool)
if verbose {
log.Printf("Debug: args - %q", args)
}
var filename string
if args["FILE"] == nil {
filename = "-"
} else {
filename = args["FILE"].(string)
}
data, _ := readData(filename)
var input_format string
if args["--source"].(string) == "auto" {
input_format = detectInputFormat(data)
} else {
input_format = args["--source"].(string)
}
parsed_data, err := parseData(data, input_format)
if err != nil {
log.Fatal(err)
}
output_data, _ := outputData(parsed_data, args["--target"].(string))
fmt.Println(string(output_data))
}