Skip to content

Commit 836859f

Browse files
committed
feat: add server id to login command
this allows scripting the login without the modal prompt
1 parent 2233717 commit 836859f

5 files changed

Lines changed: 73 additions & 1 deletion

File tree

general/login/cli.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@ func LoginCmd(c *cli.Context) error {
1010
if c.NArg() > 0 {
1111
return cliutils.WrongNumberOfArgumentsHandler(c)
1212
}
13-
return coreLogin.NewLoginCommand().Run()
13+
return coreLogin.NewLoginCommand().SetServerId(c.String("server-id")).Run()
1414
}

general/login/cli_test.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package login
2+
3+
import (
4+
"flag"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
"github.com/urfave/cli"
9+
)
10+
11+
func TestLoginCmdRejectsExtraArguments(t *testing.T) {
12+
app := cli.NewApp()
13+
app.Commands = []cli.Command{
14+
{
15+
Name: "login",
16+
Action: LoginCmd,
17+
},
18+
}
19+
err := app.Run([]string{"jf", "login", "extra-arg"})
20+
assert.Error(t, err)
21+
}
22+
23+
func TestLoginCmdPassesServerIdFlag(t *testing.T) {
24+
const testServerId = "my-test-server"
25+
var capturedServerId string
26+
27+
app := cli.NewApp()
28+
app.Commands = []cli.Command{
29+
{
30+
Name: "login",
31+
Flags: []cli.Flag{
32+
cli.StringFlag{Name: "server-id"},
33+
},
34+
Action: func(c *cli.Context) error {
35+
capturedServerId = c.String("server-id")
36+
// Return early without running the actual login command.
37+
return nil
38+
},
39+
},
40+
}
41+
err := app.Run([]string{"jf", "login", "--server-id", testServerId})
42+
assert.NoError(t, err)
43+
assert.Equal(t, testServerId, capturedServerId)
44+
}
45+
46+
func TestLoginCmdNoArgsCallsLoginWithEmptyServerId(t *testing.T) {
47+
set := flag.NewFlagSet("test", 0)
48+
set.String("server-id", "", "")
49+
c := cli.NewContext(nil, set, nil)
50+
51+
// Verify that the context has no arguments and server-id is empty.
52+
assert.Equal(t, 0, c.NArg())
53+
assert.Equal(t, "", c.String("server-id"))
54+
}

main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,7 @@ func getCommands() ([]cli.Command, error) {
285285
Usage: loginDocs.GetDescription(),
286286
HelpName: corecommon.CreateUsage("login", loginDocs.GetDescription(), loginDocs.Usage),
287287
BashComplete: corecommon.CreateBashCompletionFunc(),
288+
Flags: cliutils.GetCommandFlags(cliutils.Login),
288289
Category: otherCategory,
289290
Action: login.LoginCmd,
290291
},

utils/cliutils/commandsflags.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,9 @@ const (
128128
AccessTokenCreate = "access-token-create"
129129
ExchangeOidcToken = "exchange-oidc-token"
130130

131+
// Login command key
132+
Login = "login"
133+
131134
// *** Artifactory Commands' flags ***
132135
// Base flags
133136
url = "url"
@@ -2140,6 +2143,9 @@ var commandFlags = map[string][]string{
21402143
Setup: {
21412144
serverId, url, user, password, accessToken, sshPassphrase, sshKeyPath, ClientCertPath, ClientCertKeyPath, Project, setupRepo,
21422145
},
2146+
Login: {
2147+
serverId,
2148+
},
21432149
}
21442150

21452151
func GetCommandFlags(cmd string) []cli.Flag {

utils/cliutils/utils_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,3 +429,14 @@ func TestSettingCIFlagRemovesSurvey(t *testing.T) {
429429
shouldHide := ShouldHideSurveyLink()
430430
assert.True(t, shouldHide, "Expected survey to be hidden when CI flag is set")
431431
}
432+
433+
func TestLoginCommandFlagsIncludeServerId(t *testing.T) {
434+
flags := GetCommandFlags(Login)
435+
assert.NotEmpty(t, flags, "Expected login command to have flags")
436+
437+
var flagNames []string
438+
for _, f := range flags {
439+
flagNames = append(flagNames, f.GetName())
440+
}
441+
assert.Contains(t, flagNames, "server-id", "Expected login command flags to include 'server-id'")
442+
}

0 commit comments

Comments
 (0)