Skip to content

Commit

Permalink
feat: add cli that checks the validity of avro schemas (#31)
Browse files Browse the repository at this point in the history
* feat: add cli that checks the validity of avro schemas

Signed-off-by: Georgi Ivanov <georgi@ouzi.dev>

* fix: update dependencies

Signed-off-by: Georgi Ivanov <georgi@ouzi.dev>
  • Loading branch information
givanov authored Jul 20, 2020
1 parent 12c106f commit 0ba77e4
Show file tree
Hide file tree
Showing 10 changed files with 628 additions and 34 deletions.
7 changes: 7 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ covhtml: test
@scripts/coverage.sh
@go tool cover -html=.cover/cover.out

.PHONY: test-checker-cli
test-checker-cli:
$(MAKE) -C cli bootstrap test lint

.PHONY: package-checker-cli
package-checker-cli:
$(MAKE) -C cli clean dist VERSION=$(VERSION)

.PHONY: semantic-release
semantic-release:
Expand Down
1 change: 1 addition & 0 deletions cli/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
_dist/
81 changes: 81 additions & 0 deletions cli/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
BUILD_PATH = github.com/ouzi-dev/avro-kedavro/cli
TARGETS ?= darwin/amd64 linux/amd64 windows/amd64
DIST_DIRS = find * -type d -exec
LDFLAGS := -w -s
NAME = avro-kedavro-checker

GIT_SHORT_COMMIT := $(shell git rev-parse --short HEAD)
GIT_TAG := $(shell git describe --tags --abbrev=0 --exact-match 2>/dev/null)
GIT_DIRTY = $(shell test -n "`git status --porcelain`" && echo "dirty" || echo "clean")

TMP_VERSION := $(GIT_SHORT_COMMIT)


ifndef VERSION
ifeq ($(GIT_DIRTY), clean)
ifdef GIT_TAG
TMP_VERSION = $(GIT_TAG)
endif
endif
endif

VERSION ?= $(TMP_VERSION)

HAS_GOLANCI_LINT := $(shell command -v golangci-lint;)
GOLANGCI_LINT_VERSION := v1.21.0

SRC = $(shell find . -type f -name '*.go' -not -path "./vendor/*")

.PHONY: tidy
tidy:
@echo "tidy target..."
@go mod tidy

.PHONY: vendor
vendor: tidy
@echo "vendor target..."
@go mod vendor

.PHONY: test
test: fmt lint
@echo "test target..."
@go test ./... -v -count=1

.PHONY: lint
lint: bootstrap
@echo "lint target..."
@golangci-lint run --enable-all --disable lll,godox,wsl,funlen,gochecknoglobals,gochecknoinits ./...

.PHONY: bootstrap
bootstrap:
@echo "bootstrap target..."
ifndef HAS_GOLANCI_LINT
@GOPROXY=direct GOSUMDB=off go get -u github.com/golangci/golangci-lint/cmd/golangci-lint@$(GOLANGCI_LINT_VERSION)
endif

.PHONY: fmt
fmt:
@echo "fmt target..."
@find . -name '*.go' -not -wholename './vendor/*' | while read -r file; do gofmt -w -s "$$file"; goimports -w "$$file"; done

.PHONY: build
build: build-cross

# usage: make clean build-cross dist VERSION=v0.2-alpha
.PHONY: build-cross
build-cross: LDFLAGS += -extldflags "-static"
build-cross:
CGO_ENABLED=0 gox -parallel=3 -output="_dist/{{.OS}}-{{.Arch}}/avro-kedavro-checker" -osarch='$(TARGETS)' -ldflags '$(LDFLAGS)' $(BUILD_PATH)

.PHONY: dist
dist: build
ls
@( \
cd _dist && \
$(DIST_DIRS) tar -zcf $(NAME)-${VERSION}-{}.tar.gz {} \; && \
$(DIST_DIRS) zip -r $(NAME)-${VERSION}-{}.zip {} \; \
)

.PHONY: clean
clean:
@rm -rf $(BINDIR) ./_dist
58 changes: 58 additions & 0 deletions cli/cmd/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package cmd

import (
"fmt"
"os"

"github.com/spf13/cobra"

homedir "github.com/mitchellh/go-homedir"
"github.com/spf13/viper"
)

var cfgFile string

// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "avro-kedavro-check",
Short: "A CLI to verify compatibility with avro-kedavro",
}

func Execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}

func init() {
cobra.OnInitialize(initConfig)

rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.cli.yaml)")
}

// initConfig reads in config file and ENV variables if set.
func initConfig() {
if cfgFile != "" {
// Use config file from the flag.
viper.SetConfigFile(cfgFile)
} else {
// Find home directory.
home, err := homedir.Dir()
if err != nil {
fmt.Println(err)
os.Exit(1)
}

// Search config in home directory with name ".cli" (without extension).
viper.AddConfigPath(home)
viper.SetConfigName(".cli")
}

viper.AutomaticEnv() // read in environment variables that match

// If a config file is found, read it in.
if err := viper.ReadInConfig(); err == nil {
fmt.Println("Using config file:", viper.ConfigFileUsed())
}
}
38 changes: 38 additions & 0 deletions cli/cmd/verifySchema.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package cmd

import (
"io/ioutil"

"github.com/ouzi-dev/avro-kedavro/pkg/kedavro"
"github.com/spf13/cobra"
)

var schemaFilePath = ""

// verifySchemaCmd represents the verifySchema command
var verifySchemaCmd = &cobra.Command{
Use: "verifySchema",
Short: "Verifies an avro schema is syntactically correct",
RunE: func(cmd *cobra.Command, args []string) error {
data, err := ioutil.ReadFile(schemaFilePath)
if err != nil {
// Error reading file
return err
}

_, err = kedavro.NewParser(string(data), kedavro.WithStringToNumber(), kedavro.WithTimestampToMillis())
if err != nil {
// Error parsing schema
return err
}

return nil
},
}

func init() {
rootCmd.AddCommand(verifySchemaCmd)

verifySchemaCmd.Flags().StringVarP(&schemaFilePath, "schema-file-path", "s", "", "The path to the schema file to check")
_ = verifySchemaCmd.MarkFlagRequired("schema-file-path")
}
10 changes: 10 additions & 0 deletions cli/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module github.com/ouzi-dev/avro-kedavro/cli

go 1.14

require (
github.com/mitchellh/go-homedir v1.1.0
github.com/ouzi-dev/avro-kedavro v0.4.0
github.com/spf13/cobra v1.0.0
github.com/spf13/viper v1.7.0
)
Loading

0 comments on commit 0ba77e4

Please sign in to comment.