Skip to content

Commit d35e301

Browse files
author
Deepak Sharma
authored
fix: unable to bind env variables (#60)
* fix: binding env variables * fix: binding env variables
1 parent 4de5a80 commit d35e301

File tree

4 files changed

+47
-16
lines changed

4 files changed

+47
-16
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ GOVET=$(GOCMD) vet
44
BINARY_NAME=crda
55
GITCOMMIT:=$(shell git rev-parse --short HEAD)
66
REPO=github.com/fabric8-analytics/cli-tools
7-
VERSION?=0.0.1
7+
VERSION?=0.2.2
88
EXPORT_RESULT?=false
99
CGO_ENABLED:=0
1010
LDFLAGS +=-X ${REPO}/pkg/version.timestamp=$(shell date +%s)

docs/cli_README.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,28 +22,28 @@ You can manually change your preference about usage data collection by running i
2222

2323

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

2727
#### curl
2828

2929
- ##### For Linux
3030
```
31-
$ 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 .
31+
$ 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 .
3232
```
3333
- ##### For Linux - Fedora/CentOS/RHEL
3434
```
35-
$ curl -s -L https://github.com/fabric8-analytics/cli-tools/releases/download/v0.2.0/crda_0.2.0_Linux-64bit.rpm
35+
$ curl -s -L https://github.com/fabric8-analytics/cli-tools/releases/download/v0.2.2/crda_0.2.2_Linux-64bit.rpm
3636
```
3737
- ##### For MacOS
3838
```
39-
$ 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 .
39+
$ 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 .
4040
```
4141
- ##### For MacOS - Apple Silicon
4242
```
43-
$ 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 .
43+
$ 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 .
4444
```
4545
- ##### For Windows
46-
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.
46+
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.
4747

4848
### Usage:
4949
Executable supports following commands:
@@ -81,15 +81,15 @@ Executable supports following commands:
8181

8282
Possible exit codes and their meaning:
8383

84-
- 0: success, no vulns found
84+
- 0: success, no vulnerabilities found
8585
- 1: failure, try to re-run command
86-
- 2: action_needed, vulns found
86+
- 2: action_needed, vulnerabilities found
8787

8888

8989
#### Build:
9090

9191
```go
92-
go build -o crda
92+
make build
9393
```
9494

9595

pkg/config/viper_config.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,26 @@ import (
66
)
77

88
type viperConfigs struct {
9-
Host string `mapstructure:"host"`
10-
AuthToken string `mapstructure:"auth_token"`
11-
ConsentTelemetry string `mapstructure:"consent_telemetry"`
9+
Host string `mapstructure:"HOST" yaml:"host"`
10+
AuthToken string `mapstructure:"AUTH_TOKEN" yaml:"auth_token"`
11+
CrdaKey string `mapstructure:"CRDA_KEY" yaml:"crda_key"`
12+
ConsentTelemetry string `mapstructure:"CONSENT_TELEMETRY" yaml:"consent_telemetry"`
1213
}
1314

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

17-
1818
// ViperUnMarshal loads viper configs into ActiveConfigValues
19-
func ViperUnMarshal() {
20-
19+
func ViperUnMarshal() *viperConfigs {
20+
viper.AutomaticEnv()
21+
// Have to bind individual variables: https://github.com/spf13/viper/issues/188
22+
viper.BindEnv("CONSENT_TELEMETRY")
23+
viper.BindEnv("AUTH_TOKEN")
24+
viper.BindEnv("HOST")
25+
viper.BindEnv("CRDA_KEY")
2126
err := viper.Unmarshal(&ActiveConfigValues)
2227
if err != nil {
2328
log.Fatal().Msgf("unable to decode into struct, %v", err)
2429
}
30+
return ActiveConfigValues
2531
}

pkg/config/viper_config_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package config
2+
3+
import (
4+
"os"
5+
"testing"
6+
7+
"github.com/google/go-cmp/cmp"
8+
)
9+
10+
func TestViperUnMarshal(t *testing.T) {
11+
os.Setenv("HOST", "host")
12+
os.Setenv("AUTH_TOKEN", "token")
13+
os.Setenv("CONSENT_TELEMETRY", "false")
14+
os.Setenv("CRDA_KEY", "abc")
15+
want := &viperConfigs{
16+
Host: "host",
17+
AuthToken: "token",
18+
ConsentTelemetry: "false",
19+
CrdaKey: "abc",
20+
}
21+
got := ViperUnMarshal()
22+
if diff := cmp.Diff(want, got); diff != "" {
23+
t.Errorf("Error in ViperUnMarshal func. (-want, +got):\n%s", diff)
24+
}
25+
}

0 commit comments

Comments
 (0)