-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathcreate.go
More file actions
79 lines (65 loc) · 2.18 KB
/
create.go
File metadata and controls
79 lines (65 loc) · 2.18 KB
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
package cmd
import (
"context"
"errors"
"fmt"
"os"
"github.com/MakeNowJust/heredoc"
"github.com/raystack/salt/cli/printer"
stencilv1beta1 "github.com/raystack/stencil/proto/raystack/stencil/v1beta1"
"github.com/spf13/cobra"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
func createSchemaCmd(cdk *CDK) *cobra.Command {
var format, comp, file, namespaceID string
var req stencilv1beta1.CreateSchemaRequest
cmd := &cobra.Command{
Use: "create",
Short: "Create a schema",
Args: cobra.ExactArgs(1),
Example: heredoc.Doc(`
$ stencil schema create booking -n raystack –F booking.json
$ stencil schema create booking -n raystack -f FORMAT_JSON –c COMPATIBILITY_BACKWARD –F ./booking.json
`),
RunE: func(cmd *cobra.Command, args []string) error {
fileData, err := os.ReadFile(file)
if err != nil {
return err
}
req.Data = fileData
spinner := printer.Spin("")
defer spinner.Stop()
client, cancel, err := createClient(cmd, cdk)
if err != nil {
return err
}
defer cancel()
schemaID := args[0]
req.NamespaceId = namespaceID
req.SchemaId = schemaID
req.Format = stencilv1beta1.Schema_Format(stencilv1beta1.Schema_Format_value[format])
req.Compatibility = stencilv1beta1.Schema_Compatibility(stencilv1beta1.Schema_Compatibility_value[comp])
res, err := client.CreateSchema(context.Background(), &req)
if err != nil {
errStatus := status.Convert(err)
if codes.AlreadyExists == errStatus.Code() {
fmt.Printf("\n%s Schema with id '%s' already exist.\n", printer.Icon("failure"), args[0])
return nil
}
return errors.New(errStatus.Message())
}
id := res.GetId()
spinner.Stop()
fmt.Printf("\n%s Created schema with id %s.\n", printer.Green(printer.Icon("success")), printer.Cyan(id))
return nil
},
}
cmd.Flags().StringVarP(&namespaceID, "namespace", "n", "", "Namespace ID")
cmd.MarkFlagRequired("namespace")
cmd.Flags().StringVarP(&format, "format", "f", "", "Schema format")
cmd.Flags().StringVarP(&comp, "comp", "c", "", "Schema compatibility")
cmd.Flags().StringVarP(&file, "file", "F", "", "Path to the schema file")
cmd.MarkFlagRequired("file")
return cmd
}