-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.go
101 lines (81 loc) · 1.71 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
package main
import (
"bytes"
"flag"
"fmt"
"io/ioutil"
"os"
"github.com/jbowes/oag/config"
"github.com/jbowes/oag/mutator"
"github.com/jbowes/oag/openapi"
"github.com/jbowes/oag/translator"
"github.com/jbowes/oag/writer"
)
var cfgFile = flag.String("c", ".oag.yaml", "Use this configuration file.")
func usage() {
fmt.Println("Usage: oag [init]")
os.Exit(-1)
}
func initConfig() error {
f, err := os.OpenFile(*cfgFile, os.O_CREATE|os.O_EXCL|os.O_RDWR, 0600)
if err != nil {
return err
}
defer f.Close()
if err = config.WriteDefaultConfig(f); err != nil {
return err
}
fmt.Printf("Wrote '%s'. Please edit it for your configuration.\n", *cfgFile)
return nil
}
func generateClient() error {
cfg, err := config.Load(*cfgFile)
if err != nil {
return err
}
doc, err := openapi.LoadFile(cfg.Document)
if err != nil {
return err
}
code, err := translator.Translate(doc, cfg.Package.Path, cfg.Package.Name, cfg.Types, cfg.StringFormats)
if err != nil {
return err
}
code = mutator.Mutate(code)
o, err := ioutil.ReadFile(cfg.Output)
if err != nil {
return err
}
var buf bytes.Buffer
if err = writer.Write(&buf, code, &cfg.Boilerplate); err != nil {
return err
}
n := buf.Bytes()
if bytes.Equal(n, o) {
fmt.Println("No change to", cfg.Output)
return nil
}
if err = ioutil.WriteFile(cfg.Output, n, 0666); err != nil {
return err
}
fmt.Println("Wrote", cfg.Output)
return nil
}
func main() {
flag.Parse()
args := flag.Args()
switch {
case len(args) == 0:
if err := generateClient(); err != nil {
fmt.Println(err)
os.Exit(-1)
}
case len(args) > 1, args[0] != "init":
usage()
default:
if err := initConfig(); err != nil {
fmt.Println(err)
os.Exit(-1)
}
}
}