Simple lint check for yaml files in the spirit of python -mjson.tool.

main.go 606B

1234567891011121314151617181920212223242526272829303132333435
  1. package main
  2. import (
  3. "bytes"
  4. "fmt"
  5. "io"
  6. "io/ioutil"
  7. "log"
  8. "os"
  9. "gopkg.in/yaml.v2"
  10. )
  11. func main() {
  12. log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
  13. b, err := ioutil.ReadAll(os.Stdin)
  14. if err != nil {
  15. fmt.Fprintf(os.Stderr, "problem reading stdin: %v\n", err)
  16. os.Exit(1)
  17. }
  18. var obj interface{}
  19. err = yaml.Unmarshal(b, &obj)
  20. if err != nil {
  21. fmt.Fprintf(os.Stderr, "issue in decoding: %v\n", err)
  22. os.Exit(1)
  23. }
  24. bs, err := yaml.Marshal(obj)
  25. if err != nil {
  26. fmt.Fprintf(os.Stderr, "issue in encoding: %v\n", err)
  27. os.Exit(1)
  28. }
  29. r := bytes.NewReader(bs)
  30. io.Copy(os.Stdout, r)
  31. }