@@ -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.
7173func 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.
9888func (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.
114116func (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.
0 commit comments