Skip to content

Commit ed7b39d

Browse files
committed
Improve repositores code
Signed-off-by: Michael Sverdlov <michaelsv@jfrog.com>
1 parent 9a20c6c commit ed7b39d

File tree

3 files changed

+206
-122
lines changed

3 files changed

+206
-122
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package python
2+
3+
import (
4+
"github.com/jfrog/jfrog-cli-core/v2/artifactory/commands/repository"
5+
cmdutils "github.com/jfrog/jfrog-cli-core/v2/artifactory/commands/utils"
6+
"github.com/jfrog/jfrog-cli-core/v2/artifactory/utils"
7+
"github.com/jfrog/jfrog-cli-core/v2/common/project"
8+
"github.com/jfrog/jfrog-cli-core/v2/utils/config"
9+
"github.com/jfrog/jfrog-cli-core/v2/utils/coreutils"
10+
"github.com/jfrog/jfrog-client-go/artifactory/services"
11+
"github.com/jfrog/jfrog-client-go/utils/log"
12+
)
13+
14+
type PythonLoginCommand struct {
15+
ר
16+
}
17+
18+
func NewPythonLoginCommand() *PythonLoginCommand {
19+
return &PythonLoginCommand{commandName: "rt_python_login"}
20+
}
21+
22+
// Run configures python to use the specified or selected JFrog Artifactory repository
23+
// for package management, setting up registry and authentication.
24+
func (plc *PythonLoginCommand) Run() (err error) {
25+
// If no repository is specified, prompt the user to select a pypi-compatible repository.
26+
if plc.repo == "" {
27+
// Define filter parameters to select virtual repositories of npm package type.
28+
repoFilterParams := services.RepositoriesFilterParams{
29+
RepoType: utils.Virtual.String(),
30+
PackageType: repository.Npm,
31+
}
32+
33+
// Select repository interactively based on filter parameters and server details.
34+
plc.repo, err = utils.SelectRepositoryInteractively(plc.serverDetails, repoFilterParams)
35+
if err != nil {
36+
return err
37+
}
38+
}
39+
40+
// Initialize NpmrcYarnrcManager for npm to manage registry and authentication configurations.
41+
npmrcManager := cmdutils.NewNpmrcYarnrcManager(project.Npm, plc.repo, plc.serverDetails)
42+
43+
// Configure the registry URL for npm in the npm configuration.
44+
if err = npmrcManager.ConfigureRegistry(); err != nil {
45+
return err
46+
}
47+
48+
// Configure authentication settings, handling token or basic auth as needed.
49+
if err = npmrcManager.ConfigureAuth(); err != nil {
50+
return err
51+
}
52+
53+
// Output success message indicating successful npm configuration.
54+
log.Output(coreutils.PrintTitle("Successfully configured npm client to work with your JFrog Artifactory repository: " + plc.repo))
55+
return nil
56+
}
57+
58+
func (plc *PythonLoginCommand) CommandName() string {
59+
return plc.commandName
60+
}
61+
62+
func (plc *PythonLoginCommand) ServerDetails() (*config.ServerDetails, error) {
63+
return plc.serverDetails, nil
64+
}
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
package utils
2+
3+
import (
4+
"encoding/base64"
5+
"fmt"
6+
"github.com/jfrog/jfrog-cli-core/v2/artifactory/utils/npm"
7+
"github.com/jfrog/jfrog-cli-core/v2/artifactory/utils/yarn"
8+
"github.com/jfrog/jfrog-cli-core/v2/common/project"
9+
"github.com/jfrog/jfrog-cli-core/v2/utils/config"
10+
"github.com/jfrog/jfrog-client-go/utils/errorutils"
11+
"strings"
12+
)
13+
14+
// NpmrcYarnrcManager is responsible for configuring npm and Yarn registries
15+
// and authentication settings based on the specified project type.
16+
type NpmrcYarnrcManager struct {
17+
// buildTool represents the project type, either NPM or Yarn.
18+
buildTool project.ProjectType
19+
// repoUrl holds the URL to the npm or Yarn repository.
20+
repoUrl string
21+
// serverDetails contains configuration details for the Artifactory server.
22+
serverDetails *config.ServerDetails
23+
}
24+
25+
// NewNpmrcYarnrcManager initializes a new NpmrcYarnrcManager with the given project type,
26+
// repository name, and Artifactory server details.
27+
func NewNpmrcYarnrcManager(buildTool project.ProjectType, repoName string, serverDetails *config.ServerDetails) *NpmrcYarnrcManager {
28+
repoUrl := GetNpmRepositoryUrl(repoName, serverDetails.ArtifactoryUrl)
29+
return &NpmrcYarnrcManager{
30+
buildTool: buildTool,
31+
repoUrl: repoUrl,
32+
serverDetails: serverDetails,
33+
}
34+
}
35+
36+
// ConfigureRegistry sets the registry URL in the npmrc or yarnrc file.
37+
func (nm *NpmrcYarnrcManager) Run() error {
38+
switch nm.buildTool {
39+
case project.Npm, project.Yarn:
40+
if err := nm.ConfigureRegistry(); err != nil {
41+
return err
42+
}
43+
return nm.ConfigureAuth()
44+
default:
45+
return errorutils.CheckError(fmt.Errorf("unsupported build tool: %s", nm.buildTool))
46+
}
47+
}
48+
49+
// ConfigureRegistry sets the registry URL in the npmrc or yarnrc file.
50+
func (nm *NpmrcYarnrcManager) ConfigureRegistry() error {
51+
return nm.configSet(NpmConfigRegistryKey, nm.repoUrl)
52+
}
53+
54+
// ConfigureAuth configures authentication in npmrc or yarnrc using token or basic auth,
55+
// or clears authentication for anonymous access.
56+
func (nm *NpmrcYarnrcManager) ConfigureAuth() error {
57+
authArtDetails, err := nm.serverDetails.CreateArtAuthConfig()
58+
if err != nil {
59+
return err
60+
}
61+
62+
// Configure authentication based on available credentials.
63+
switch {
64+
case authArtDetails.GetAccessToken() != "":
65+
return nm.handleNpmrcTokenAuth(authArtDetails.GetAccessToken())
66+
case authArtDetails.GetUser() != "" && authArtDetails.GetPassword() != "":
67+
return nm.handleNpmrcBasicAuth(authArtDetails.GetUser(), authArtDetails.GetPassword())
68+
default:
69+
return nm.handleNpmAnonymousAccess()
70+
}
71+
}
72+
73+
// handleNpmrcTokenAuth sets the token in the npmrc or yarnrc file and clears basic auth if it exists.
74+
func (nm *NpmrcYarnrcManager) handleNpmrcTokenAuth(token string) error {
75+
authKey := nm.createAuthKey(NpmConfigAuthTokenKey)
76+
if err := nm.configSet(authKey, token); err != nil {
77+
return err
78+
}
79+
return nm.removeNpmrcBasicAuthIfExists()
80+
}
81+
82+
// handleNpmrcBasicAuth sets basic auth credentials and clears any token-based auth.
83+
func (nm *NpmrcYarnrcManager) handleNpmrcBasicAuth(user, password string) error {
84+
authKey := nm.createAuthKey(NpmConfigAuthKey)
85+
authValue := basicAuthBase64Encode(user, password)
86+
if err := nm.configSet(authKey, authValue); err != nil {
87+
return err
88+
}
89+
return nm.removeNpmrcTokenAuthIfExists()
90+
}
91+
92+
// handleNpmAnonymousAccess removes any existing authentication settings for anonymous access.
93+
func (nm *NpmrcYarnrcManager) handleNpmAnonymousAccess() error {
94+
if err := nm.removeNpmrcBasicAuthIfExists(); err != nil {
95+
return err
96+
}
97+
return nm.removeNpmrcTokenAuthIfExists()
98+
}
99+
100+
// removeNpmrcBasicAuthIfExists deletes basic auth credentials if present.
101+
func (nm *NpmrcYarnrcManager) removeNpmrcBasicAuthIfExists() error {
102+
return nm.configDelete(nm.createAuthKey(NpmConfigAuthKey))
103+
}
104+
105+
// removeNpmrcTokenAuthIfExists deletes token auth credentials if present.
106+
func (nm *NpmrcYarnrcManager) removeNpmrcTokenAuthIfExists() error {
107+
return nm.configDelete(nm.createAuthKey(NpmConfigAuthTokenKey))
108+
}
109+
110+
// configSet applies a configuration setting in npmrc or yarnrc, based on the build tool type.
111+
func (nm *NpmrcYarnrcManager) configSet(key, value string) error {
112+
switch nm.buildTool {
113+
case project.Npm:
114+
return npm.ConfigSet(key, value, nm.buildTool.String())
115+
case project.Yarn:
116+
return yarn.ConfigSet(key, value, nm.buildTool.String(), false)
117+
default:
118+
return errorutils.CheckError(fmt.Errorf("unsupported build tool: %s", nm.buildTool))
119+
}
120+
}
121+
122+
// configDelete removes a configuration setting from npmrc or yarnrc, based on the build tool type.
123+
func (nm *NpmrcYarnrcManager) configDelete(key string) error {
124+
switch nm.buildTool {
125+
case project.Npm:
126+
return npm.ConfigDelete(key, nm.buildTool.String())
127+
case project.Yarn:
128+
return yarn.ConfigDelete(key, nm.buildTool.String())
129+
default:
130+
return errorutils.CheckError(fmt.Errorf("unsupported build tool: %s", nm.buildTool))
131+
}
132+
}
133+
134+
// createAuthKey generates the correct authentication key for npm or Yarn, based on the repo URL.
135+
func (nm *NpmrcYarnrcManager) createAuthKey(keySuffix string) string {
136+
return fmt.Sprintf("//%s:%s", strings.TrimPrefix(nm.repoUrl, "https://"), keySuffix)
137+
}
138+
139+
// basicAuthBase64Encode encodes user credentials in Base64 for basic authentication.
140+
func basicAuthBase64Encode(user, password string) string {
141+
return base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%s:%s", user, password)))
142+
}
Lines changed: 0 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
11
package utils
22

33
import (
4-
"encoding/base64"
54
"fmt"
65
"github.com/jfrog/jfrog-cli-core/v2/artifactory/utils"
7-
"github.com/jfrog/jfrog-cli-core/v2/artifactory/utils/npm"
8-
"github.com/jfrog/jfrog-cli-core/v2/artifactory/utils/yarn"
96
"github.com/jfrog/jfrog-cli-core/v2/common/build"
107
outFormat "github.com/jfrog/jfrog-cli-core/v2/common/format"
11-
"github.com/jfrog/jfrog-cli-core/v2/common/project"
12-
"github.com/jfrog/jfrog-cli-core/v2/utils/config"
138
"github.com/jfrog/jfrog-cli-core/v2/utils/coreutils"
149
"github.com/jfrog/jfrog-client-go/auth"
1510
"github.com/jfrog/jfrog-client-go/http/httpclient"
@@ -128,120 +123,3 @@ func ExtractNpmOptionsFromArgs(args []string) (detailedSummary, xrayScan bool, s
128123
cleanArgs, buildConfig, err = build.ExtractBuildDetailsFromArgs(cleanArgs)
129124
return
130125
}
131-
132-
// NpmrcYarnrcManager is responsible for configuring npm and Yarn registries
133-
// and authentication settings based on the specified project type.
134-
type NpmrcYarnrcManager struct {
135-
// buildTool represents the project type, either NPM or Yarn.
136-
buildTool project.ProjectType
137-
// repoUrl holds the URL to the npm or Yarn repository.
138-
repoUrl string
139-
// serverDetails contains configuration details for the Artifactory server.
140-
serverDetails *config.ServerDetails
141-
}
142-
143-
// NewNpmrcYarnrcManager initializes a new NpmrcYarnrcManager with the given project type,
144-
// repository name, and Artifactory server details.
145-
func NewNpmrcYarnrcManager(buildTool project.ProjectType, repoName string, serverDetails *config.ServerDetails) *NpmrcYarnrcManager {
146-
repoUrl := GetNpmRepositoryUrl(repoName, serverDetails.ArtifactoryUrl)
147-
return &NpmrcYarnrcManager{
148-
buildTool: buildTool,
149-
repoUrl: repoUrl,
150-
serverDetails: serverDetails,
151-
}
152-
}
153-
154-
// ConfigureRegistry sets the registry URL in the npmrc or yarnrc file.
155-
func (nm *NpmrcYarnrcManager) ConfigureRegistry() error {
156-
return nm.configSet(NpmConfigRegistryKey, nm.repoUrl)
157-
}
158-
159-
// ConfigureAuth configures authentication in npmrc or yarnrc using token or basic auth,
160-
// or clears authentication for anonymous access.
161-
func (nm *NpmrcYarnrcManager) ConfigureAuth() error {
162-
authArtDetails, err := nm.serverDetails.CreateArtAuthConfig()
163-
if err != nil {
164-
return err
165-
}
166-
167-
// Configure authentication based on available credentials.
168-
switch {
169-
case authArtDetails.GetAccessToken() != "":
170-
return nm.handleNpmrcTokenAuth(authArtDetails.GetAccessToken())
171-
case authArtDetails.GetUser() != "" && authArtDetails.GetPassword() != "":
172-
return nm.handleNpmrcBasicAuth(authArtDetails.GetUser(), authArtDetails.GetPassword())
173-
default:
174-
return nm.handleNpmAnonymousAccess()
175-
}
176-
}
177-
178-
// handleNpmrcTokenAuth sets the token in the npmrc or yarnrc file and clears basic auth if it exists.
179-
func (nm *NpmrcYarnrcManager) handleNpmrcTokenAuth(token string) error {
180-
authKey := nm.createAuthKey(NpmConfigAuthTokenKey)
181-
if err := nm.configSet(authKey, token); err != nil {
182-
return err
183-
}
184-
return nm.removeNpmrcBasicAuthIfExists()
185-
}
186-
187-
// handleNpmrcBasicAuth sets basic auth credentials and clears any token-based auth.
188-
func (nm *NpmrcYarnrcManager) handleNpmrcBasicAuth(user, password string) error {
189-
authKey := nm.createAuthKey(NpmConfigAuthKey)
190-
authValue := basicAuthBase64Encode(user, password)
191-
if err := nm.configSet(authKey, authValue); err != nil {
192-
return err
193-
}
194-
return nm.removeNpmrcTokenAuthIfExists()
195-
}
196-
197-
// handleNpmAnonymousAccess removes any existing authentication settings for anonymous access.
198-
func (nm *NpmrcYarnrcManager) handleNpmAnonymousAccess() error {
199-
if err := nm.removeNpmrcBasicAuthIfExists(); err != nil {
200-
return err
201-
}
202-
return nm.removeNpmrcTokenAuthIfExists()
203-
}
204-
205-
// removeNpmrcBasicAuthIfExists deletes basic auth credentials if present.
206-
func (nm *NpmrcYarnrcManager) removeNpmrcBasicAuthIfExists() error {
207-
return nm.configDelete(nm.createAuthKey(NpmConfigAuthKey))
208-
}
209-
210-
// removeNpmrcTokenAuthIfExists deletes token auth credentials if present.
211-
func (nm *NpmrcYarnrcManager) removeNpmrcTokenAuthIfExists() error {
212-
return nm.configDelete(nm.createAuthKey(NpmConfigAuthTokenKey))
213-
}
214-
215-
// configSet applies a configuration setting in npmrc or yarnrc, based on the build tool type.
216-
func (nm *NpmrcYarnrcManager) configSet(key, value string) error {
217-
switch nm.buildTool {
218-
case project.Npm:
219-
return npm.ConfigSet(key, value, nm.buildTool.String())
220-
case project.Yarn:
221-
return yarn.ConfigSet(key, value, nm.buildTool.String(), false)
222-
default:
223-
return errorutils.CheckError(fmt.Errorf("unsupported build tool: %s", nm.buildTool))
224-
}
225-
}
226-
227-
// configDelete removes a configuration setting from npmrc or yarnrc, based on the build tool type.
228-
func (nm *NpmrcYarnrcManager) configDelete(key string) error {
229-
switch nm.buildTool {
230-
case project.Npm:
231-
return npm.ConfigDelete(key, nm.buildTool.String())
232-
case project.Yarn:
233-
return yarn.ConfigDelete(key, nm.buildTool.String())
234-
default:
235-
return errorutils.CheckError(fmt.Errorf("unsupported build tool: %s", nm.buildTool))
236-
}
237-
}
238-
239-
// createAuthKey generates the correct authentication key for npm or Yarn, based on the repo URL.
240-
func (nm *NpmrcYarnrcManager) createAuthKey(keySuffix string) string {
241-
return fmt.Sprintf("//%s:%s", strings.TrimPrefix(nm.repoUrl, "https://"), keySuffix)
242-
}
243-
244-
// basicAuthBase64Encode encodes user credentials in Base64 for basic authentication.
245-
func basicAuthBase64Encode(user, password string) string {
246-
return base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%s:%s", user, password)))
247-
}

0 commit comments

Comments
 (0)