...

Source file src/github.com/pelletier/go-toml/v2/cmd/jsontoml/main.go

Documentation: github.com/pelletier/go-toml/v2/cmd/jsontoml

     1  // Package jsontoml is a program that converts JSON to TOML.
     2  //
     3  // # Usage
     4  //
     5  // Reading from stdin:
     6  //
     7  //	cat file.json | jsontoml > file.toml
     8  //
     9  // Reading from a file:
    10  //
    11  //	jsontoml file.json > file.toml
    12  //
    13  // # Installation
    14  //
    15  // Using Go:
    16  //
    17  //	go install github.com/pelletier/go-toml/v2/cmd/jsontoml@latest
    18  package main
    19  
    20  import (
    21  	"encoding/json"
    22  	"io"
    23  
    24  	"github.com/pelletier/go-toml/v2"
    25  	"github.com/pelletier/go-toml/v2/internal/cli"
    26  )
    27  
    28  const usage = `jsontoml can be used in two ways:
    29  Reading from stdin:
    30    cat file.json | jsontoml > file.toml
    31  
    32  Reading from a file:
    33    jsontoml file.json > file.toml
    34  `
    35  
    36  func main() {
    37  	p := cli.Program{
    38  		Usage: usage,
    39  		Fn:    convert,
    40  	}
    41  	p.Execute()
    42  }
    43  
    44  func convert(r io.Reader, w io.Writer) error {
    45  	var v interface{}
    46  
    47  	d := json.NewDecoder(r)
    48  	err := d.Decode(&v)
    49  	if err != nil {
    50  		return err
    51  	}
    52  
    53  	e := toml.NewEncoder(w)
    54  	return e.Encode(v)
    55  }
    56  

View as plain text