File: main.go

package info (click to toggle)
golang-github-kisom-goutils 0.0~git20161101.0.858c9cb-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 384 kB
  • ctags: 331
  • sloc: makefile: 6
file content (85 lines) | stat: -rw-r--r-- 1,516 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
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)
	}

	if flag.NArg() == 1 && flag.Arg(0) == "-" {
		path := "stdin"
		in, err := ioutil.ReadAll(os.Stdin)
		if err != nil {
			errorf("%s FAILED: %s", path, err)
			os.Exit(1)
		}

		var e empty
		err = yaml.Unmarshal(in, &e)
		if err != nil {
			errorf("%s FAILED: %s", path, err)
			os.Exit(1)
		}

		if !*quiet {
			fmt.Printf("%s: OK\n", path)
		}

		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)
		}
	}
}