From cd6c00a20383c39d03e55377838ad57f01a2dbed Mon Sep 17 00:00:00 2001 From: Kyle Isom Date: Fri, 27 May 2016 16:50:40 -0700 Subject: [PATCH] Add YAML linter. --- cmd/yamll/README | 3 +++ cmd/yamll/main.go | 63 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 cmd/yamll/README create mode 100644 cmd/yamll/main.go diff --git a/cmd/yamll/README b/cmd/yamll/README new file mode 100644 index 0000000..77b3559 --- /dev/null +++ b/cmd/yamll/README @@ -0,0 +1,3 @@ +yamll: yaml linter + +Run this over YAML files to determine if they are well-formed or not. diff --git a/cmd/yamll/main.go b/cmd/yamll/main.go new file mode 100644 index 0000000..8982348 --- /dev/null +++ b/cmd/yamll/main.go @@ -0,0 +1,63 @@ +package main + +import ( + "flag" + "fmt" + "io" + "io/ioutil" + "os" + + "gopkg.in/yaml.v2" +) + +type empty struct{} + +func errorf(format string, args ...interface{}) { + format += "\n" + fmt.Fprintf(os.Stderr, format, args...) +} + +func usage(w io.Writer) { + fmt.Fprintf(w, `Usage: yamll [-hq] files... + + For each file, yamll will make sure it is a well-formatted YAML + file. Unless the -q option is passed, yamll will print the names + of each file and whether it was well-formed. With the -q option, + only malformed files are printed. +`) +} + +func init() { + flag.Usage = func() { usage(os.Stderr); os.Exit(1) } +} + +func main() { + help := flag.Bool("h", false, "Print program usage.") + quiet := flag.Bool("q", false, + "Quiet mode - don't note well-formed files, only malformed ones.") + flag.Parse() + + if *help { + usage(os.Stdout) + os.Exit(0) + } + + for _, path := range flag.Args() { + in, err := ioutil.ReadFile(path) + if err != nil { + errorf("%s FAILED: %s", path, err) + continue + } + + var e empty + err = yaml.Unmarshal(in, &e) + if err != nil { + errorf("%s FAILED: %s", path, err) + continue + } + + if !*quiet { + fmt.Printf("%s: OK\n", path) + } + } +}