From f6613dbcb9e2b5c38421d7c08a7ee7b9fe488abc Mon Sep 17 00:00:00 2001 From: filip Date: Mon, 4 Nov 2024 15:41:16 +0100 Subject: [PATCH] Let viper handle env variable for token --- internal/client/deb_client.go | 18 ++--------- internal/client/deb_client_test.go | 48 +----------------------------- internal/client/request.go | 3 +- internal/cmd/root/root.go | 9 ++++-- internal/cmd/root/root_test.go | 2 +- 5 files changed, 11 insertions(+), 69 deletions(-) diff --git a/internal/client/deb_client.go b/internal/client/deb_client.go index d003eba3..15330f3f 100644 --- a/internal/client/deb_client.go +++ b/internal/client/deb_client.go @@ -45,7 +45,7 @@ func NewDebClient(accessToken *string, httpClient IClient) *DebClient { return &DebClient{ host: &host, httpClient: httpClient, - accessToken: initAccessToken(accessToken), + accessToken: accessToken, jwtToken: "", authenticator: authenticator, } @@ -68,27 +68,13 @@ func (debClient *DebClient) Get(uri string, format string) (*http.Response, erro } func (debClient *DebClient) SetAccessToken(accessToken *string) { - debClient.accessToken = initAccessToken(accessToken) + debClient.accessToken = accessToken } func (debClient *DebClient) Authenticator() auth.IAuthenticator { return debClient.authenticator } -func initAccessToken(accessToken *string) *string { - if accessToken == nil { - accessToken = new(string) - } - if len(*accessToken) == 0 { - ok := false - *accessToken, ok = os.LookupEnv("DEBRICKED_TOKEN") - if !ok { - accessToken = nil - } - } - return accessToken -} - type BillingPlan struct { SCA string `json:"sca"` Select string `json:"select"` diff --git a/internal/client/deb_client_test.go b/internal/client/deb_client_test.go index 99e350ca..d1dc0ab0 100644 --- a/internal/client/deb_client_test.go +++ b/internal/client/deb_client_test.go @@ -38,35 +38,6 @@ func TestNewDebClientWithNilToken(t *testing.T) { const debrickedTknEnvVar = "DEBRICKED_TOKEN" -func TestNewDebClientWithTokenEnvVariable(t *testing.T) { - envVarTkn := "env-tkn" - oldEnvValue, ok := os.LookupEnv(debrickedTknEnvVar) - err := os.Setenv(debrickedTknEnvVar, "env-tkn") - if err != nil { - t.Fatalf("failed to set env var %s", debrickedTknEnvVar) - } - defer func(key, value string, ok bool) { - var err error = nil - if ok { - err = os.Setenv(key, value) - } else { - err = os.Unsetenv(key) - } - if err != nil { - t.Fatalf("failed to reset env var %s", debrickedTknEnvVar) - } - }(debrickedTknEnvVar, oldEnvValue, ok) - - accessToken := "" - debClient := NewDebClient(&accessToken, nil) - if *debClient.host != DefaultDebrickedUri { - t.Error("failed to assert that host was set properly") - } - if *debClient.accessToken != envVarTkn { - t.Errorf("failed to assert that access token was set to %s. Got %s", envVarTkn, *debClient.accessToken) - } -} - func TestNewDebClientWithWithURI(t *testing.T) { accessToken := "" os.Setenv("DEBRICKED_URI", "https://subdomain.debricked.com") @@ -215,24 +186,7 @@ func TestAuthenticateCachedToken(t *testing.T) { authenticator: testdataAuth.MockAuthenticator{}, } - oldEnvValue, ok := os.LookupEnv(debrickedTknEnvVar) - err := os.Unsetenv(debrickedTknEnvVar) - if err != nil { - t.Fatalf("failed to set env var %s", debrickedTknEnvVar) - } - defer func(key, value string, ok bool) { - var err error = nil - if ok { - err = os.Setenv(key, value) - } else { - err = os.Unsetenv(key) - } - if err != nil { - t.Fatalf("failed to reset env var %s", debrickedTknEnvVar) - } - }(debrickedTknEnvVar, oldEnvValue, ok) - - err = client.authenticate() + err := client.authenticate() if err != nil { t.Fatal("failed to assert that no error occurred") } diff --git a/internal/client/request.go b/internal/client/request.go index f87d0309..e91df427 100644 --- a/internal/client/request.go +++ b/internal/client/request.go @@ -117,8 +117,7 @@ type errorMessage struct { } func (debClient *DebClient) authenticate() error { - - if debClient.accessToken != nil { + if len(*debClient.accessToken) != 0 { return debClient.authenticateExplicitToken() } diff --git a/internal/cmd/root/root.go b/internal/cmd/root/root.go index 51a9c481..0ace847b 100644 --- a/internal/cmd/root/root.go +++ b/internal/cmd/root/root.go @@ -15,7 +15,8 @@ import ( var accessToken string -const AccessTokenFlag = "access-token" +const AccessTokenFlag = "token" +const OldAccessTokenFlag = "access-token" func NewRootCmd(version string, container *wire.CliContainer) *cobra.Command { rootCmd := &cobra.Command{ @@ -29,15 +30,17 @@ Complete documentation is available at https://docs.debricked.com/tools-and-inte Version: version, } viper.SetEnvPrefix("DEBRICKED") - viper.MustBindEnv(AccessTokenFlag) + viper.AutomaticEnv() rootCmd.PersistentFlags().StringVarP( &accessToken, AccessTokenFlag, "t", - "", + viper.GetString(AccessTokenFlag), `Debricked access token. Read more: https://docs.debricked.com/product/administration/generate-access-token`, ) + viper.MustBindEnv(AccessTokenFlag) + viper.RegisterAlias(OldAccessTokenFlag, AccessTokenFlag) // To avoid breaking change var debClient = container.DebClient() debClient.SetAccessToken(&accessToken) diff --git a/internal/cmd/root/root_test.go b/internal/cmd/root/root_test.go index 52e32cb8..0fd0cff4 100644 --- a/internal/cmd/root/root_test.go +++ b/internal/cmd/root/root_test.go @@ -35,7 +35,7 @@ func TestNewRootCmd(t *testing.T) { } } assert.Truef(t, match, "failed to assert that flag was present: "+AccessTokenFlag) - assert.Len(t, viperKeys, 22) + assert.Len(t, viperKeys, 23) } func TestPreRun(t *testing.T) {