validtoml/main.go

57 lines
1.1 KiB
Go
Raw Normal View History

2016-07-12 12:32:37 +00:00
package main
import (
"fmt"
"io/ioutil"
"os"
"github.com/BurntSushi/toml"
2023-02-25 17:58:12 +00:00
"github.com/alecthomas/kingpin/v2"
2016-09-27 13:17:34 +00:00
termutil "github.com/andrew-d/go-termutil"
2016-07-12 12:32:37 +00:00
)
var (
2016-09-27 13:17:34 +00:00
inFile = kingpin.Arg("file", "TOML file.").String()
2016-07-12 12:32:37 +00:00
quiet = kingpin.Flag("quiet", "Don't output on success.").Short('q').Bool()
)
func main() {
// support -h for --help
kingpin.CommandLine.HelpFlag.Short('h')
kingpin.Parse()
2016-09-27 13:17:34 +00:00
data, err := readPipeOrFile(*inFile)
if err != nil {
fmt.Println("error:", err)
os.Exit(1)
}
2016-07-12 12:32:37 +00:00
2018-01-08 00:23:25 +00:00
filename := "-"
if *inFile != "" {
filename = *inFile
}
2016-07-12 12:32:37 +00:00
var f interface{}
2016-09-27 13:17:34 +00:00
_, err = toml.Decode(string(data), &f)
2016-07-12 12:32:37 +00:00
if err != nil {
2018-01-08 00:23:25 +00:00
fmt.Println("ERROR:", filename, err)
2016-07-12 12:32:37 +00:00
os.Exit(1)
}
if !*quiet {
2018-01-08 00:23:25 +00:00
fmt.Println("OK:", filename)
2016-07-12 12:32:37 +00:00
}
}
2016-09-27 13:17:34 +00:00
// readPipeOrFile reads from stdin if pipe exists, else from provided file
func readPipeOrFile(fileName string) ([]byte, error) {
if !termutil.Isatty(os.Stdin.Fd()) {
return ioutil.ReadAll(os.Stdin)
}
if fileName == "" {
return nil, fmt.Errorf("no piped data and no file provided")
}
return ioutil.ReadFile(fileName)
}