Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
NAME="github.com/raystack/stencil"
VERSION=$(shell git describe --always --tags 2>/dev/null)
PROTON_COMMIT := "a6c7056fa80128145d00d5ee72f216c28578ec43"

.PHONY: all build test clean dist vet proto install ui

Expand All @@ -18,13 +17,12 @@ coverage: ui ## Print code coverage
vet: ## Run the go vet tool
go vet ./...

lint: ## Run golang-ci lint
lint: ## Run golang-ci lint
golangci-lint run

proto: ## Generate the protobuf files
@echo " > generating protobuf from raystack/proton"
@echo " > [info] make sure correct version of dependencies are installed using 'make install'"
@buf generate https://github.com/raystack/proton/archive/${PROTON_COMMIT}.zip#strip_components=1 --template buf.gen.yaml --path raystack/stencil
@buf generate
@echo " > protobuf compilation finished"

clean: ## Clean the build artifacts
Expand All @@ -35,4 +33,4 @@ ui:
@cd ui && $(MAKE) dep && $(MAKE) dist

help: ## Display this help message
@cat $(MAKEFILE_LIST) | grep -e "^[a-zA-Z_\-]*: *.*## *" | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
@cat $(MAKEFILE_LIST) | grep -e "^[a-zA-Z_\-]*: *.*## *" | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
36 changes: 20 additions & 16 deletions buf.gen.yaml
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
version: v1
version: v2
inputs:
- git_repo: https://github.com/raystack/proton.git
ref: 8ef3e30859491f877a791fe0f9039370235c99b2
paths:
- raystack/stencil
managed:
enabled: true
override:
- file_option: go_package
path: raystack/stencil/v1beta1/stencil.proto
value: github.com/raystack/stencil/gen/raystack/stencil/v1beta1;stencilv1beta1
plugins:
- name: go
out: ./proto
opt: paths=source_relative
- name: go-grpc
out: ./proto
opt: paths=source_relative
- remote: buf.build/raystack/plugins/validate
out: "proto"
opt: "paths=source_relative,lang=go"
- name: grpc-gateway
out: ./proto
opt: paths=source_relative
- name: openapiv2
out: ./proto
opt: "allow_merge=true"
- remote: buf.build/protocolbuffers/go:v1.36.11
out: gen
opt:
- paths=source_relative
- remote: buf.build/connectrpc/go:v1.18.1
out: gen
opt:
- paths=source_relative
24 changes: 11 additions & 13 deletions cmd/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,18 @@ package cmd

import (
"context"
"errors"
"fmt"
"os"

"connectrpc.com/connect"
"github.com/MakeNowJust/heredoc"
"github.com/raystack/salt/cli/printer"
stencilv1beta1 "github.com/raystack/stencil/proto/raystack/stencil/v1beta1"
stencilv1beta1 "github.com/raystack/stencil/gen/raystack/stencil/v1beta1"
"github.com/spf13/cobra"
"google.golang.org/grpc/status"
)

func checkSchemaCmd(cdk *CDK) *cobra.Command {
var comp, file, namespaceID string
var req stencilv1beta1.CheckCompatibilityRequest

cmd := &cobra.Command{
Use: "check <id>",
Expand All @@ -36,23 +34,23 @@ func checkSchemaCmd(cdk *CDK) *cobra.Command {
return err
}

client, cancel, err := createClient(cmd, cdk)
client, err := createClient(cmd, cdk)
if err != nil {
return err
}
defer cancel()

schemaID := args[0]

req.Data = fileData
req.NamespaceId = namespaceID
req.SchemaId = schemaID
req.Compatibility = stencilv1beta1.Schema_Compatibility(stencilv1beta1.Schema_Compatibility_value[comp])
req := &stencilv1beta1.CheckCompatibilityRequest{
Data: fileData,
NamespaceId: namespaceID,
SchemaId: schemaID,
Compatibility: stencilv1beta1.Schema_Compatibility(stencilv1beta1.Schema_Compatibility_value[comp]),
}

_, err = client.CheckCompatibility(context.Background(), &req)
_, err = client.CheckCompatibility(context.Background(), connect.NewRequest(req))
if err != nil {
errStatus := status.Convert(err)
return errors.New(errStatus.Message())
return err
}

spinner.Stop()
Expand Down
41 changes: 10 additions & 31 deletions cmd/client.go
Original file line number Diff line number Diff line change
@@ -1,55 +1,34 @@
package cmd

import (
"context"
"time"
"net/http"

"github.com/raystack/salt/config"
stencilv1beta1 "github.com/raystack/stencil/proto/raystack/stencil/v1beta1"
stencilv1beta1connect "github.com/raystack/stencil/gen/raystack/stencil/v1beta1/stencilv1beta1connect"
"github.com/spf13/cobra"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)

type ClientConfig struct {
Host string `yaml:"host" cmdx:"host"`
}

func createConnection(ctx context.Context, host string) (*grpc.ClientConn, error) {
opts := []grpc.DialOption{
grpc.WithTransportCredentials(insecure.NewCredentials()),
grpc.WithBlock(),
}

return grpc.DialContext(ctx, host, opts...)
}

func createClient(cmd *cobra.Command, cdk *CDK) (stencilv1beta1.StencilServiceClient, func(), error) {
func createClient(cmd *cobra.Command, cdk *CDK) (stencilv1beta1connect.StencilServiceClient, error) {
c, err := loadClientConfig(cmd, cdk.Config)
if err != nil {
return nil, nil, err
return nil, err
}

host := c.Host

if host == "" {
return nil, nil, ErrClientConfigHostNotFound
}

dialTimeoutCtx, dialCancel := context.WithTimeout(cmd.Context(), time.Second*2)
conn, err := createConnection(dialTimeoutCtx, host)
if err != nil {
dialCancel()
return nil, nil, err
}

cancel := func() {
dialCancel()
conn.Close()
return nil, ErrClientConfigHostNotFound
}

client := stencilv1beta1.NewStencilServiceClient(conn)
return client, cancel, nil
client := stencilv1beta1connect.NewStencilServiceClient(
http.DefaultClient,
host,
)
return client, nil
}

func loadClientConfig(cmd *cobra.Command, cmdxConfig *config.Loader) (*ClientConfig, error) {
Expand Down
36 changes: 17 additions & 19 deletions cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,62 +2,60 @@ package cmd

import (
"context"
"errors"
"fmt"
"os"

"connectrpc.com/connect"
"github.com/MakeNowJust/heredoc"
"github.com/raystack/salt/cli/printer"
stencilv1beta1 "github.com/raystack/stencil/proto/raystack/stencil/v1beta1"
stencilv1beta1 "github.com/raystack/stencil/gen/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
$ 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)
client, 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])
req := &stencilv1beta1.CreateSchemaRequest{
NamespaceId: namespaceID,
SchemaId: schemaID,
Data: fileData,
Format: stencilv1beta1.Schema_Format(stencilv1beta1.Schema_Format_value[format]),
Compatibility: stencilv1beta1.Schema_Compatibility(stencilv1beta1.Schema_Compatibility_value[comp]),
}

res, err := client.CreateSchema(context.Background(), &req)
res, err := client.CreateSchema(context.Background(), connect.NewRequest(req))
if err != nil {
errStatus := status.Convert(err)
if codes.AlreadyExists == errStatus.Code() {
connectErr, ok := err.(*connect.Error)
if ok && connectErr.Code() == connect.CodeAlreadyExists {
fmt.Printf("\n%s Schema with id '%s' already exist.\n", printer.Icon("failure"), args[0])
return nil
}
return errors.New(errStatus.Message())
return err
}

id := res.GetId()
id := res.Msg.GetId()

spinner.Stop()
fmt.Printf("\n%s Created schema with id %s.\n", printer.Green(printer.Icon("success")), printer.Cyan(id))
Expand Down
26 changes: 12 additions & 14 deletions cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,15 @@ import (
"context"
"fmt"

"connectrpc.com/connect"
"github.com/MakeNowJust/heredoc"
"github.com/raystack/salt/cli/printer"
stencilv1beta1 "github.com/raystack/stencil/proto/raystack/stencil/v1beta1"
stencilv1beta1 "github.com/raystack/stencil/gen/raystack/stencil/v1beta1"
"github.com/spf13/cobra"
)

func deleteSchemaCmd(cdk *CDK) *cobra.Command {
var namespaceID string
var req stencilv1beta1.DeleteSchemaRequest
var reqVer stencilv1beta1.DeleteVersionRequest
var version int32

cmd := &cobra.Command{
Expand All @@ -27,28 +26,27 @@ func deleteSchemaCmd(cdk *CDK) *cobra.Command {
spinner := printer.Spin("")
defer spinner.Stop()

client, cancel, err := createClient(cmd, cdk)
client, err := createClient(cmd, cdk)
if err != nil {
return err
}
defer cancel()

schemaID := args[0]

if version == 0 {
req.NamespaceId = namespaceID
req.SchemaId = schemaID

_, err = client.DeleteSchema(context.Background(), &req)
_, err = client.DeleteSchema(context.Background(), connect.NewRequest(&stencilv1beta1.DeleteSchemaRequest{
NamespaceId: namespaceID,
SchemaId: schemaID,
}))
if err != nil {
return err
}
} else {
reqVer.NamespaceId = namespaceID
reqVer.SchemaId = schemaID
reqVer.VersionId = version

_, err = client.DeleteVersion(context.Background(), &reqVer)
_, err = client.DeleteVersion(context.Background(), connect.NewRequest(&stencilv1beta1.DeleteVersionRequest{
NamespaceId: namespaceID,
SchemaId: schemaID,
VersionId: version,
}))
if err != nil {
return err
}
Expand Down
Loading
Loading