Skip to content

Commit

Permalink
Merge pull request #32 from fastbill/update-go-and-deps
Browse files Browse the repository at this point in the history
Update Go, dependencies, linter and fix bug with envloader
  • Loading branch information
junedev authored May 16, 2022
2 parents f720863 + 79bed6e commit 8309398
Show file tree
Hide file tree
Showing 17 changed files with 1,535 additions and 524 deletions.
30 changes: 30 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: Run tests and linter

on: [push, pull_request, workflow_dispatch]

jobs:

test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v3
with:
go-version: '^1.18.x'
- name: Install dependencies
run: go mod vendor
- name: Run tests
run: go test -race -cover ./...

lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v3
with:
go-version: '^1.18.x'
- name: Install dependencies
run: go mod vendor
- uses: golangci/golangci-lint-action@v3
with:
version: v1.45
7 changes: 3 additions & 4 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
linters:
enable:
- gocyclo
- golint
- revive
- dupl
- interfacer
- unconvert
- goconst
- gosec
Expand All @@ -25,13 +24,13 @@ issues:
- govet
text: 'shadow: declaration of "err" shadows declaration'
- linters:
- golint
- revive
text: 'in another file for this package'

linters-settings:
gocyclo:
min-complexity: 10
golint:
revive:
min-confidence: 0
govet:
check-shadowing: true
15 changes: 0 additions & 15 deletions .travis.yml

This file was deleted.

46 changes: 31 additions & 15 deletions envloader/envloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,38 +28,54 @@ func LoadEnvs(folderPath string) error {
customConfigPath := path.Join(folderPath, stage+".env")
defaultConfigPath := path.Join(folderPath, DefaultEnvFile)

missingEnvs := []string{}
combinedEnvMap, err := createCombinedEnvMap(customConfigPath, defaultConfigPath)
err := checkForMissingEnvs(customConfigPath, defaultConfigPath)
if err != nil {
return err
}

for envName, value := range combinedEnvMap {
if value == "" && os.Getenv(envName) == "" {
missingEnvs = append(missingEnvs, envName)
}
}
if len(missingEnvs) > 0 {
return fmt.Errorf("environment variables missing: %v", missingEnvs)
}
return godotenv.Load(customConfigPath, defaultConfigPath)
}

func createCombinedEnvMap(customConfigPath string, defaultConfigPath string) (map[string]string, error) {
// checkForMissingEnvs errors if an env was defined in the default but not set in
// either the default file, custom config file or environment variables.
// Returns nil otherwise.
func checkForMissingEnvs(customConfigPath string, defaultConfigPath string) error {
envMapCustom, err := godotenv.Read(customConfigPath)
if err != nil {
return nil, err
return fmt.Errorf("error reading custom config file: %w", err)
}
envMapDefault, err := godotenv.Read(defaultConfigPath)
if err != nil {
return nil, err
return fmt.Errorf("error reading default config file: %w", err)
}

envMapCombined := combineMaps(envMapDefault, envMapCustom)

missingEnvs := []string{}
for envName, value := range envMapCombined {
_, keyPresentInCustomEnvs := envMapCustom[envName]
if value == "" && os.Getenv(envName) == "" && !keyPresentInCustomEnvs {
missingEnvs = append(missingEnvs, envName)
}
}
if len(missingEnvs) > 0 {
return fmt.Errorf("environment variables missing: %v", missingEnvs)
}

return nil
}

func combineMaps(envMapDefault, envMapOverwrite map[string]string) map[string]string {
envMapCombined := map[string]string{}
for key, value := range envMapOverwrite {
envMapCombined[key] = value
}

envMapCombined := envMapCustom
for key, value := range envMapDefault {
if envMapCombined[key] == "" {
envMapCombined[key] = value
}
}
return envMapCombined, nil

return envMapCombined
}
33 changes: 21 additions & 12 deletions envloader/envloader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,16 @@ import (
)

func TestLoadingDefaults(t *testing.T) {
defer cleanup(t)
err := LoadEnvs("testdata")
if assert.NoError(t, err) {
assert.Equal(t, "defaultValue1", os.Getenv("ENVLOADER_TESTKEY1"))
assert.Equal(t, "defaultValue2", os.Getenv("ENVLOADER_TESTKEY2"))
}
assert.NoError(t, cleanup())
}

func TestOverwritingDefaultsWithCustomsSuccess(t *testing.T) {
defer cleanup(t)
DefaultEnvFile = "production_missing.env"
StageEnv = "ENVLOADER_APP_ENV"
err := os.Setenv("ENVLOADER_APP_ENV", "teststage_success")
Expand All @@ -28,10 +29,10 @@ func TestOverwritingDefaultsWithCustomsSuccess(t *testing.T) {
assert.Equal(t, "customValue2", os.Getenv("ENVLOADER_TESTKEY2"))
assert.Equal(t, "customValue3", os.Getenv("ENVLOADER_TESTKEY3"))
}
assert.NoError(t, cleanup())
}

func TestNotOverwritingExistingEnvs(t *testing.T) {
defer cleanup(t)
err := os.Setenv("ENVLOADER_TESTKEY1", "outerValue1")
assert.NoError(t, err)
err = os.Setenv("ENVLOADER_TESTKEY2", "outerValue2")
Expand All @@ -48,10 +49,10 @@ func TestNotOverwritingExistingEnvs(t *testing.T) {
assert.Equal(t, "outerValue2", os.Getenv("ENVLOADER_TESTKEY2"))
assert.Equal(t, "customValue3", os.Getenv("ENVLOADER_TESTKEY3"))
}
assert.NoError(t, cleanup())
}

func TestOverwritingDefaultsWithCustomsFail(t *testing.T) {
defer cleanup(t)
DefaultEnvFile = "production_missing.env"
StageEnv = "ENVLOADER_APP_ENV"
err := os.Setenv("ENVLOADER_APP_ENV", "teststage_fail")
Expand All @@ -60,27 +61,35 @@ func TestOverwritingDefaultsWithCustomsFail(t *testing.T) {
if assert.Error(t, err) {
assert.Equal(t, "environment variables missing: [ENVLOADER_TESTKEY3]", err.Error())
}
assert.NoError(t, cleanup())
}

func TestOverwriteEmptyDefaultWithEmptyValue(t *testing.T) {
defer cleanup(t)
DefaultEnvFile = "production_missing.env"
StageEnv = "ENVLOADER_APP_ENV"
err := os.Setenv("ENVLOADER_APP_ENV", "empty_overwrite")
assert.NoError(t, err)
err = LoadEnvs("testdata")
assert.NoError(t, err)
assert.Equal(t, "", os.Getenv("ENVLOADER_TESTKEY3"))
}

func TestMissingEnvsAreDetected(t *testing.T) {
defer cleanup(t)
DefaultEnvFile = "production_missing.env"
StageEnv = "ENVLOADER_APP_ENV"
err := os.Setenv("ENVLOADER_APP_ENV", "missing")
assert.NoError(t, err)
err = LoadEnvs("testdata")
assert.Equal(t, "environment variables missing: [ENVLOADER_TESTKEY4]", err.Error())
assert.NoError(t, cleanup())
assert.Error(t, err)
assert.Equal(t, "environment variables missing: [ENVLOADER_TESTKEY3]", err.Error())
}

func cleanup() error {
func cleanup(t *testing.T) {
for _, line := range os.Environ() {
if strings.HasPrefix(line, "ENVLOADER") {
pair := strings.Split(line, "=")
err := os.Unsetenv(pair[0])
if err != nil {
return err
}
assert.NoError(t, os.Unsetenv(pair[0]))
}
}
return nil
}
1 change: 1 addition & 0 deletions envloader/testdata/empty_overwrite.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ENVLOADER_TESTKEY3=""
3 changes: 1 addition & 2 deletions envloader/testdata/missing.env
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
ENVLOADER_TESTKEY4=
ENVLOADER_TESTKEY3=customValue3
ENVLOADER_TESTKEY4=customValue4
3 changes: 2 additions & 1 deletion example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import (
"os"
"time"

toolkit "github.com/fastbill/go-service-toolkit/v4"
"github.com/labstack/echo/v4"

toolkit "github.com/fastbill/go-service-toolkit/v4"
)

// User holds all basic user information.
Expand Down
85 changes: 59 additions & 26 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,36 +1,69 @@
module github.com/fastbill/go-service-toolkit/v4

go 1.15
go 1.18

require (
github.com/alicebob/miniredis/v2 v2.14.1
github.com/fastbill/go-httperrors/v2 v2.0.1
github.com/getsentry/sentry-go v0.2.1
github.com/go-playground/validator/v10 v10.4.1
github.com/go-redis/redis/v8 v8.4.8
github.com/golang-migrate/migrate/v4 v4.14.1
github.com/alicebob/miniredis/v2 v2.20.0
github.com/fastbill/go-httperrors/v2 v2.0.2
github.com/getsentry/sentry-go v0.13.0
github.com/go-playground/validator/v10 v10.11.0
github.com/go-redis/redis/v8 v8.11.5
github.com/golang-migrate/migrate/v4 v4.15.2
github.com/joho/godotenv v1.4.0
github.com/labstack/echo/v4 v4.7.2
github.com/labstack/gommon v0.3.1
github.com/prometheus/client_golang v1.12.1
github.com/sirupsen/logrus v1.8.1
github.com/stretchr/testify v1.7.1
gorm.io/driver/mysql v1.3.3
gorm.io/driver/postgres v1.3.5
gorm.io/gorm v1.23.5
)

require (
github.com/alicebob/gopher-json v0.0.0-20200520072559-a9ecdc9d1d3a // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/go-playground/locales v0.14.0 // indirect
github.com/go-playground/universal-translator v0.18.0 // indirect
github.com/go-sql-driver/mysql v1.6.0 // indirect
github.com/golang-jwt/jwt v3.2.2+incompatible // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/go-cmp v0.5.7 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/jackc/pgproto3/v2 v2.0.7 // indirect
github.com/joho/godotenv v1.3.0
github.com/labstack/echo/v4 v4.1.6
github.com/labstack/gommon v0.3.0
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/jackc/chunkreader/v2 v2.0.1 // indirect
github.com/jackc/pgconn v1.12.0 // indirect
github.com/jackc/pgio v1.0.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgproto3/v2 v2.3.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20200714003250-2b9c44734f2b // indirect
github.com/jackc/pgtype v1.11.0 // indirect
github.com/jackc/pgx/v4 v4.16.0 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/leodido/go-urn v1.2.1 // indirect
github.com/lib/pq v1.9.0 // indirect
github.com/mattn/go-colorable v0.1.8 // indirect
github.com/pingcap/errors v0.11.4 // indirect
github.com/prometheus/client_golang v1.9.0
github.com/sirupsen/logrus v1.7.0
github.com/lib/pq v1.10.5 // indirect
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182affec369 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_model v0.2.0 // indirect
github.com/prometheus/common v0.34.0 // indirect
github.com/prometheus/procfs v0.7.3 // indirect
github.com/stretchr/objx v0.3.0 // indirect
github.com/stretchr/testify v1.6.1
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasttemplate v1.2.1 // indirect
github.com/yuin/gopher-lua v0.0.0-20200816102855-ee81675732da // indirect
golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad // indirect
golang.org/x/net v0.0.0-20201224014010-6772e930b67b // indirect
golang.org/x/sys v0.0.0-20210110051926-789bb1bd4061 // indirect
golang.org/x/text v0.3.5 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
github.com/yuin/gopher-lua v0.0.0-20220428201426-ff834ae8486b // indirect
go.uber.org/atomic v1.9.0 // indirect
golang.org/x/crypto v0.0.0-20220427172511-eb4f295cb31f // indirect
golang.org/x/net v0.0.0-20220425223048-2871e0cb64e4 // indirect
golang.org/x/sys v0.0.0-20220502124256-b6088ccd6cba // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/time v0.0.0-20220411224347-583f2d630306 // indirect
golang.org/x/xerrors v0.0.0-20220411194840-2f41105eb62f // indirect
google.golang.org/protobuf v1.28.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
gorm.io/driver/mysql v1.0.3
gorm.io/driver/postgres v1.0.6
gorm.io/gorm v1.20.11
)
Loading

0 comments on commit 8309398

Please sign in to comment.