...

Source file src/github.com/BurntSushi/toml/cmd/tomlv/main.go

Documentation: github.com/BurntSushi/toml/cmd/tomlv

     1  // Command tomlv validates TOML documents and prints each key's type.
     2  package main
     3  
     4  import (
     5  	"flag"
     6  	"fmt"
     7  	"log"
     8  	"os"
     9  	"path"
    10  	"strings"
    11  	"text/tabwriter"
    12  
    13  	"github.com/BurntSushi/toml"
    14  )
    15  
    16  var (
    17  	flagTypes = false
    18  )
    19  
    20  func init() {
    21  	log.SetFlags(0)
    22  	flag.BoolVar(&flagTypes, "types", flagTypes, "Show the types for every key.")
    23  	flag.Usage = usage
    24  	flag.Parse()
    25  }
    26  
    27  func usage() {
    28  	log.Printf("Usage: %s toml-file [ toml-file ... ]\n", path.Base(os.Args[0]))
    29  	flag.PrintDefaults()
    30  	os.Exit(1)
    31  }
    32  
    33  func main() {
    34  	if flag.NArg() < 1 {
    35  		flag.Usage()
    36  	}
    37  	for _, f := range flag.Args() {
    38  		var tmp interface{}
    39  		md, err := toml.DecodeFile(f, &tmp)
    40  		if err != nil {
    41  			log.Fatalf("Error in '%s': %s", f, err)
    42  		}
    43  		if flagTypes {
    44  			printTypes(md)
    45  		}
    46  	}
    47  }
    48  
    49  func printTypes(md toml.MetaData) {
    50  	tabw := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
    51  	for _, key := range md.Keys() {
    52  		fmt.Fprintf(tabw, "%s%s\t%s\n",
    53  			strings.Repeat("    ", len(key)-1), key, md.Type(key...))
    54  	}
    55  	tabw.Flush()
    56  }
    57  

View as plain text