Skip to content

Commit b885a5e

Browse files
committed
Return an error when one then more alias is provided
1 parent 33fabf4 commit b885a5e

File tree

4 files changed

+28
-13
lines changed

4 files changed

+28
-13
lines changed

internal/cmd/profile/get_alias.go

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,20 @@ import (
66
"github.com/urfave/cli/v2"
77
)
88

9-
var (
10-
profileAlias string
11-
)
9+
var profileAlias string
1210

13-
func getAlias(cliCtx *cli.Context) error {
14-
if nArgs := cliCtx.NArg(); nArgs != 1 {
15-
return fmt.Errorf("expecting profile alias as the only argument, got %d instead", nArgs)
11+
// setGlobalProfileAlias sets the global profile alias if it is provided as the only argument.
12+
// It returns false if no arguments were provided and error if there were more than one.
13+
//
14+
// If false is returned, the caller should attempt to get the profile alias on its own.
15+
func setGlobalProfileAlias(cliCtx *cli.Context) (bool, error) {
16+
switch cliCtx.NArg() {
17+
case 0:
18+
return false, nil
19+
case 1:
20+
profileAlias = cliCtx.Args().Get(0)
21+
return true, nil
22+
default:
23+
return false, fmt.Errorf("expecting profile alias as the only argument, got %d instead", cliCtx.NArg())
1624
}
17-
18-
profileAlias = cliCtx.Args().Get(0)
19-
20-
return nil
2125
}

internal/cmd/profile/get_alias_with_profile.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,12 @@ var (
1313
)
1414

1515
func getAliasWithAPITokenProfile(cliCtx *cli.Context) error {
16-
if err := getAlias(cliCtx); err == nil {
16+
ok, err := setGlobalProfileAlias(cliCtx)
17+
if err != nil {
18+
return err
19+
}
20+
21+
if ok {
1722
return nil
1823
}
1924

internal/cmd/profile/logout_command.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ func logoutCommand() *cli.Command {
99
Name: "logout",
1010
Usage: "Remove Spacelift credentials for an existing profile",
1111
ArgsUsage: "<account-alias>",
12-
Before: getAlias,
12+
Before: func(cliCtx *cli.Context) error {
13+
_, err := setGlobalProfileAlias(cliCtx)
14+
return err
15+
},
1316
Action: func(*cli.Context) error {
1417
return manager.Delete(profileAlias)
1518
},

internal/cmd/profile/select_command.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ func selectCommand() *cli.Command {
99
Name: "select",
1010
Usage: "Select one of your Spacelift account profiles",
1111
ArgsUsage: "<account-alias>",
12-
Before: getAlias,
12+
Before: func(cliCtx *cli.Context) error {
13+
_, err := setGlobalProfileAlias(cliCtx)
14+
return err
15+
},
1316
Action: func(*cli.Context) error {
1417
return manager.Select(profileAlias)
1518
},

0 commit comments

Comments
 (0)