-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
57 lines (49 loc) · 1.09 KB
/
main.go
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
package main
import (
"flag"
"log"
"os"
yaml "gopkg.in/yaml.v2"
)
func main() {
file := flag.String("filename", "", "filename")
fileShort := flag.String("f", "", "filename")
flag.Parse()
if *file == "" && *fileShort != "" {
file = fileShort
}
var (
yamlFile *os.File
yamlFileOut *os.File
err error
overwriteFile bool
)
if *file != "" {
overwriteFile = true
yamlFile, err = os.OpenFile(*file, os.O_RDWR, 0644)
if err != nil {
log.Fatalf("could not open file: %v", err)
}
yamlFileOut = yamlFile
defer yamlFile.Close()
} else {
yamlFile = os.Stdin
yamlFileOut = os.Stdout
}
var out interface{}
if err = yaml.NewDecoder(yamlFile).Decode(&out); err != nil {
log.Fatalf("could not decode file: %v", err)
}
if overwriteFile {
if err = yamlFileOut.Truncate(0); err != nil {
log.Fatalf("could not truncate file: %v", err)
}
if _, err = yamlFileOut.Seek(0, 0); err != nil {
log.Fatalf("could not seek file: %v", err)
}
}
err = yaml.NewEncoder(yamlFileOut).Encode(out)
if err != nil {
log.Fatalf("could not encode file: %v", err)
}
}