-
Notifications
You must be signed in to change notification settings - Fork 4
/
toml-merge.go
68 lines (56 loc) · 1.24 KB
/
toml-merge.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
package main
import (
"encoding/json"
"flag"
"log"
"os"
"path"
"github.com/BurntSushi/toml"
)
var (
flagOutputJSON = false
flagShowVersion = false
version = "99.99-local"
)
func init() {
log.SetFlags(0)
flag.BoolVar(&flagOutputJSON, "output-json", flagOutputJSON, "Output JSON instead of TOML")
flag.BoolVar(&flagShowVersion, "version", flagShowVersion, "Show version")
flag.Usage = usage
flag.Parse()
}
func usage() {
log.Printf("Usage: %s toml-file [ toml-file ... ]\n", path.Base(os.Args[0]))
flag.PrintDefaults()
os.Exit(1)
}
func main() {
if flagShowVersion {
log.Printf("toml-merge version %s\n", version)
os.Exit(0)
}
if flag.NArg() < 1 {
flag.Usage()
}
var output = make(map[string]interface{})
output["__autogenerated_do_not_modify__"] =
"This file was automatically generated. Please do not modify."
for _, f := range flag.Args() {
var tmp map[string]interface{}
_, err := toml.DecodeFile(f, &tmp)
if err != nil {
log.Fatalf("Error in '%s': %s", f, err)
}
for k, v := range tmp {
output[k] = v
}
}
if flagOutputJSON {
jsonwrt := json.NewEncoder(os.Stdout)
jsonwrt.SetIndent("", " ")
jsonwrt.Encode(output)
} else {
tomlrt := toml.NewEncoder(os.Stdout)
tomlrt.Encode(output)
}
}