From d3f47f1944f0c170eb6255eb82dd65088aa84198 Mon Sep 17 00:00:00 2001 From: Tomas <40318863+tomasmik@users.noreply.github.com> Date: Mon, 11 Nov 2024 09:47:08 +0200 Subject: [PATCH] Return an error when one then more alias is provided (#267) --- internal/cmd/profile/get_alias.go | 24 +++++++++++-------- .../cmd/profile/get_alias_with_profile.go | 7 +++++- internal/cmd/profile/logout_command.go | 5 +++- internal/cmd/profile/select_command.go | 5 +++- 4 files changed, 28 insertions(+), 13 deletions(-) diff --git a/internal/cmd/profile/get_alias.go b/internal/cmd/profile/get_alias.go index d5d0a8e..64ef6e5 100644 --- a/internal/cmd/profile/get_alias.go +++ b/internal/cmd/profile/get_alias.go @@ -6,16 +6,20 @@ import ( "github.com/urfave/cli/v2" ) -var ( - profileAlias string -) +var profileAlias string -func getAlias(cliCtx *cli.Context) error { - if nArgs := cliCtx.NArg(); nArgs != 1 { - return fmt.Errorf("expecting profile alias as the only argument, got %d instead", nArgs) +// setGlobalProfileAlias sets the global profile alias if it is provided as the only argument. +// It returns false if no arguments were provided and error if there were more than one. +// +// If false is returned, the caller should attempt to get the profile alias on its own. +func setGlobalProfileAlias(cliCtx *cli.Context) (bool, error) { + switch cliCtx.NArg() { + case 0: + return false, nil + case 1: + profileAlias = cliCtx.Args().Get(0) + return true, nil + default: + return false, fmt.Errorf("expecting profile alias as the only argument, got %d instead", cliCtx.NArg()) } - - profileAlias = cliCtx.Args().Get(0) - - return nil } diff --git a/internal/cmd/profile/get_alias_with_profile.go b/internal/cmd/profile/get_alias_with_profile.go index bbc6f7d..b551a8c 100644 --- a/internal/cmd/profile/get_alias_with_profile.go +++ b/internal/cmd/profile/get_alias_with_profile.go @@ -13,7 +13,12 @@ var ( ) func getAliasWithAPITokenProfile(cliCtx *cli.Context) error { - if err := getAlias(cliCtx); err == nil { + ok, err := setGlobalProfileAlias(cliCtx) + if err != nil { + return err + } + + if ok { return nil } diff --git a/internal/cmd/profile/logout_command.go b/internal/cmd/profile/logout_command.go index 12ea4a5..f94c263 100644 --- a/internal/cmd/profile/logout_command.go +++ b/internal/cmd/profile/logout_command.go @@ -9,7 +9,10 @@ func logoutCommand() *cli.Command { Name: "logout", Usage: "Remove Spacelift credentials for an existing profile", ArgsUsage: "", - Before: getAlias, + Before: func(cliCtx *cli.Context) error { + _, err := setGlobalProfileAlias(cliCtx) + return err + }, Action: func(*cli.Context) error { return manager.Delete(profileAlias) }, diff --git a/internal/cmd/profile/select_command.go b/internal/cmd/profile/select_command.go index a05370a..59e67ed 100644 --- a/internal/cmd/profile/select_command.go +++ b/internal/cmd/profile/select_command.go @@ -9,7 +9,10 @@ func selectCommand() *cli.Command { Name: "select", Usage: "Select one of your Spacelift account profiles", ArgsUsage: "", - Before: getAlias, + Before: func(cliCtx *cli.Context) error { + _, err := setGlobalProfileAlias(cliCtx) + return err + }, Action: func(*cli.Context) error { return manager.Select(profileAlias) },