Skip to content

Commit c7e7d24

Browse files
committed
feat: add server id to login command
this allows scripting the login without the modal prompt
1 parent 7a0bc2c commit c7e7d24

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

general/login/login.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,22 @@ const (
1515
)
1616

1717
type LoginCommand struct {
18+
serverId string
1819
}
1920

2021
func NewLoginCommand() *LoginCommand {
2122
return &LoginCommand{}
2223
}
2324

25+
func (lc *LoginCommand) SetServerId(serverId string) *LoginCommand {
26+
lc.serverId = serverId
27+
return lc
28+
}
29+
2430
func (lc *LoginCommand) Run() error {
31+
if lc.serverId != "" {
32+
return existingServerLogin(lc.serverId)
33+
}
2534
configurations, err := config.GetAllServersConfigs()
2635
if err != nil {
2736
return err

general/login/login_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package login
2+
3+
import (
4+
"testing"
5+
6+
"github.com/jfrog/jfrog-cli-core/v2/utils/config"
7+
utilsTests "github.com/jfrog/jfrog-cli-core/v2/utils/tests"
8+
"github.com/stretchr/testify/assert"
9+
"github.com/stretchr/testify/require"
10+
)
11+
12+
func TestSetServerId(t *testing.T) {
13+
lc := NewLoginCommand()
14+
result := lc.SetServerId("my-server")
15+
assert.Equal(t, "my-server", lc.serverId)
16+
// Verify fluent API returns the same instance
17+
assert.Same(t, lc, result)
18+
}
19+
20+
func TestRunWithNonExistentServerId(t *testing.T) {
21+
cleanUp, err := utilsTests.SetJfrogHome()
22+
require.NoError(t, err)
23+
defer cleanUp()
24+
25+
// At least one server must exist so GetConfig actually looks up by ID
26+
err = config.SaveServersConf([]*config.ServerDetails{
27+
{ServerId: "other-server", Url: "https://example.jfrog.io/"},
28+
})
29+
require.NoError(t, err)
30+
31+
lc := NewLoginCommand().SetServerId("non-existent-server")
32+
err = lc.Run()
33+
assert.ErrorContains(t, err, "non-existent-server")
34+
}
35+

0 commit comments

Comments
 (0)