diff --git a/Makefile b/Makefile index 4c32fe6..c6fb8eb 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,3 @@ -.PHONY: all test test-with-coverage test-with-coverage-profile lint lint-format lint-import lint-style clean - # Set the mode for code-coverage GO_TEST_COVERAGE_MODE ?= count GO_TEST_COVERAGE_FILE_NAME ?= coverage.out @@ -9,14 +7,17 @@ GO_LINT_MIN_CONFIDENCE ?= 0.2 all: test +.PHONY: test test: @echo "Run unit tests" @go test -v ./... +.PHONY: test-with-coverage test-with-coverage: @echo "Run unit tests with coverage" @go test -cover ./... +.PHONY: test-with-coverage-profile test-with-coverage-profile: @echo "Run unit tests with coverage profile" @echo "mode: ${GO_TEST_COVERAGE_MODE}" > "${GO_TEST_COVERAGE_FILE_NAME}" @@ -25,20 +26,38 @@ test-with-coverage-profile: @go tool cover -func="${GO_TEST_COVERAGE_FILE_NAME}"; @rm "${GO_TEST_COVERAGE_FILE_NAME}"; +.PHONY: fix +fix: fix-format fix-import + +.PHONY: fix-import +fix-import: + @echo "Fix imports" + @errors=$$(goimports -l -w -local $(GO_PKG) $$(go list -f "{{ .Dir }}" ./...)); if [ "$${errors}" != "" ]; then echo "$${errors}"; fi + +.PHONY: fix-format +fix-format: + @echo "Fix formatting" + @gofmt -w ${GO_FMT_FLAGS} $$(go list -f "{{ .Dir }}" ./...); if [ "$${errors}" != "" ]; then echo "$${errors}"; fi + +.PHONY: lint lint: lint-format lint-import lint-style +.PHONY: lint-format lint-format: @echo "Check formatting" @errors=$$(gofmt -l $$(go list -f "{{ .Dir }}" ./...)); if [ "$${errors}" != "" ]; then echo "Invalid format:\n$${errors}"; exit 1; fi +.PHONY: lint-import lint-import: @echo "Check imports" @errors=$$(goimports -l $$(go list -f "{{ .Dir }}" ./...)); if [ "$${errors}" != "" ]; then echo "Invalid imports:\n$${errors}"; exit 1; fi +.PHONY: lint-style lint-style: @echo "Check code style" @errors=$$(golint -min_confidence=${GO_LINT_MIN_CONFIDENCE} $$(go list ./...)); if [ "$${errors}" != "" ]; then echo "Invalid code style:\n$${errors}"; exit 1; fi +.PHONY: clean clean: @echo "Cleanup" @find . -type f -name "*coverage*.out" -delete diff --git a/glue.go b/glue.go index c5ec8ac..4142b60 100644 --- a/glue.go +++ b/glue.go @@ -197,7 +197,7 @@ func (k *app) initContainer() error { Use: fmt.Sprintf("%s [command]", os.Args[0]), // TODO: replace to binary name SilenceUsage: true, SilenceErrors: true, - PreRun: func(cmd *cobra.Command, args []string) { + PersistentPreRun: func(cmd *cobra.Command, args []string) { registry.Set("cli.cmd", cmd) registry.Set("cli.args", args) }, @@ -205,24 +205,24 @@ func (k *app) initContainer() error { // register commands by tag for name, def := range ctn.Definitions() { - Tags: + Tags: for _, tag := range def.Tags { switch tag.Name { - case TagCliCommand: - var command *cobra.Command - if err = ctn.Fill(name, &command); err != nil { - return nil, err - } - - rootCmd.AddCommand(command) - break Tags - case TagRootPersistentFlags: - var pf *pflag.FlagSet - if err = ctn.Fill(name, &pf); err != nil { - return nil, err - } - rootCmd.PersistentFlags().AddFlagSet(pf) - break Tags + case TagCliCommand: + var command *cobra.Command + if err = ctn.Fill(name, &command); err != nil { + return nil, err + } + + rootCmd.AddCommand(command) + break Tags + case TagRootPersistentFlags: + var pf *pflag.FlagSet + if err = ctn.Fill(name, &pf); err != nil { + return nil, err + } + rootCmd.PersistentFlags().AddFlagSet(pf) + break Tags } } }