-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
90 lines (77 loc) · 1.44 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
package main
import (
"errors"
"flag"
"fmt"
"io"
"log"
"os"
"strings"
"github.com/go-oss/avro-bq-schema/schema"
)
const (
name = "avro-bq-schema"
defaultIndent = 2
)
var (
errInvalidArgument = errors.New("invalid argument")
)
func main() {
var err error
defer func() {
if err != nil {
os.Exit(1)
}
}()
log.SetFlags(0)
var indent int
cmd := flag.NewFlagSet("", flag.ExitOnError)
cmd.IntVar(&indent, "indent", defaultIndent, "output JSON indent size")
cmd.Usage = func() {
b := new(strings.Builder)
fmt.Fprintln(b, "Usage:")
fmt.Fprintf(b, " %s [file]\n", name)
b.WriteRune('\n')
fmt.Fprintln(b, "Flags:")
//nolint:errcheck
io.WriteString(cmd.Output(), b.String())
cmd.PrintDefaults()
}
err = cmd.Parse(os.Args[1:])
if err != nil {
log.Println("cmd.Parse:", err)
return
}
files := cmd.Args()
if len(files) == 0 {
err = errInvalidArgument
log.Println(err)
return
}
f, err := os.Open(files[0])
if err != nil {
log.Println("os.Open:", err)
return
}
defer f.Close()
d, err := io.ReadAll(f)
if err != nil {
log.Println("io.ReadAll:", err)
return
}
bqSchema, err := schema.Convert(d)
if err != nil {
log.Println("schema.Convert:", err)
return
}
dst, err := schema.ToJSON(bqSchema, indent)
if err != nil {
log.Println("schema.ToJSON:", err)
return
}
_, err = io.WriteString(os.Stdout, string(dst))
if err != nil {
log.Println("io.WriteString:", err)
return
}
}