1 // Tomljson reads TOML and converts to JSON.
4 // cat file.toml | tomljson > file.json
5 // tomljson file1.toml > file.json
15 "github.com/pelletier/go-toml"
20 fmt.Fprintln(os.Stderr, `tomljson can be used in two ways:
21 Writing to STDIN and reading from STDOUT:
22 cat file.toml | tomljson > file.json
24 Reading from a file name:
29 os.Exit(processMain(flag.Args(), os.Stdin, os.Stdout, os.Stderr))
32 func processMain(files []string, defaultInput io.Reader, output io.Writer, errorOutput io.Writer) int {
33 // read from stdin and print to stdout
34 inputReader := defaultInput
38 inputReader, err = os.Open(files[0])
40 printError(err, errorOutput)
44 s, err := reader(inputReader)
46 printError(err, errorOutput)
49 io.WriteString(output, s+"\n")
53 func printError(err error, output io.Writer) {
54 io.WriteString(output, err.Error()+"\n")
57 func reader(r io.Reader) (string, error) {
58 tree, err := toml.LoadReader(r)
62 return mapToJSON(tree)
65 func mapToJSON(tree *toml.Tree) (string, error) {
66 treeMap := tree.ToMap()
67 bytes, err := json.MarshalIndent(treeMap, "", " ")
71 return string(bytes[:]), nil