Skip to content

Commit

Permalink
Added make file and root persistent flag
Browse files Browse the repository at this point in the history
  • Loading branch information
snovichkov committed Feb 7, 2019
1 parent 81e2606 commit 6972fe5
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 19 deletions.
63 changes: 63 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# Set the mode for code-coverage
GO_TEST_COVERAGE_MODE ?= count
GO_TEST_COVERAGE_FILE_NAME ?= coverage.out

# Set a default `min_confidence` value for `golint`
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}"
@go test -coverpkg=`go list ./... | grep -vE 'mock' | tr '\n' ','` -covermode ${GO_TEST_COVERAGE_MODE} -coverprofile=${GO_TEST_COVERAGE_FILE_NAME} ./...
@echo "Generate coverage report";
@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
75 changes: 56 additions & 19 deletions viper.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
// Package viper provide gozix viper bundle.
package viper

import (
"os"
"strings"

"github.com/gozix/glue"
"github.com/sarulabs/di"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/spf13/viper"
)

Expand All @@ -26,8 +30,13 @@ type (
optionFunc func(bundle *Bundle)
)

// BundleName is default definition name.
const BundleName = "viper"
const (
// BundleName is default definition name.
BundleName = "viper"

// DefConfigFlag is config persistent flag name.
DefConfigFlag = "cli.persistent_flags.config"
)

// NewBundle create bundle instance.
func NewBundle() *Bundle {
Expand Down Expand Up @@ -109,24 +118,52 @@ func (b *Bundle) Name() string {

// Build implements the glue.Bundle interface.
func (b *Bundle) Build(builder *di.Builder) error {
return builder.Add(di.Def{
Name: BundleName,
Build: func(ctn di.Container) (_ interface{}, err error) {
var registry glue.Registry
if err = ctn.Fill(glue.DefRegistry, &registry); err != nil {
return nil, err
}

var path string
if err = registry.Fill("app.path", &path); err != nil {
return nil, err
}

b.viper.AddConfigPath(path)

return b.viper, b.viper.ReadInConfig()
return builder.Add(
di.Def{
Name: BundleName,
Build: func(ctn di.Container) (_ interface{}, err error) {
var registry glue.Registry
if err = ctn.Fill(glue.DefRegistry, &registry); err != nil {
return nil, err
}

var path string
if err = registry.Fill("app.path", &path); err != nil {
return nil, err
}

b.viper.AddConfigPath(path)

var cmd *cobra.Command
if err = registry.Fill("cli.cmd", &cmd); err != nil {
return nil, err
}

var configFile string
if configFile, err = cmd.Flags().GetString("config"); err != nil {
return nil, err
}

if len(configFile) > 0 {
b.viper.SetConfigFile(configFile)
}

return b.viper, b.viper.ReadInConfig()
},
},
})
di.Def{
Name: DefConfigFlag,
Tags: []di.Tag{{
Name: glue.TagRootPersistentFlags,
}},
Build: func(_ di.Container) (i interface{}, e error) {
var flagSet = pflag.NewFlagSet(os.Args[0], pflag.ExitOnError)
flagSet.StringP("config", "c", "", "config file")

return flagSet, nil
},
},
)
}

// apply implements Option.
Expand Down

0 comments on commit 6972fe5

Please sign in to comment.