Skip to content

Commit

Permalink
fix: unable to bind env variables (#60)
Browse files Browse the repository at this point in the history
* fix: binding env variables

* fix: binding env variables
  • Loading branch information
Deepak Sharma authored May 19, 2021
1 parent 4de5a80 commit d35e301
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 16 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ GOVET=$(GOCMD) vet
BINARY_NAME=crda
GITCOMMIT:=$(shell git rev-parse --short HEAD)
REPO=github.com/fabric8-analytics/cli-tools
VERSION?=0.0.1
VERSION?=0.2.2
EXPORT_RESULT?=false
CGO_ENABLED:=0
LDFLAGS +=-X ${REPO}/pkg/version.timestamp=$(shell date +%s)
Expand Down
18 changes: 9 additions & 9 deletions docs/cli_README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,28 +22,28 @@ You can manually change your preference about usage data collection by running i


### Installation:
- Select, Download and Install latest binary from [Releases](https://github.com/fabric8-analytics/cli-tools/releases)
- Select, Download and Install the latest binary from [Releases](https://github.com/fabric8-analytics/cli-tools/releases)

#### curl

- ##### For Linux
```
$ curl -s -L https://github.com/fabric8-analytics/cli-tools/releases/download/v0.2.0/crda_0.2.0_Linux_64bit.tar.gz | tar xvz -C .
$ curl -s -L https://github.com/fabric8-analytics/cli-tools/releases/download/v0.2.2/crda_0.2.2_Linux_64bit.tar.gz | tar xvz -C .
```
- ##### For Linux - Fedora/CentOS/RHEL
```
$ curl -s -L https://github.com/fabric8-analytics/cli-tools/releases/download/v0.2.0/crda_0.2.0_Linux-64bit.rpm
$ curl -s -L https://github.com/fabric8-analytics/cli-tools/releases/download/v0.2.2/crda_0.2.2_Linux-64bit.rpm
```
- ##### For MacOS
```
$ curl -s -L https://github.com/fabric8-analytics/cli-tools/releases/download/v0.2.0/crda_0.2.0_macOS_64bit.tar.gz | tar xvz -C .
$ curl -s -L https://github.com/fabric8-analytics/cli-tools/releases/download/v0.2.2/crda_0.2.2_macOS_64bit.tar.gz | tar xvz -C .
```
- ##### For MacOS - Apple Silicon
```
$ curl -s -L https://github.com/fabric8-analytics/cli-tools/releases/download/v0.2.0/crda_0.2.0_macOS_ARM64.tar.gz | tar xvz -C .
$ curl -s -L https://github.com/fabric8-analytics/cli-tools/releases/download/v0.2.2/crda_0.2.2_macOS_ARM64.tar.gz | tar xvz -C .
```
- ##### For Windows
Click [here](https://github.com/fabric8-analytics/cli-tools/releases/download/v0.2.0/crda_0.2.0_Windows_64bit.tar.gz) to start download.
Click [here](https://github.com/fabric8-analytics/cli-tools/releases/download/v0.2.2/crda_0.2.2_Windows_64bit.tar.gz) to start download.

### Usage:
Executable supports following commands:
Expand Down Expand Up @@ -81,15 +81,15 @@ Executable supports following commands:

Possible exit codes and their meaning:

- 0: success, no vulns found
- 0: success, no vulnerabilities found
- 1: failure, try to re-run command
- 2: action_needed, vulns found
- 2: action_needed, vulnerabilities found


#### Build:

```go
go build -o crda
make build
```


Expand Down
18 changes: 12 additions & 6 deletions pkg/config/viper_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,26 @@ import (
)

type viperConfigs struct {
Host string `mapstructure:"host"`
AuthToken string `mapstructure:"auth_token"`
ConsentTelemetry string `mapstructure:"consent_telemetry"`
Host string `mapstructure:"HOST" yaml:"host"`
AuthToken string `mapstructure:"AUTH_TOKEN" yaml:"auth_token"`
CrdaKey string `mapstructure:"CRDA_KEY" yaml:"crda_key"`
ConsentTelemetry string `mapstructure:"CONSENT_TELEMETRY" yaml:"consent_telemetry"`
}

// ActiveConfigValues Maintain state of viper configurations
var ActiveConfigValues = &viperConfigs{}


// ViperUnMarshal loads viper configs into ActiveConfigValues
func ViperUnMarshal() {

func ViperUnMarshal() *viperConfigs {
viper.AutomaticEnv()
// Have to bind individual variables: https://github.com/spf13/viper/issues/188
viper.BindEnv("CONSENT_TELEMETRY")
viper.BindEnv("AUTH_TOKEN")
viper.BindEnv("HOST")
viper.BindEnv("CRDA_KEY")
err := viper.Unmarshal(&ActiveConfigValues)
if err != nil {
log.Fatal().Msgf("unable to decode into struct, %v", err)
}
return ActiveConfigValues
}
25 changes: 25 additions & 0 deletions pkg/config/viper_config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package config

import (
"os"
"testing"

"github.com/google/go-cmp/cmp"
)

func TestViperUnMarshal(t *testing.T) {
os.Setenv("HOST", "host")
os.Setenv("AUTH_TOKEN", "token")
os.Setenv("CONSENT_TELEMETRY", "false")
os.Setenv("CRDA_KEY", "abc")
want := &viperConfigs{
Host: "host",
AuthToken: "token",
ConsentTelemetry: "false",
CrdaKey: "abc",
}
got := ViperUnMarshal()
if diff := cmp.Diff(want, got); diff != "" {
t.Errorf("Error in ViperUnMarshal func. (-want, +got):\n%s", diff)
}
}

0 comments on commit d35e301

Please sign in to comment.