-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
generateSchema.go
70 lines (64 loc) · 1.78 KB
/
generateSchema.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 cmd
import (
"context"
"os"
"strings"
"beryju.io/gravity/pkg/extconfig"
"beryju.io/gravity/pkg/instance"
"beryju.io/gravity/pkg/instance/types"
"beryju.io/gravity/pkg/roles"
"beryju.io/gravity/pkg/roles/api"
"github.com/spf13/cobra"
"go.uber.org/zap"
)
var schemaFormat = ""
func GenerateSchema(ctx context.Context, format string, callback func(schema []byte)) {
rootInst := instance.New()
inst := rootInst.ForRole("api", ctx)
inst.AddEventListener(types.EventTopicInstanceBootstrapped, func(ev *roles.Event) {
defer rootInst.Stop()
api := rootInst.Role("api").(*api.Role)
schema := api.Schema(ctx)
var out []byte
var err error
switch strings.ToLower(format) {
case "yaml":
out, err = schema.MarshalYAML()
case "json":
fallthrough
default:
out, err = schema.MarshalJSON()
}
if err != nil {
rootInst.Log().Warn("failed to generate schema", zap.Error(err))
return
}
callback(out)
})
rootInst.Start()
}
// generateSchemaCmd represents the generateSchema command
var generateSchemaCmd = &cobra.Command{
Use: "generateSchema [output_file]",
Short: "Generate OpenAPI Schema",
Run: func(cmd *cobra.Command, args []string) {
logger := extconfig.Get().Logger()
GenerateSchema(cmd.Context(), schemaFormat, func(schema []byte) {
if len(args) > 0 {
err := os.WriteFile(args[0], schema, 0o644)
if err != nil {
logger.Warn("failed to write schema", zap.Error(err))
return
}
logger.Info("successfully wrote schema", zap.String("to", args[0]))
} else {
cmd.OutOrStdout().Write(schema)
logger.Info("Successfully wrote schema to stdout")
}
})
},
}
func init() {
rootCmd.AddCommand(generateSchemaCmd)
generateSchemaCmd.PersistentFlags().StringVarP(&schemaFormat, "format", "f", "yaml", "Output format (yaml/json)")
}