Skip to content

Commit

Permalink
minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
rayjlinden committed Apr 4, 2019
1 parent 9c053be commit d246903
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 41 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
cover.*
46 changes: 46 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
PKGS := $(shell go list ./... | grep -v -E mocks)
SRCS := $(shell find . go.mod -name '*.go') go.mod
export GO111MODULE=on

VERSION := $(shell cat Version)

# Set the code coverage target for this project
COVER_TARGET ?= 85

LINT_OPTS ?= --fix
LINTERS=-E gofmt -E govet -E errcheck -E staticcheck -E gosimple -E structcheck \
-E varcheck -E ineffassign -E typecheck

.PHONEY: build
build: ${SRCS}
@go build
.PHONY: tests
tests: ## Run test suite
@go test -race ${PKGS}

.PHONY: cover
cover: ## Generate test coverage results
@go test --covermode=count -coverprofile cover.profile ${PKGS}
@go tool cover -html cover.profile -o cover.html
@go tool cover -func cover.profile -o cover.func
@tail -n 1 cover.func | awk '{if (int($$3) > ${COVER_TARGET}) {print "Coverage good: " $$3} else {print "Coverage is less than ${COVER_TARGET}%: " $$3; exit 1}}'


.PHONY: lint
lint: ## Run golint and go fmt on source base
@golangci-lint run ${LINT_OPTS} --no-config --disable-all ${LINTERS} ./...

.PHONY: clean
clean: ## Clean up any generated files
@rm -f cover.*

.DEFAULT_GOAL := help
.PHONY: help
help: ## Display this help message
@grep -E '^[ a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | \
awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-15s\033[0m %s\n", $$1, $$2}'

.PHONY: version
version: ## Show the version the Makefile will build
@echo ${VERSION}

1 change: 1 addition & 0 deletions Version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0.2.3
9 changes: 3 additions & 6 deletions env.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ type CustomParsers map[reflect.Type]ParserFunc
// ParserFunc defines the signature of a function that can be used within `CustomParsers`
type ParserFunc func(v string) (interface{}, error)


// Set - sets an environment variable
func Set(key, value string) error {
return os.Setenv(key, value)
Expand All @@ -57,7 +56,6 @@ func Get(key string) string {
return os.Getenv(key)
}


// GetOr - get an environment variable or return default value if does not exist
func GetOr(key, defaultValue string) string {
value, ok := os.LookupEnv(key)
Expand All @@ -68,25 +66,24 @@ func GetOr(key, defaultValue string) string {
}

// MustGet - get an environment variable or panic if does not exist
func MustGet(key) string {
func MustGet(key string) string {
value, ok := os.LookupEnv(key)
if ok {
return value
}
panic(fmt.Sprintf("expected environment variable \"%s\" does not exist", key))
}


// Parse parses a struct containing `env` tags and loads its values from
// environment variables.
func Parse(v interface{}) error {
return ParseWithPrefixFuncs(v, "", make(map[reflect.Type]ParserFunc, 0))
return ParseWithPrefixFuncs(v, "", make(map[reflect.Type]ParserFunc))
}

// ParseWithPrefix parses a struct containing `env` tags and loads its values from
// environment variables. The actual env vars looked up include the passed in prefix.
func ParseWithPrefix(v interface{}, prefix string) error {
return ParseWithPrefixFuncs(v, prefix, make(map[reflect.Type]ParserFunc, 0))
return ParseWithPrefixFuncs(v, prefix, make(map[reflect.Type]ParserFunc))
}

// ParseWithFuncs is the same as `Parse` except it also allows the user to pass
Expand Down
41 changes: 6 additions & 35 deletions env_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package env
package env

import (
"errors"
Expand Down Expand Up @@ -54,8 +54,8 @@ type Config struct {
UnmarshalerPtr *unmarshaler `env:"UNMARSHALER_PTR"`
Unmarshalers []unmarshaler `env:"UNMARSHALERS"`
UnmarshalerPtrs []*unmarshaler `env:"UNMARSHALER_PTRS"`
URL url.URL `env:"URL"`
URLs []url.URL `env:"URLS"`
URL url.URL `env:"URL"`
URLs []url.URL `env:"URLS"`
}

type ParentStruct struct {
Expand Down Expand Up @@ -124,8 +124,8 @@ func TestParsesEnv(t *testing.T) {
assert.Equal(t, []time.Duration{d1, d2, d3}, cfg.Durations)
assert.Equal(t, time.Second, cfg.Unmarshaler.Duration)
assert.Equal(t, time.Minute, cfg.UnmarshalerPtr.Duration)
assert.Equal(t, []unmarshaler{unmarshaler{time.Minute * 2}, unmarshaler{time.Minute * 3}}, cfg.Unmarshalers)
assert.Equal(t, []*unmarshaler{&unmarshaler{time.Minute * 2}, &unmarshaler{time.Minute * 3}}, cfg.UnmarshalerPtrs)
assert.Equal(t, []unmarshaler{{time.Minute * 2}, {time.Minute * 3}}, cfg.Unmarshalers)
assert.Equal(t, []*unmarshaler{{time.Minute * 2}, {time.Minute * 3}}, cfg.UnmarshalerPtrs)
assert.Equal(t, "google.com", cfg.URL.Host)
assert.Equal(t, "ftp", cfg.URLs[0].Scheme)
assert.Equal(t, "23", cfg.URLs[0].Port())
Expand Down Expand Up @@ -387,7 +387,6 @@ func TestNoErrorRequiredSetWithPrefix(t *testing.T) {
assert.Equal(t, "val", cfg.IsRequired)
}


func TestErrorRequiredNotSet(t *testing.T) {
type config struct {
IsRequired string `env:"IS_REQUIRED,required"`
Expand Down Expand Up @@ -672,35 +671,7 @@ func ExampleParse() {
}
os.Setenv("HOME", "/tmp/fakehome")
cfg := config{}
Parse(&cfg)
_ = Parse(&cfg)
fmt.Println(cfg)
// Output: {/tmp/fakehome 3000 false}
}

func ExampleParseRequiredField() {
type config struct {
Home string `env:"HOME"`
Port int `env:"PORT" envDefault:"3000"`
IsProduction bool `env:"PRODUCTION"`
SecretKey string `env:"SECRET_KEY,required"`
}
os.Setenv("HOME", "/tmp/fakehome")
cfg := config{}
err := Parse(&cfg)
fmt.Println(err)
// Output: required environment variable "SECRET_KEY" is not set
}

func ExampleParseMultipleOptions() {
type config struct {
Home string `env:"HOME"`
Port int `env:"PORT" envDefault:"3000"`
IsProduction bool `env:"PRODUCTION"`
SecretKey string `env:"SECRET_KEY,required,option1"`
}
os.Setenv("HOME", "/tmp/fakehome")
cfg := config{}
err := Parse(&cfg)
fmt.Println(err)
// Output: env tag option "option1" not supported
}
10 changes: 10 additions & 0 deletions tag.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/bash

git checkout master
git pull
while IFS='' read -r line || [[ -n "$line" ]]; do
git tag v$line
done < "$1"
git push --tags origin master


0 comments on commit d246903

Please sign in to comment.