-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add cli that checks the validity of avro schemas (#31)
* 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
Showing
10 changed files
with
628 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
_dist/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) |
Oops, something went wrong.