diff --git a/general/login/cli.go b/general/login/cli.go index 437142240..55b1f59eb 100644 --- a/general/login/cli.go +++ b/general/login/cli.go @@ -10,5 +10,5 @@ func LoginCmd(c *cli.Context) error { if c.NArg() > 0 { return cliutils.WrongNumberOfArgumentsHandler(c) } - return coreLogin.NewLoginCommand().Run() + return coreLogin.NewLoginCommand().SetServerId(c.String("server-id")).Run() } diff --git a/general/login/cli_test.go b/general/login/cli_test.go new file mode 100644 index 000000000..90a0252ac --- /dev/null +++ b/general/login/cli_test.go @@ -0,0 +1,54 @@ +package login + +import ( + "flag" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/urfave/cli" +) + +func TestLoginCmdRejectsExtraArguments(t *testing.T) { + app := cli.NewApp() + app.Commands = []cli.Command{ + { + Name: "login", + Action: LoginCmd, + }, + } + err := app.Run([]string{"jf", "login", "extra-arg"}) + assert.Error(t, err) +} + +func TestLoginCmdPassesServerIdFlag(t *testing.T) { + const testServerId = "my-test-server" + var capturedServerId string + + app := cli.NewApp() + app.Commands = []cli.Command{ + { + Name: "login", + Flags: []cli.Flag{ + cli.StringFlag{Name: "server-id"}, + }, + Action: func(c *cli.Context) error { + capturedServerId = c.String("server-id") + // Return early without running the actual login command. + return nil + }, + }, + } + err := app.Run([]string{"jf", "login", "--server-id", testServerId}) + assert.NoError(t, err) + assert.Equal(t, testServerId, capturedServerId) +} + +func TestLoginCmdNoArgsCallsLoginWithEmptyServerId(t *testing.T) { + set := flag.NewFlagSet("test", 0) + set.String("server-id", "", "") + c := cli.NewContext(nil, set, nil) + + // Verify that the context has no arguments and server-id is empty. + assert.Equal(t, 0, c.NArg()) + assert.Equal(t, "", c.String("server-id")) +} diff --git a/main.go b/main.go index 845ad694d..e66b3e2b1 100644 --- a/main.go +++ b/main.go @@ -285,6 +285,7 @@ func getCommands() ([]cli.Command, error) { Usage: loginDocs.GetDescription(), HelpName: corecommon.CreateUsage("login", loginDocs.GetDescription(), loginDocs.Usage), BashComplete: corecommon.CreateBashCompletionFunc(), + Flags: cliutils.GetCommandFlags(cliutils.Login), Category: otherCategory, Action: login.LoginCmd, }, diff --git a/utils/cliutils/commandsflags.go b/utils/cliutils/commandsflags.go index bd0e845fa..f89181ce7 100644 --- a/utils/cliutils/commandsflags.go +++ b/utils/cliutils/commandsflags.go @@ -128,6 +128,9 @@ const ( AccessTokenCreate = "access-token-create" ExchangeOidcToken = "exchange-oidc-token" + // Login command key + Login = "login" + // *** Artifactory Commands' flags *** // Base flags url = "url" @@ -2140,6 +2143,9 @@ var commandFlags = map[string][]string{ Setup: { serverId, url, user, password, accessToken, sshPassphrase, sshKeyPath, ClientCertPath, ClientCertKeyPath, Project, setupRepo, }, + Login: { + serverId, + }, } func GetCommandFlags(cmd string) []cli.Flag { diff --git a/utils/cliutils/utils_test.go b/utils/cliutils/utils_test.go index f1a17a560..b8579c5db 100644 --- a/utils/cliutils/utils_test.go +++ b/utils/cliutils/utils_test.go @@ -429,3 +429,14 @@ func TestSettingCIFlagRemovesSurvey(t *testing.T) { shouldHide := ShouldHideSurveyLink() assert.True(t, shouldHide, "Expected survey to be hidden when CI flag is set") } + +func TestLoginCommandFlagsIncludeServerId(t *testing.T) { + flags := GetCommandFlags(Login) + assert.NotEmpty(t, flags, "Expected login command to have flags") + + var flagNames []string + for _, f := range flags { + flagNames = append(flagNames, f.GetName()) + } + assert.Contains(t, flagNames, "server-id", "Expected login command flags to include 'server-id'") +}