Skip to content

Commit 2c66d3e

Browse files
committed
Let viper handle env variable for token
1 parent 2506177 commit 2c66d3e

File tree

5 files changed

+17
-75
lines changed

5 files changed

+17
-75
lines changed

internal/client/deb_client.go

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,13 @@ func NewDebClient(accessToken *string, httpClient IClient) *DebClient {
4141
if len(host) == 0 {
4242
host = DefaultDebrickedUri
4343
}
44-
authenticator := auth.NewDebrickedAuthenticator(host)
44+
4545
return &DebClient{
4646
host: &host,
4747
httpClient: httpClient,
48-
accessToken: initAccessToken(accessToken),
48+
accessToken: accessToken,
4949
jwtToken: "",
50-
authenticator: authenticator,
50+
authenticator: auth.NewDebrickedAuthenticator(host),
5151
}
5252
}
5353

@@ -68,27 +68,13 @@ func (debClient *DebClient) Get(uri string, format string) (*http.Response, erro
6868
}
6969

7070
func (debClient *DebClient) SetAccessToken(accessToken *string) {
71-
debClient.accessToken = initAccessToken(accessToken)
71+
debClient.accessToken = accessToken
7272
}
7373

7474
func (debClient *DebClient) Authenticator() auth.IAuthenticator {
7575
return debClient.authenticator
7676
}
7777

78-
func initAccessToken(accessToken *string) *string {
79-
if accessToken == nil {
80-
accessToken = new(string)
81-
}
82-
if len(*accessToken) == 0 {
83-
ok := false
84-
*accessToken, ok = os.LookupEnv("DEBRICKED_TOKEN")
85-
if !ok {
86-
accessToken = nil
87-
}
88-
}
89-
return accessToken
90-
}
91-
9278
type BillingPlan struct {
9379
SCA string `json:"sca"`
9480
Select string `json:"select"`

internal/client/deb_client_test.go

Lines changed: 1 addition & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -36,37 +36,6 @@ func TestNewDebClientWithNilToken(t *testing.T) {
3636
}
3737
}
3838

39-
const debrickedTknEnvVar = "DEBRICKED_TOKEN"
40-
41-
func TestNewDebClientWithTokenEnvVariable(t *testing.T) {
42-
envVarTkn := "env-tkn"
43-
oldEnvValue, ok := os.LookupEnv(debrickedTknEnvVar)
44-
err := os.Setenv(debrickedTknEnvVar, "env-tkn")
45-
if err != nil {
46-
t.Fatalf("failed to set env var %s", debrickedTknEnvVar)
47-
}
48-
defer func(key, value string, ok bool) {
49-
var err error = nil
50-
if ok {
51-
err = os.Setenv(key, value)
52-
} else {
53-
err = os.Unsetenv(key)
54-
}
55-
if err != nil {
56-
t.Fatalf("failed to reset env var %s", debrickedTknEnvVar)
57-
}
58-
}(debrickedTknEnvVar, oldEnvValue, ok)
59-
60-
accessToken := ""
61-
debClient := NewDebClient(&accessToken, nil)
62-
if *debClient.host != DefaultDebrickedUri {
63-
t.Error("failed to assert that host was set properly")
64-
}
65-
if *debClient.accessToken != envVarTkn {
66-
t.Errorf("failed to assert that access token was set to %s. Got %s", envVarTkn, *debClient.accessToken)
67-
}
68-
}
69-
7039
func TestNewDebClientWithWithURI(t *testing.T) {
7140
accessToken := ""
7241
os.Setenv("DEBRICKED_URI", "https://subdomain.debricked.com")
@@ -215,24 +184,7 @@ func TestAuthenticateCachedToken(t *testing.T) {
215184
authenticator: testdataAuth.MockAuthenticator{},
216185
}
217186

218-
oldEnvValue, ok := os.LookupEnv(debrickedTknEnvVar)
219-
err := os.Unsetenv(debrickedTknEnvVar)
220-
if err != nil {
221-
t.Fatalf("failed to set env var %s", debrickedTknEnvVar)
222-
}
223-
defer func(key, value string, ok bool) {
224-
var err error = nil
225-
if ok {
226-
err = os.Setenv(key, value)
227-
} else {
228-
err = os.Unsetenv(key)
229-
}
230-
if err != nil {
231-
t.Fatalf("failed to reset env var %s", debrickedTknEnvVar)
232-
}
233-
}(debrickedTknEnvVar, oldEnvValue, ok)
234-
235-
err = client.authenticate()
187+
err := client.authenticate()
236188
if err != nil {
237189
t.Fatal("failed to assert that no error occurred")
238190
}

internal/client/request.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,10 @@ type errorMessage struct {
117117
}
118118

119119
func (debClient *DebClient) authenticate() error {
120-
121-
if debClient.accessToken != nil {
122-
return debClient.authenticateExplicitToken()
120+
if debClient.accessToken != nil { // To avoid segfault
121+
if len(*debClient.accessToken) != 0 {
122+
return debClient.authenticateExplicitToken()
123+
}
123124
}
124125

125126
return debClient.authenticateCachedToken()

internal/cmd/root/root.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ import (
1515

1616
var accessToken string
1717

18-
const AccessTokenFlag = "access-token"
18+
const AccessTokenFlag = "token"
19+
const OldAccessTokenFlag = "access-token"
1920

2021
func NewRootCmd(version string, container *wire.CliContainer) *cobra.Command {
2122
rootCmd := &cobra.Command{
@@ -29,12 +30,14 @@ Complete documentation is available at https://docs.debricked.com/tools-and-inte
2930
Version: version,
3031
}
3132
viper.SetEnvPrefix("DEBRICKED")
33+
viper.AutomaticEnv()
3234
viper.MustBindEnv(AccessTokenFlag)
35+
3336
rootCmd.PersistentFlags().StringVarP(
3437
&accessToken,
35-
AccessTokenFlag,
38+
OldAccessTokenFlag,
3639
"t",
37-
"",
40+
viper.GetString(AccessTokenFlag),
3841
`Debricked access token.
3942
Read more: https://docs.debricked.com/product/administration/generate-access-token`,
4043
)

internal/cmd/root/root_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func TestNewRootCmd(t *testing.T) {
2121
}
2222

2323
flags := cmd.PersistentFlags()
24-
flag := flags.Lookup(AccessTokenFlag)
24+
flag := flags.Lookup(OldAccessTokenFlag)
2525
assert.NotNil(t, flag)
2626
assert.Equal(t, "t", flag.Shorthand)
2727

@@ -34,7 +34,7 @@ func TestNewRootCmd(t *testing.T) {
3434
break
3535
}
3636
}
37-
assert.Truef(t, match, "failed to assert that flag was present: "+AccessTokenFlag)
37+
assert.Truef(t, match, "failed to assert that flag was present: "+OldAccessTokenFlag)
3838
assert.Len(t, viperKeys, 22)
3939
}
4040

0 commit comments

Comments
 (0)