-
Notifications
You must be signed in to change notification settings - Fork 37
/
cmd_generate.go
70 lines (58 loc) · 1.45 KB
/
cmd_generate.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
package main
import (
"bytes"
"context"
gofmt "go/format"
"os"
"github.com/urfave/cli/v2"
)
var clientOutFile string
var eventsOutFile string
func init() {
commands = append(commands, &cli.Command{
Name: "generate-api",
Usage: "generates the API code",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "client-output",
Usage: "client output file",
Value: "../client_generated.go",
Destination: &clientOutFile,
},
&cli.StringFlag{
Name: "events-output",
Usage: "events output file",
Value: "../events/generated.go",
Destination: &eventsOutFile,
},
},
Action: func(c *cli.Context) error {
clientFileBuf := &bytes.Buffer{}
eventsFileBuf := &bytes.Buffer{}
if err := GenerateAPI(context.Background(), clientFileBuf, eventsFileBuf); err != nil {
return err
}
if err := os.WriteFile(clientOutFile, clientFileBuf.Bytes(), 0655); err != nil {
return err
}
clientFileDat, err := gofmt.Source(clientFileBuf.Bytes())
if err != nil {
clientFileDat = clientFileBuf.Bytes()
}
if err := os.WriteFile(clientOutFile, clientFileDat, 0655); err != nil {
return err
}
if err != nil {
return err
}
eventsFileDat, err := gofmt.Source(eventsFileBuf.Bytes())
if err != nil {
eventsFileDat = eventsFileBuf.Bytes()
}
if err := os.WriteFile(eventsOutFile, eventsFileDat, 0655); err != nil {
return err
}
return err
},
})
}