-
Notifications
You must be signed in to change notification settings - Fork 33
/
schematic.go
78 lines (66 loc) · 1.23 KB
/
schematic.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
// Schematic
//
// Generate Go client code for HTTP APIs
// described by JSON Hyper-Schemas: http://json-schema.org/latest/json-schema-hypermedia.html.
//
// Run it against your schema:
//
// $ schematic platform-api.json > heroku/heroku.go
//
// This will generate a Go package named after your schema:
//
// package heroku
// ...
//
package main
import (
"encoding/json"
"flag"
"fmt"
"io"
"log"
"os"
"github.com/interagent/schematic"
)
var output = flag.String("o", "", "Output file")
func main() {
defer func() {
if r := recover(); r != nil {
log.Fatal(r)
}
}()
log.SetFlags(0)
log.SetPrefix("schematic: ")
flag.Parse()
if flag.NArg() != 1 {
log.Fatal("missing schema file")
}
var i io.Reader
var err error
if flag.Arg(0) == "-" {
i = os.Stdin
} else {
if i, err = os.Open(flag.Arg(0)); err != nil {
log.Fatal(err)
}
}
var o io.Writer
if *output == "" {
o = os.Stdout
} else {
if o, err = os.Create(*output); err != nil {
log.Fatal(err)
}
}
var s schematic.Schema
d := json.NewDecoder(i)
if err := d.Decode(&s); err != nil {
log.Fatal(err)
}
code, err := s.Generate()
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", code)
log.Fatal(err)
}
fmt.Fprintln(o, string(code))
}