jsontoml: port to v2 (#726)

Fixes #719
This commit is contained in:
Thomas Pelletier
2021-12-31 14:40:20 -05:00
committed by GitHub
parent 82f8dad811
commit d8ddc00c61
7 changed files with 254 additions and 175 deletions
+38
View File
@@ -0,0 +1,38 @@
// Jsontoml reads JSON and converts to TOML.
//
// Usage:
// cat file.toml | jsontoml > file.json
// jsontoml file1.toml > file.json
package main
import (
"encoding/json"
"io"
"github.com/pelletier/go-toml/v2"
"github.com/pelletier/go-toml/v2/internal/cli"
)
func main() {
usage := `jsontoml can be used in two ways:
Reading from stdin:
cat file.json | jsontoml > file.toml
Reading from a file:
jsontoml file.json > file.toml
`
cli.Execute(usage, convert)
}
func convert(r io.Reader, w io.Writer) error {
var v interface{}
d := json.NewDecoder(r)
err := d.Decode(&v)
if err != nil {
return err
}
e := toml.NewEncoder(w)
return e.Encode(v)
}