From 4fbcd54ab9c58efc83bbfbf39d36494900d476a0 Mon Sep 17 00:00:00 2001 From: Jose Antonio Insua Date: Thu, 26 Feb 2026 13:03:41 +0100 Subject: [PATCH] Deduplicate token type resolution and use pretty-print in admin commands Extract shared resolveToken() helper into config.go to replace duplicate switch blocks in validate.go and invalidate.go. Use output.PrintPrettyJSON in admin profile and organizations commands for consistent JSON output. --- cmd/admin/organizations.go | 3 ++- cmd/admin/profile.go | 3 ++- ims/config.go | 20 ++++++++++++++++++++ ims/invalidate.go | 19 ++----------------- ims/validate.go | 19 ++----------------- 5 files changed, 28 insertions(+), 36 deletions(-) diff --git a/cmd/admin/organizations.go b/cmd/admin/organizations.go index 3f9ced8..53c86cf 100644 --- a/cmd/admin/organizations.go +++ b/cmd/admin/organizations.go @@ -14,6 +14,7 @@ import ( "fmt" "github.com/adobe/imscli/ims" + "github.com/adobe/imscli/output" "github.com/spf13/cobra" ) @@ -32,7 +33,7 @@ func OrganizationsCmd(imsConfig *ims.Config) *cobra.Command { if err != nil { return fmt.Errorf("error in get admin organizations cmd: %w", err) } - fmt.Println(resp) + output.PrintPrettyJSON(resp) return nil }, } diff --git a/cmd/admin/profile.go b/cmd/admin/profile.go index 1d816a4..b285447 100644 --- a/cmd/admin/profile.go +++ b/cmd/admin/profile.go @@ -14,6 +14,7 @@ import ( "fmt" "github.com/adobe/imscli/ims" + "github.com/adobe/imscli/output" "github.com/spf13/cobra" ) @@ -30,7 +31,7 @@ func ProfileCmd(imsConfig *ims.Config) *cobra.Command { if err != nil { return fmt.Errorf("error in get admin profile cmd: %w", err) } - fmt.Println(resp) + output.PrintPrettyJSON(resp) return nil }, } diff --git a/ims/config.go b/ims/config.go index b54bff0..87ff7c8 100644 --- a/ims/config.go +++ b/ims/config.go @@ -11,7 +11,10 @@ package ims import ( + "fmt" "net/url" + + "github.com/adobe/ims-go/ims" ) type Config struct { @@ -61,6 +64,23 @@ type RefreshInfo struct { RefreshToken string } +func (i Config) resolveToken() (string, ims.TokenType, error) { + switch { + case i.AccessToken != "": + return i.AccessToken, ims.AccessToken, nil + case i.RefreshToken != "": + return i.RefreshToken, ims.RefreshToken, nil + case i.DeviceToken != "": + return i.DeviceToken, ims.DeviceToken, nil + case i.ServiceToken != "": + return i.ServiceToken, ims.ServiceToken, nil + case i.AuthorizationCode != "": + return i.AuthorizationCode, ims.AuthorizationCode, nil + default: + return "", "", fmt.Errorf("no token provided") + } +} + func validateURL(u string) bool { parsedURL, err := url.Parse(u) if err != nil { diff --git a/ims/invalidate.go b/ims/invalidate.go index 52c040d..a3f3b5e 100644 --- a/ims/invalidate.go +++ b/ims/invalidate.go @@ -68,23 +68,8 @@ func (i Config) InvalidateToken() error { return fmt.Errorf("create client: %w", err) } - var token string - var tokenType ims.TokenType - - switch { - case i.AccessToken != "": - token = i.AccessToken - tokenType = ims.AccessToken - case i.RefreshToken != "": - token = i.RefreshToken - tokenType = ims.RefreshToken - case i.DeviceToken != "": - token = i.DeviceToken - tokenType = ims.DeviceToken - case i.ServiceToken != "": - token = i.ServiceToken - tokenType = ims.ServiceToken - default: + token, tokenType, err := i.resolveToken() + if err != nil { return fmt.Errorf("unexpected error, broken parameter validation") } diff --git a/ims/validate.go b/ims/validate.go index e28b17e..2278760 100644 --- a/ims/validate.go +++ b/ims/validate.go @@ -69,23 +69,8 @@ func (i Config) ValidateToken() (TokenInfo, error) { return TokenInfo{}, fmt.Errorf("create client: %w", err) } - var token string - var tokenType ims.TokenType - - switch { - case i.AccessToken != "": - token = i.AccessToken - tokenType = ims.AccessToken - case i.RefreshToken != "": - token = i.RefreshToken - tokenType = ims.RefreshToken - case i.DeviceToken != "": - token = i.DeviceToken - tokenType = ims.DeviceToken - case i.AuthorizationCode != "": - token = i.AuthorizationCode - tokenType = ims.AuthorizationCode - default: + token, tokenType, err := i.resolveToken() + if err != nil { return TokenInfo{}, fmt.Errorf("unexpected error, broken validation") }