Debian Codegraph @

cmd/yamll/main.go

raw
1 package main
2
3 import (
4 "flag"
5 "fmt"
6 "io"
7 "io/ioutil"
8 "os"
9
10 "gopkg.in/yaml.v2"
11 )
12
13 type empty struct{}
14
15 func errorf(format string, args ...interface{}) {
16 format += "\n"
17 fmt.Fprintf(os.Stderr, format, args...)
18 }
19
20 func usage(w io.Writer) {
21 fmt.Fprintf(w, `Usage: yamll [-hq] files...
22
23 For each file, yamll will make sure it is a well-formatted YAML
24 file. Unless the -q option is passed, yamll will print the names
25 of each file and whether it was well-formed. With the -q option,
26 only malformed files are printed.
27 `)
28 }
29
30 func init() {
31 flag.Usage = func() { usage(os.Stderr); os.Exit(1) }
32 }
33
34 func main() {
35 help := flag.Bool("h", false, "Print program usage.")
36 quiet := flag.Bool("q", false,
37 "Quiet mode - don't note well-formed files, only malformed ones.")
38 flag.Parse()
39
40 if *help {
41 usage(os.Stdout)
42 os.Exit(0)
43 }
44
45 if flag.NArg() == 1 && flag.Arg(0) == "-" {
46 path := "stdin"
47 in, err := ioutil.ReadAll(os.Stdin)
48 if err != nil {
49 errorf("%s FAILED: %s", path, err)
50 os.Exit(1)
51 }
52
53 var e empty
54 err = yaml.Unmarshal(in, &e)
55 if err != nil {
56 errorf("%s FAILED: %s", path, err)
57 os.Exit(1)
58 }
59
60 if !*quiet {
61 fmt.Printf("%s: OK\n", path)
62 }
63
64 os.Exit(0)
65 }
66
67 for _, path := range flag.Args() {
68 in, err := ioutil.ReadFile(path)
69 if err != nil {
70 errorf("%s FAILED: %s", path, err)
71 continue
72 }
73
74 var e empty
75 err = yaml.Unmarshal(in, &e)
76 if err != nil {
77 errorf("%s FAILED: %s", path, err)
78 continue
79 }
80
81 if !*quiet {
82 fmt.Printf("%s: OK\n", path)
83 }
84 }
85 }