diff --git a/internal/client/deb_client.go b/internal/client/deb_client.go index d003eba3..a1714cef 100644 --- a/internal/client/deb_client.go +++ b/internal/client/deb_client.go @@ -41,13 +41,13 @@ func NewDebClient(accessToken *string, httpClient IClient) *DebClient { if len(host) == 0 { host = DefaultDebrickedUri } - authenticator := auth.NewDebrickedAuthenticator(host) + return &DebClient{ host: &host, httpClient: httpClient, - accessToken: initAccessToken(accessToken), + accessToken: accessToken, jwtToken: "", - authenticator: authenticator, + authenticator: auth.NewDebrickedAuthenticator(host), } } @@ -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..5097ee95 100644 --- a/internal/client/deb_client_test.go +++ b/internal/client/deb_client_test.go @@ -36,37 +36,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 +184,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..d352377f 100644 --- a/internal/client/request.go +++ b/internal/client/request.go @@ -117,9 +117,10 @@ type errorMessage struct { } func (debClient *DebClient) authenticate() error { - - if debClient.accessToken != nil { - return debClient.authenticateExplicitToken() + if debClient.accessToken != nil { // To avoid segfault + if len(*debClient.accessToken) != 0 { + return debClient.authenticateExplicitToken() + } } return debClient.authenticateCachedToken() diff --git a/internal/cmd/root/root.go b/internal/cmd/root/root.go index 51a9c481..d51ad765 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,12 +30,14 @@ Complete documentation is available at https://docs.debricked.com/tools-and-inte Version: version, } viper.SetEnvPrefix("DEBRICKED") + viper.AutomaticEnv() viper.MustBindEnv(AccessTokenFlag) + rootCmd.PersistentFlags().StringVarP( &accessToken, - AccessTokenFlag, + OldAccessTokenFlag, "t", - "", + viper.GetString(AccessTokenFlag), `Debricked access token. Read more: https://docs.debricked.com/product/administration/generate-access-token`, ) diff --git a/internal/cmd/root/root_test.go b/internal/cmd/root/root_test.go index 52e32cb8..5541b033 100644 --- a/internal/cmd/root/root_test.go +++ b/internal/cmd/root/root_test.go @@ -21,7 +21,7 @@ func TestNewRootCmd(t *testing.T) { } flags := cmd.PersistentFlags() - flag := flags.Lookup(AccessTokenFlag) + flag := flags.Lookup(OldAccessTokenFlag) assert.NotNil(t, flag) assert.Equal(t, "t", flag.Shorthand) @@ -34,7 +34,7 @@ func TestNewRootCmd(t *testing.T) { break } } - assert.Truef(t, match, "failed to assert that flag was present: "+AccessTokenFlag) + assert.Truef(t, match, "failed to assert that flag was present: "+OldAccessTokenFlag) assert.Len(t, viperKeys, 22) }