git.sr.ht/~pingoo/stdx@v0.0.0-20240218134121-094174641f6e/toml/cmd/toml-test-decoder/main.go (about)

     1  // Command toml-test-decoder satisfies the toml-test interface for testing TOML
     2  // decoders. Namely, it accepts TOML on stdin and outputs JSON on stdout.
     3  package main
     4  
     5  import (
     6  	"encoding/json"
     7  	"flag"
     8  	"log"
     9  	"os"
    10  	"path"
    11  
    12  	"git.sr.ht/~pingoo/stdx/toml"
    13  	"git.sr.ht/~pingoo/stdx/toml/internal/tag"
    14  )
    15  
    16  func init() {
    17  	log.SetFlags(0)
    18  	flag.Usage = usage
    19  	flag.Parse()
    20  }
    21  
    22  func usage() {
    23  	log.Printf("Usage: %s < toml-file\n", path.Base(os.Args[0]))
    24  	flag.PrintDefaults()
    25  	os.Exit(1)
    26  }
    27  
    28  func main() {
    29  	if flag.NArg() != 0 {
    30  		flag.Usage()
    31  	}
    32  
    33  	var decoded interface{}
    34  	if _, err := toml.NewDecoder(os.Stdin).Decode(&decoded); err != nil {
    35  		log.Fatalf("Error decoding TOML: %s", err)
    36  	}
    37  
    38  	j := json.NewEncoder(os.Stdout)
    39  	j.SetIndent("", "  ")
    40  	if err := j.Encode(tag.Add("", decoded)); err != nil {
    41  		log.Fatalf("Error encoding JSON: %s", err)
    42  	}
    43  }