-
Notifications
You must be signed in to change notification settings - Fork 684
/
main.go
107 lines (94 loc) · 2.24 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package main
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
"github.com/google/go-cmp/cmp"
apiext "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
"github.com/datawire/dlib/dlog"
)
var ErrDiff = errors.New("file is not formatted")
func main() {
if len(os.Args) < 3 {
fmt.Fprintf(os.Stderr, "Usage: %s -<w|d> FILES.schema.../\n", os.Args[0])
os.Exit(2)
}
if err := Main(context.Background(), os.Args[1][1], os.Args[2:]...); err != nil {
if !errors.Is(err, ErrDiff) {
fmt.Fprintf(os.Stderr, "%s: error: %v\n", os.Args[0], err)
}
os.Exit(1)
}
}
func processFile(ctx context.Context, op byte, filename string) error {
var err error
var file *os.File
switch op {
case 'd':
file, err = os.Open(filename)
case 'w':
file, err = os.OpenFile(filename, os.O_RDWR, 0)
}
if err != nil {
return err
}
defer file.Close()
inputBytes, err := ioutil.ReadAll(file)
if err != nil {
return err
}
decoder := json.NewDecoder(bytes.NewReader(inputBytes))
decoder.DisallowUnknownFields()
var schema apiext.JSONSchemaProps
if err := decoder.Decode(&schema); err != nil {
return fmt.Errorf("%q: %w", filename, err)
}
outputBytes, err := json.MarshalIndent(schema, "", " ")
if err != nil {
return fmt.Errorf("%q: %w", filename, err)
}
outputBytes = append(outputBytes, '\n')
if bytes.Equal(inputBytes, outputBytes) {
dlog.Infof(ctx, "File %q is already propperly formatted", filename)
return nil
}
switch op {
case 'd':
fmt.Printf("diff a/%[1]s b/%[1]s\n--- a/%[1]s\n+++ b/%[1]s\n%s\n",
filename,
cmp.Diff(string(inputBytes), string(outputBytes)))
return fmt.Errorf("%q: %w", filename, ErrDiff)
case 'w':
if _, err := file.Seek(0, 0); err != nil {
return err
}
if err := file.Truncate(0); err != nil {
return err
}
if _, err := file.Write(outputBytes); err != nil {
return err
}
dlog.Infof(ctx, "File %q reformatted", filename)
}
return nil
}
func Main(ctx context.Context, op byte, filenames ...string) error {
diff := false
for _, filename := range filenames {
if err := processFile(ctx, op, filename); err != nil {
if errors.Is(err, ErrDiff) {
diff = true
} else {
return err
}
}
}
if diff {
return ErrDiff
}
return nil
}