-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.go
More file actions
65 lines (55 loc) · 1.24 KB
/
Copy pathmain.go
File metadata and controls
65 lines (55 loc) · 1.24 KB
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
package main
import (
"fmt"
"io/ioutil"
"os"
"github.com/alecthomas/kingpin/v2"
termutil "github.com/andrew-d/go-termutil"
"gopkg.in/yaml.v2"
)
var (
inFile = kingpin.Arg("file", "YAML file.").String()
pretty = kingpin.Flag("pretty", "Pretty print result.").Short('p').Bool()
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()
data, err := readPipeOrFile(*inFile)
if err != nil {
fmt.Println("error:", err)
os.Exit(1)
}
filename := "-"
if *inFile != "" {
filename = *inFile
}
var f interface{}
err = yaml.Unmarshal(data, &f)
if err != nil {
fmt.Println("ERROR:", filename, err)
os.Exit(1)
}
if *pretty {
b, err := yaml.Marshal(f)
if err != nil {
fmt.Println("ERROR:", err)
}
fmt.Printf(string(b))
} else {
if !*quiet {
fmt.Println("OK:", filename)
}
}
}
// 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)
}