Skip to content

Commit

Permalink
Return an error when one then more alias is provided (#267)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasmik authored Nov 11, 2024
1 parent 33fabf4 commit d3f47f1
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 13 deletions.
24 changes: 14 additions & 10 deletions internal/cmd/profile/get_alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
7 changes: 6 additions & 1 deletion internal/cmd/profile/get_alias_with_profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
5 changes: 4 additions & 1 deletion internal/cmd/profile/logout_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ func logoutCommand() *cli.Command {
Name: "logout",
Usage: "Remove Spacelift credentials for an existing profile",
ArgsUsage: "<account-alias>",
Before: getAlias,
Before: func(cliCtx *cli.Context) error {
_, err := setGlobalProfileAlias(cliCtx)
return err
},
Action: func(*cli.Context) error {
return manager.Delete(profileAlias)
},
Expand Down
5 changes: 4 additions & 1 deletion internal/cmd/profile/select_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ func selectCommand() *cli.Command {
Name: "select",
Usage: "Select one of your Spacelift account profiles",
ArgsUsage: "<account-alias>",
Before: getAlias,
Before: func(cliCtx *cli.Context) error {
_, err := setGlobalProfileAlias(cliCtx)
return err
},
Action: func(*cli.Context) error {
return manager.Select(profileAlias)
},
Expand Down

0 comments on commit d3f47f1

Please sign in to comment.