Skip to content

Commit

Permalink
better env lookup
Browse files Browse the repository at this point in the history
  • Loading branch information
jancajthaml committed Feb 20, 2021
1 parent 69ee757 commit f525124
Show file tree
Hide file tree
Showing 17 changed files with 428 additions and 540 deletions.
21 changes: 11 additions & 10 deletions services/fio-bco-import/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package config

import (
"github.com/jancajthaml-openbank/fio-bco-import/support/env"
"strings"
"time"
)
Expand Down Expand Up @@ -46,15 +47,15 @@ type Configuration struct {
// LoadConfig loads application configuration
func LoadConfig() Configuration {
return Configuration{
Tenant: envString("FIO_BCO_TENANT", ""),
RootStorage: envString("FIO_BCO_STORAGE", "/data") + "/t_" + envString("FIO_BCO_TENANT", "") + "/import/fio",
EncryptionKey: envSecret("FIO_BCO_ENCRYPTION_KEY", nil),
FioGateway: envString("FIO_BCO_FIO_GATEWAY", "https://www.fio.cz/ib_api/rest"),
LedgerGateway: envString("FIO_BCO_LEDGER_GATEWAY", "https://127.0.0.1:4401"),
VaultGateway: envString("FIO_BCO_VAULT_GATEWAY", "https://127.0.0.1:4400"),
LakeHostname: envString("FIO_BCO_LAKE_HOSTNAME", "127.0.0.1"),
SyncRate: envDuration("FIO_BCO_SYNC_RATE", 22*time.Second),
LogLevel: strings.ToUpper(envString("FIO_BCO_LOG_LEVEL", "INFO")),
MetricsStastdEndpoint: envString("FIO_BCO_STATSD_ENDPOINT", "127.0.0.1:8125"),
Tenant: env.String("FIO_BCO_TENANT", ""),
RootStorage: env.String("FIO_BCO_STORAGE", "/data") + "/t_" + env.String("FIO_BCO_TENANT", "") + "/import/fio",
EncryptionKey: env.HexFile("FIO_BCO_ENCRYPTION_KEY", nil),
FioGateway: env.String("FIO_BCO_FIO_GATEWAY", "https://www.fio.cz/ib_api/rest"),
LedgerGateway: env.String("FIO_BCO_LEDGER_GATEWAY", "https://127.0.0.1:4401"),
VaultGateway: env.String("FIO_BCO_VAULT_GATEWAY", "https://127.0.0.1:4400"),
LakeHostname: env.String("FIO_BCO_LAKE_HOSTNAME", "127.0.0.1"),
SyncRate: env.Duration("FIO_BCO_SYNC_RATE", 22*time.Second),
LogLevel: strings.ToUpper(env.String("FIO_BCO_LOG_LEVEL", "INFO")),
MetricsStastdEndpoint: env.String("FIO_BCO_STATSD_ENDPOINT", "127.0.0.1:8125"),
}
}
103 changes: 0 additions & 103 deletions services/fio-bco-import/config/environment.go

This file was deleted.

147 changes: 0 additions & 147 deletions services/fio-bco-import/config/environment_test.go

This file was deleted.

2 changes: 1 addition & 1 deletion services/fio-bco-import/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.15
require (
github.com/DataDog/datadog-go v4.4.0+incompatible
github.com/Microsoft/go-winio v0.4.16 // indirect
github.com/jancajthaml-openbank/actor-system v1.4.6
github.com/jancajthaml-openbank/actor-system v1.4.7
github.com/jancajthaml-openbank/local-fs v1.2.2
github.com/rs/zerolog v1.20.0
github.com/stretchr/testify v1.6.1 // indirect
Expand Down
10 changes: 4 additions & 6 deletions services/fio-bco-import/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,16 @@ github.com/DataDog/datadog-go v4.4.0+incompatible/go.mod h1:LButxg5PwREeZtORoXG3
github.com/Microsoft/go-winio v0.4.16 h1:FtSW/jqD+l4ba5iPBj9CODVtgfYAD8w2wS923g/cFDk=
github.com/Microsoft/go-winio v0.4.16/go.mod h1:XB6nPKklQyQ7GC9LdcBEcBl8PF76WugXOPRXwdLnMv0=
github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/jancajthaml-openbank/actor-system v1.4.6 h1:mo9DFiFW136tLG3oYRXGvEgwPmxvrDxmzQiWj3WpVq4=
github.com/jancajthaml-openbank/actor-system v1.4.6/go.mod h1:mLnN69sePtKknn+OL18hOM4N8f/llDIoia98eNzBkXo=
github.com/jancajthaml-openbank/actor-system v1.4.7 h1:sqO/7BTm47y/NQ29ZuWxgPU7d2CmIEH1pCrcKyN/7ck=
github.com/jancajthaml-openbank/actor-system v1.4.7/go.mod h1:KzC0k0aZLZyj+eKAyEByXqM2eKCCKmBlMcjo2TXLtYU=
github.com/jancajthaml-openbank/local-fs v1.2.2 h1:zBhEDRh2zY7VI1KHTMwp8uqxTP7XR9dB9cK0NYGnzmw=
github.com/jancajthaml-openbank/local-fs v1.2.2/go.mod h1:aeF1ybqH3QQsFvqGdvk/0+5TXnHk1T7R1l+ZQ5d8SkY=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/pebbe/zmq4 v1.2.2 h1:RZ5Ogp0D5S6u+tSxopnI3afAf0ifWbvQOAw9HxXvZP4=
github.com/pebbe/zmq4 v1.2.2/go.mod h1:7N4y5R18zBiu3l0vajMUWQgZyjv464prE8RCyBcmnZM=
github.com/pebbe/zmq4 v1.2.3 h1:SwOjTnXrtwyq4Hs4AXghkrNNEMuMnpvCZfEGTkDgrgI=
github.com/pebbe/zmq4 v1.2.3/go.mod h1:7N4y5R18zBiu3l0vajMUWQgZyjv464prE8RCyBcmnZM=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand All @@ -22,7 +21,6 @@ github.com/rs/xid v1.2.1/go.mod h1:+uKXf+4Djp6Md1KODXJxgGQPKngRmWyn10oCKFzNHOQ=
github.com/rs/zerolog v1.20.0 h1:38k9hgtUBdxFwE34yS8rTHmHBa4eN16E4DJlv177LNs=
github.com/rs/zerolog v1.20.0/go.mod h1:IzD0RJ65iWH0w97OQQebJEvTZYvsCUm9WVLWBQrJRjo=
github.com/sirupsen/logrus v1.4.1/go.mod h1:ni0Sbl8bgC9z8RoU9G6nDWqqs/fq4eDPysMBDgk/93Q=
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1 h1:2vfRuCMp5sSVIDSqO8oNnWJq7mPa6KVP3iPIwFBuy8A=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand Down
74 changes: 74 additions & 0 deletions services/fio-bco-import/support/env/lookup.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// Copyright (c) 2016-2021, Jan Cajthaml <jan.cajthaml@gmail.com>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package env

import (
"encoding/hex"
"io/ioutil"
"os"
"path/filepath"
"time"
)

// Get retrieves the string value of the environment variable named by the key
func Get(key string) (string, bool) {
if v := os.Getenv(key); v != "" {
return v, true
}
return "", false
}

// String retrieves the string value from environment named by the key.
func String(key string, fallback string) string {
if str, exists := Get(key); exists {
return str
}
return fallback
}

// Duration retrieves duration value from the environment.
func Duration(key string, fallback time.Duration) time.Duration {
if str, exists := Get(key); exists {
v, err := time.ParseDuration(str)
if err != nil {
log.Warn().Msgf("invalid value in %s, using fallback", key)
return fallback
}
return v
}
return fallback
}

// HexFile retrieves bytes value from hexfile path defined from the environment.
func HexFile(key string, fallback []byte) []byte {
if str, exists := Get(key); exists {
data, err := ioutil.ReadFile(filepath.Clean(str))
if err != nil {
log.Warn().Msgf("invalid value in %s, using fallback", key)
return fallback
}
if len(data) == 0 {
log.Warn().Msgf("invalid value in %s, using fallback", key)
return fallback
}
v, err := hex.DecodeString(string(data))
if err != nil {
log.Warn().Msgf("invalid value in %s, using fallback", key)
return fallback
}
return []byte(v)
}
return fallback
}

0 comments on commit f525124

Please sign in to comment.