-
Notifications
You must be signed in to change notification settings - Fork 12
/
main.go
75 lines (63 loc) · 1.34 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package main
import (
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
"github.com/go-clix/cli"
neat "github.com/itaysk/kubectl-neat/cmd"
)
func main() {
log.SetFlags(0)
cmd := cli.Command{
Use: "kubectl-neat-diff [file1] [file2]",
Short: "Remove fields from kubectl diff that carry low / no information",
Args: cli.ArgsExact(2),
}
cmd.Run = func(cmd *cli.Command, args []string) error {
if err := neatifyDir(args[0]); err != nil {
return err
}
if err := neatifyDir(args[1]); err != nil {
return err
}
c := exec.Command("diff", "-uN", args[0], args[1])
c.Stdout = os.Stdout
c.Stderr = os.Stderr
return c.Run()
}
err := cmd.Execute()
if err != nil {
switch e := err.(type) {
case *exec.ExitError:
// exit status 1 means there is a diff, so we ignore this
if e.ExitCode() == 1 {
return
}
}
// otherwise log all errors
log.Fatalln("Error:", err)
}
}
func neatifyDir(dir string) error {
fis, err := ioutil.ReadDir(dir)
if err != nil {
return err
}
for _, fi := range fis {
filename := filepath.Join(dir, fi.Name())
data, err := ioutil.ReadFile(filename)
if err != nil {
return err
}
n, err := neat.NeatYAMLOrJSON(data, "same")
if err != nil {
return err
}
if err := ioutil.WriteFile(filename, []byte(n), fi.Mode()); err != nil {
return err
}
}
return nil
}