|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "os" |
| 8 | + |
| 9 | + "github.com/MakeNowJust/heredoc" |
| 10 | + "github.com/odpf/salt/printer" |
| 11 | + "github.com/odpf/salt/term" |
| 12 | + stencilv1beta1 "github.com/odpf/stencil/proto/odpf/stencil/v1beta1" |
| 13 | + "github.com/spf13/cobra" |
| 14 | + "google.golang.org/grpc/codes" |
| 15 | + "google.golang.org/grpc/status" |
| 16 | +) |
| 17 | + |
| 18 | +func createSchemaCmd() *cobra.Command { |
| 19 | + var host, format, comp, file, namespaceID string |
| 20 | + var req stencilv1beta1.CreateSchemaRequest |
| 21 | + |
| 22 | + cmd := &cobra.Command{ |
| 23 | + Use: "create", |
| 24 | + Short: "Create a schema", |
| 25 | + Args: cobra.ExactArgs(1), |
| 26 | + Example: heredoc.Doc(` |
| 27 | + $ stencil schema create booking -n odpf –F booking.json |
| 28 | + $ stencil schema create booking -n odpf -f FORMAT_JSON –c COMPATIBILITY_BACKWARD –F ./booking.json |
| 29 | + `), |
| 30 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 31 | + fileData, err := os.ReadFile(file) |
| 32 | + if err != nil { |
| 33 | + return err |
| 34 | + } |
| 35 | + req.Data = fileData |
| 36 | + |
| 37 | + spinner := printer.Spin("") |
| 38 | + defer spinner.Stop() |
| 39 | + client, cancel, err := createClient(cmd) |
| 40 | + if err != nil { |
| 41 | + return err |
| 42 | + } |
| 43 | + defer cancel() |
| 44 | + |
| 45 | + schemaID := args[0] |
| 46 | + req.NamespaceId = namespaceID |
| 47 | + req.SchemaId = schemaID |
| 48 | + req.Format = stencilv1beta1.Schema_Format(stencilv1beta1.Schema_Format_value[format]) |
| 49 | + req.Compatibility = stencilv1beta1.Schema_Compatibility(stencilv1beta1.Schema_Compatibility_value[comp]) |
| 50 | + |
| 51 | + res, err := client.CreateSchema(context.Background(), &req) |
| 52 | + if err != nil { |
| 53 | + errStatus := status.Convert(err) |
| 54 | + if codes.AlreadyExists == errStatus.Code() { |
| 55 | + fmt.Printf("\n%s Schema with id '%s' already exist.\n", term.FailureIcon(), args[0]) |
| 56 | + return nil |
| 57 | + } |
| 58 | + return errors.New(errStatus.Message()) |
| 59 | + } |
| 60 | + |
| 61 | + id := res.GetId() |
| 62 | + |
| 63 | + spinner.Stop() |
| 64 | + fmt.Printf("\n%s Created schema with id %s.\n", term.Green(term.SuccessIcon()), term.Cyan(id)) |
| 65 | + return nil |
| 66 | + }, |
| 67 | + } |
| 68 | + |
| 69 | + cmd.Flags().StringVar(&host, "host", "", "Stencil host address eg: localhost:8000") |
| 70 | + cmd.MarkFlagRequired("host") |
| 71 | + |
| 72 | + cmd.Flags().StringVarP(&namespaceID, "namespace", "n", "", "Namespace ID") |
| 73 | + cmd.MarkFlagRequired("namespace") |
| 74 | + |
| 75 | + cmd.Flags().StringVarP(&format, "format", "f", "", "Schema format") |
| 76 | + |
| 77 | + cmd.Flags().StringVarP(&comp, "comp", "c", "", "Schema compatibility") |
| 78 | + |
| 79 | + cmd.Flags().StringVarP(&file, "file", "F", "", "Path to the schema file") |
| 80 | + cmd.MarkFlagRequired("file") |
| 81 | + |
| 82 | + return cmd |
| 83 | +} |
0 commit comments