Skip to content

Commit 3a1bcf0

Browse files
committed
Improve upload archive progress bar
Signed-off-by: Michael Sverdlov <[email protected]>
1 parent e8566f1 commit 3a1bcf0

File tree

3 files changed

+80
-23
lines changed

3 files changed

+80
-23
lines changed

artifactory/commands/packagemanagerlogin/packagemanagerlogin.go

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import (
2121
"github.com/jfrog/jfrog-client-go/utils/log"
2222
"golang.org/x/exp/maps"
2323
"net/url"
24-
"sort"
24+
"slices"
2525
)
2626

2727
// packageManagerToRepositoryPackageType maps project types to corresponding Artifactory repository package types.
@@ -52,6 +52,8 @@ type PackageManagerLoginCommand struct {
5252
packageManager project.ProjectType
5353
// repoName is the name of the repository used for configuration.
5454
repoName string
55+
// projectKey is the JFrog Project key in JFrog Platform.
56+
projectKey string
5557
// serverDetails contains Artifactory server configuration.
5658
serverDetails *config.ServerDetails
5759
// commandName specifies the command for this instance.
@@ -70,8 +72,9 @@ func NewPackageManagerLoginCommand(packageManager project.ProjectType) *PackageM
7072
// GetSupportedPackageManagersList returns a sorted list of supported package managers.
7173
func GetSupportedPackageManagersList() []project.ProjectType {
7274
allSupportedPackageManagers := maps.Keys(packageManagerToRepositoryPackageType)
73-
sort.Slice(allSupportedPackageManagers, func(i, j int) bool {
74-
return allSupportedPackageManagers[i] < allSupportedPackageManagers[j]
75+
// Sort keys based on their natural enum order
76+
slices.SortFunc(allSupportedPackageManagers, func(a, b project.ProjectType) int {
77+
return int(a) - int(b)
7578
})
7679
return allSupportedPackageManagers
7780
}
@@ -81,19 +84,6 @@ func IsSupportedPackageManager(packageManager project.ProjectType) bool {
8184
return exists
8285
}
8386

84-
// packageManagerToPackageType maps project types to corresponding Artifactory package types (e.g., npm, pypi).
85-
func packageManagerToPackageType(packageManager project.ProjectType) (string, error) {
86-
// Retrieve the package type from the map.
87-
if packageType, exists := packageManagerToRepositoryPackageType[packageManager]; exists {
88-
return packageType, nil
89-
}
90-
if !IsSupportedPackageManager(packageManager) {
91-
return "", errorutils.CheckErrorf("unsupported package type for package manager: %s", packageManager)
92-
}
93-
// Return an error if the package manager is unsupported.
94-
return packageManagerToRepositoryPackageType[packageManager], nil
95-
}
96-
9787
// CommandName returns the name of the login command.
9888
func (pmlc *PackageManagerLoginCommand) CommandName() string {
9989
return pmlc.commandName
@@ -110,8 +100,24 @@ func (pmlc *PackageManagerLoginCommand) ServerDetails() (*config.ServerDetails,
110100
return pmlc.serverDetails, nil
111101
}
112102

103+
// SetRepoName assigns the repository name to the command.
104+
func (pmlc *PackageManagerLoginCommand) SetRepoName(repoName string) *PackageManagerLoginCommand {
105+
pmlc.repoName = repoName
106+
return pmlc
107+
}
108+
109+
// SetProjectKey assigns the project key to the command.
110+
func (pmlc *PackageManagerLoginCommand) SetProjectKey(projectKey string) *PackageManagerLoginCommand {
111+
pmlc.projectKey = projectKey
112+
return pmlc
113+
}
114+
113115
// Run executes the configuration method corresponding to the package manager specified for the command.
114116
func (pmlc *PackageManagerLoginCommand) Run() (err error) {
117+
if IsSupportedPackageManager(pmlc.packageManager) {
118+
return errorutils.CheckErrorf("unsupported package manager: %s", pmlc.packageManager)
119+
}
120+
115121
if pmlc.repoName == "" {
116122
// Prompt the user to select a virtual repository that matches the package manager.
117123
if err = pmlc.promptUserToSelectRepository(); err != nil {
@@ -147,15 +153,11 @@ func (pmlc *PackageManagerLoginCommand) Run() (err error) {
147153
}
148154

149155
// promptUserToSelectRepository prompts the user to select a compatible virtual repository.
150-
func (pmlc *PackageManagerLoginCommand) promptUserToSelectRepository() error {
151-
// Map the package manager to its corresponding package type.
152-
packageType, err := packageManagerToPackageType(pmlc.packageManager)
153-
if err != nil {
154-
return err
155-
}
156+
func (pmlc *PackageManagerLoginCommand) promptUserToSelectRepository() (err error) {
156157
repoFilterParams := services.RepositoriesFilterParams{
157158
RepoType: utils.Virtual.String(),
158-
PackageType: packageType,
159+
PackageType: packageManagerToRepositoryPackageType[pmlc.packageManager],
160+
ProjectKey: pmlc.projectKey,
159161
}
160162

161163
// Prompt for repository selection based on filter parameters.

artifactory/commands/packagemanagerlogin/packagemanagerlogin_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
clientTestUtils "github.com/jfrog/jfrog-client-go/utils/tests"
1414
"github.com/stretchr/testify/assert"
1515
"github.com/stretchr/testify/require"
16+
"golang.org/x/exp/slices"
1617
"os"
1718
"os/exec"
1819
"path/filepath"
@@ -52,6 +53,13 @@ func createTestPackageManagerLoginCommand(packageManager project.ProjectType) *P
5253
return cmd
5354
}
5455

56+
func TestPackageManagerLoginCommand_NotSupported(t *testing.T) {
57+
notSupportedLoginCmd := createTestPackageManagerLoginCommand(project.Cocoapods)
58+
err := notSupportedLoginCmd.Run()
59+
assert.Error(t, err)
60+
assert.ErrorContains(t, err, "unsupported package manager")
61+
}
62+
5563
func TestPackageManagerLoginCommand_Npm(t *testing.T) {
5664
// Create a temporary directory to act as the environment's npmrc file location.
5765
tempDir := t.TempDir()
@@ -381,3 +389,20 @@ func testBuildToolLoginCommandConfigureDotnetNuget(t *testing.T, packageManager
381389
})
382390
}
383391
}
392+
393+
func TestGetSupportedPackageManagersList(t *testing.T) {
394+
result := GetSupportedPackageManagersList()
395+
// Check that Go is before Pip, and Pip is before Npm using GreaterOrEqual
396+
assert.GreaterOrEqual(t, slices.Index(result, project.Pip), slices.Index(result, project.Go), "Go should come before Pip")
397+
assert.GreaterOrEqual(t, slices.Index(result, project.Npm), slices.Index(result, project.Pip), "Pip should come before Npm")
398+
}
399+
400+
func TestIsSupportedPackageManager(t *testing.T) {
401+
// Test valid package managers
402+
for pm := range packageManagerToRepositoryPackageType {
403+
assert.True(t, IsSupportedPackageManager(pm), "Package manager %s should be supported", pm)
404+
}
405+
406+
// Test unsupported package manager
407+
assert.False(t, IsSupportedPackageManager(project.Cocoapods), "Package manager Cocoapods should not be supported")
408+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package project
2+
3+
import (
4+
"github.com/stretchr/testify/assert"
5+
"testing"
6+
)
7+
8+
func TestFromString(t *testing.T) {
9+
// Test valid conversions
10+
tests := []struct {
11+
input string
12+
expected ProjectType
13+
}{
14+
{"go", Go},
15+
{"pip", Pip},
16+
{"npm", Npm},
17+
{"pnpm", Pnpm},
18+
}
19+
20+
for _, test := range tests {
21+
t.Run(test.input, func(t *testing.T) {
22+
result := FromString(test.input)
23+
assert.Equal(t, test.expected, result, "For input %s, expected %v but got %v", test.input, test.expected, result)
24+
})
25+
}
26+
27+
// Test invalid conversion
28+
result := FromString("InvalidProject")
29+
assert.Equal(t, ProjectType(-1), result, "Expected -1 for invalid project type")
30+
}

0 commit comments

Comments
 (0)