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/go.mod b/go.mod index 78816b499..c673ad0e3 100644 --- a/go.mod +++ b/go.mod @@ -20,7 +20,7 @@ require ( github.com/jfrog/gofrog v1.7.6 github.com/jfrog/jfrog-cli-application v1.0.2-0.20260405065840-c930d515ef34 github.com/jfrog/jfrog-cli-artifactory v0.8.1-0.20260421101844-c42ed5f491fa - github.com/jfrog/jfrog-cli-core/v2 v2.60.1-0.20260420010255-65b7a8d432af + github.com/jfrog/jfrog-cli-core/v2 v2.60.1-0.20260427010241-873f53d940b3 github.com/jfrog/jfrog-cli-evidence v0.9.2 github.com/jfrog/jfrog-cli-platform-services v1.10.1-0.20260421062714-60f8615a2c6c github.com/jfrog/jfrog-cli-security v1.27.0 diff --git a/go.sum b/go.sum index 6655ac6aa..085931547 100644 --- a/go.sum +++ b/go.sum @@ -420,8 +420,8 @@ github.com/jfrog/jfrog-cli-application v1.0.2-0.20260405065840-c930d515ef34 h1:q github.com/jfrog/jfrog-cli-application v1.0.2-0.20260405065840-c930d515ef34/go.mod h1:xum2HquWO5uExa/A7MQs3TgJJVEeoqTR+6Z4mfBr1Xw= github.com/jfrog/jfrog-cli-artifactory v0.8.1-0.20260421101844-c42ed5f491fa h1:TYcV+5qmFF1x8+jNpklseXQnKc4K5xh6XminwFhZbTo= github.com/jfrog/jfrog-cli-artifactory v0.8.1-0.20260421101844-c42ed5f491fa/go.mod h1:6QJFQvde/CLnFeIIFOvm/6QuQr8OT1QWiTJAkQ+1Mnc= -github.com/jfrog/jfrog-cli-core/v2 v2.60.1-0.20260420010255-65b7a8d432af h1:TGYTFW5egYMD1MhLtuLbYwB2vn+KRlVZLk+9uLVjKyM= -github.com/jfrog/jfrog-cli-core/v2 v2.60.1-0.20260420010255-65b7a8d432af/go.mod h1:qpD7einonjqskDTEyqeG3NzAbZO6se0s0Pet0ObBQ3I= +github.com/jfrog/jfrog-cli-core/v2 v2.60.1-0.20260427010241-873f53d940b3 h1:LdLQQmhOMUfU+3x7wbtB7kY/Dd2LXKHz7CCUpHWn7uM= +github.com/jfrog/jfrog-cli-core/v2 v2.60.1-0.20260427010241-873f53d940b3/go.mod h1:qpD7einonjqskDTEyqeG3NzAbZO6se0s0Pet0ObBQ3I= github.com/jfrog/jfrog-cli-evidence v0.9.2 h1:huiBzQSI9z3OF3l2RphthdXl1aH9zBsvAt+zLsApORI= github.com/jfrog/jfrog-cli-evidence v0.9.2/go.mod h1:R9faPfyQESBmKrdZCmHvlpmYSHmffswjNnFeT3RMq8I= github.com/jfrog/jfrog-cli-platform-services v1.10.1-0.20260421062714-60f8615a2c6c h1:FmKtkKwf9LPXwikjCmX5ofJ1qg4T7hfsg3v00QDvaUY= diff --git a/main.go b/main.go index 2aa107b0c..85be3bb99 100644 --- a/main.go +++ b/main.go @@ -300,6 +300,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 9fd46d740..299b9c36e 100644 --- a/utils/cliutils/commandsflags.go +++ b/utils/cliutils/commandsflags.go @@ -130,6 +130,9 @@ const ( ExchangeOidcToken = "exchange-oidc-token" Api = "api" + // Login command key + Login = "login" + // *** Artifactory Commands' flags *** // Base flags url = "url" @@ -2183,6 +2186,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 335bc27e0..4b42686df 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'") +}