Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setup command - added support for custom configuration and pnpm #1326

Merged
merged 94 commits into from
Jan 9, 2025
Merged
Changes from 1 commit
Commits
Show all changes
94 commits
Select commit Hold shift + click to select a range
bb36015
Improve repositores code
sverdlov93 Oct 25, 2024
6583684
Improve repositores code
sverdlov93 Oct 25, 2024
fb20e20
Improve repositores code
sverdlov93 Oct 27, 2024
9a20c6c
Merge branch 'dev' of https://github.com/jfrog/jfrog-cli-core into np…
sverdlov93 Oct 29, 2024
ed7b39d
Improve repositores code
sverdlov93 Oct 30, 2024
633d8a0
Improve repositores code
sverdlov93 Nov 3, 2024
74c9a47
Improve repositores code
sverdlov93 Nov 3, 2024
e0e5024
Improve repositores code
sverdlov93 Nov 4, 2024
f51ae6d
Improve repositores code
sverdlov93 Nov 4, 2024
7233563
Merge branch 'dev' into npm-login
sverdlov93 Nov 5, 2024
f8e793a
Improve repositores code
sverdlov93 Nov 6, 2024
c53b4c2
Merge branch 'npm-login' of https://github.com/sverdlov93/jfrog-cli-c…
sverdlov93 Nov 6, 2024
c3e0486
Improve repositores code
sverdlov93 Nov 6, 2024
e8cc9f8
Improve repositores code
sverdlov93 Nov 6, 2024
95ba696
Improve repositores code
sverdlov93 Nov 6, 2024
3dff2bb
Improve repositores code
sverdlov93 Nov 6, 2024
0f0856e
Improve repositores code
sverdlov93 Nov 6, 2024
f280aa5
Improve repositores code
sverdlov93 Nov 6, 2024
ecb74bb
Improve repositores code
sverdlov93 Nov 6, 2024
cd0fda3
Improve repositores code
sverdlov93 Nov 6, 2024
547fa02
Improve repositores code
sverdlov93 Nov 6, 2024
265fea7
Improve repositores code
sverdlov93 Nov 6, 2024
e8f884c
Improve repositores code
sverdlov93 Nov 7, 2024
2ee5950
Merge branch 'dev' of https://github.com/jfrog/jfrog-cli-core into np…
sverdlov93 Nov 7, 2024
aec982a
Improve repositores code
sverdlov93 Nov 7, 2024
57df296
Improve repositores code
sverdlov93 Nov 7, 2024
e8d5842
Improve repositores code
sverdlov93 Nov 7, 2024
f84e6f8
Improve repositores code
sverdlov93 Nov 7, 2024
8c2b1c6
Merge branch 'dev' into npm-login
sverdlov93 Nov 10, 2024
e6afac1
Merge branch 'dev' of https://github.com/jfrog/jfrog-cli-core into np…
sverdlov93 Nov 10, 2024
f6eed54
Merge branch 'dev' into npm-login
sverdlov93 Nov 11, 2024
be5cdcb
Improve upload archive progress bar
sverdlov93 Nov 11, 2024
1087833
Merge remote-tracking branch 'sverdlov93/npm-login' into npm-login
sverdlov93 Nov 11, 2024
cd0ce91
Improve upload archive progress bar
sverdlov93 Nov 11, 2024
31b0627
Improve upload archive progress bar
sverdlov93 Nov 11, 2024
89cdb97
Improve upload archive progress bar
sverdlov93 Nov 11, 2024
02252d2
Improve upload archive progress bar
sverdlov93 Nov 11, 2024
76fd3b4
Improve upload archive progress bar
sverdlov93 Nov 11, 2024
77e8d71
Improve upload archive progress bar
sverdlov93 Nov 12, 2024
f28f014
Improve upload archive progress bar
sverdlov93 Nov 12, 2024
0e959a3
Improve upload archive progress bar
sverdlov93 Nov 12, 2024
01e0f45
Improve upload archive progress bar
sverdlov93 Nov 12, 2024
95bf72e
Improve upload archive progress bar
sverdlov93 Nov 12, 2024
c90cbb3
Improve upload archive progress bar
sverdlov93 Nov 12, 2024
5f37066
Improve upload archive progress bar
sverdlov93 Nov 12, 2024
1ecb8ed
Improve upload archive progress bar
sverdlov93 Nov 12, 2024
6e3f8b9
Improve upload archive progress bar
sverdlov93 Nov 17, 2024
07af542
Merge branch 'dev' of https://github.com/jfrog/jfrog-cli-core into np…
sverdlov93 Nov 17, 2024
746d67d
Improve upload archive progress bar
sverdlov93 Nov 20, 2024
5f8e0aa
Merge branch 'dev' of https://github.com/jfrog/jfrog-cli-core into np…
sverdlov93 Nov 20, 2024
43d1a80
Improve upload archive progress bar
sverdlov93 Nov 21, 2024
3f8ab57
Merge branch 'dev' of https://github.com/jfrog/jfrog-cli-core into np…
sverdlov93 Dec 16, 2024
b73d2eb
Merge branch 'dev' into npm-login
sverdlov93 Dec 22, 2024
0d5e16b
Merge branch 'dev' of https://github.com/jfrog/jfrog-cli-core into np…
sverdlov93 Dec 22, 2024
bc7f2d4
Merge branch 'dev' of https://github.com/jfrog/jfrog-cli-core into np…
sverdlov93 Dec 24, 2024
6e5def8
Improve upload archive progress bar
sverdlov93 Dec 24, 2024
a89e2f5
Improve upload archive progress bar
sverdlov93 Dec 24, 2024
e8566f1
Merge remote-tracking branch 'sverdlov93/npm-login' into npm-login
sverdlov93 Dec 24, 2024
3a1bcf0
Improve upload archive progress bar
sverdlov93 Dec 25, 2024
52894c8
Improve upload archive progress bar
sverdlov93 Dec 25, 2024
3a0f99b
Merge branch 'dev' into npm-login
sverdlov93 Dec 25, 2024
70015a8
Merge branch 'dev' into npm-login
sverdlov93 Dec 26, 2024
dc79716
Improve upload archive progress bar
sverdlov93 Dec 26, 2024
4355159
Merge remote-tracking branch 'sverdlov93/npm-login' into npm-login
sverdlov93 Dec 26, 2024
4908ff5
Improve upload archive progress bar
sverdlov93 Dec 26, 2024
84f28a3
Merge branch 'dev' into npm-login
sverdlov93 Dec 26, 2024
c7de277
Merge branch 'dev' of https://github.com/jfrog/jfrog-cli-core into np…
sverdlov93 Dec 28, 2024
84f3f27
Improve upload archive progress bar
sverdlov93 Jan 6, 2025
5efc6e2
Merge branch 'dev' of https://github.com/jfrog/jfrog-cli-core into np…
sverdlov93 Jan 6, 2025
fd928f7
Merge remote-tracking branch 'sverdlov93/npm-login' into npm-login
sverdlov93 Jan 6, 2025
97c4fe2
Improve upload archive progress bar
sverdlov93 Jan 6, 2025
2733b60
Improve upload archive progress bar
sverdlov93 Jan 6, 2025
3a8cfe5
Improve upload archive progress bar
sverdlov93 Jan 6, 2025
a0afe89
Improve upload archive progress bar
sverdlov93 Jan 6, 2025
b3e78b8
Improve upload archive progress bar
sverdlov93 Jan 6, 2025
4a048ae
Improve upload archive progress bar
sverdlov93 Jan 6, 2025
91f9a19
Improve upload archive progress bar
sverdlov93 Jan 6, 2025
0a734da
Improve upload archive progress bar
sverdlov93 Jan 8, 2025
0c20c77
Update .github/workflows/test.yml
sverdlov93 Jan 8, 2025
6ac61ff
Update artifactory/commands/dotnet/dotnetcommand_test.go
sverdlov93 Jan 8, 2025
2b347b8
Update artifactory/commands/dotnet/dotnetcommand_test.go
sverdlov93 Jan 8, 2025
17f4f5f
Update artifactory/commands/dotnet/dotnetcommand.go
sverdlov93 Jan 8, 2025
51b9b20
Improve upload archive progress bar
sverdlov93 Jan 8, 2025
b515800
Improve upload archive progress bar
sverdlov93 Jan 8, 2025
e193963
Merge branch 'npm-login' of https://github.com/sverdlov93/jfrog-cli-c…
sverdlov93 Jan 8, 2025
a412162
Merge branch 'dev' of https://github.com/jfrog/jfrog-cli-core into np…
sverdlov93 Jan 8, 2025
a789a1f
Improve upload archive progress bar
sverdlov93 Jan 8, 2025
c609123
Improve upload archive progress bar
sverdlov93 Jan 8, 2025
d9df61a
Improve upload archive progress bar
sverdlov93 Jan 8, 2025
cb49813
Improve upload archive progress bar
sverdlov93 Jan 8, 2025
5e43e9a
Improve upload archive progress bar
sverdlov93 Jan 8, 2025
287788a
Improve upload archive progress bar
sverdlov93 Jan 9, 2025
5e7af1d
Improve upload archive progress bar
sverdlov93 Jan 9, 2025
707bb56
Improve upload archive progress bar
sverdlov93 Jan 9, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Improve repositores code
Signed-off-by: Michael Sverdlov <[email protected]>
sverdlov93 committed Oct 30, 2024
commit ed7b39d724462eb8e7e3ec729a5130cdf78bf6b0
64 changes: 64 additions & 0 deletions artifactory/commands/python/login.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package python

import (
"github.com/jfrog/jfrog-cli-core/v2/artifactory/commands/repository"
cmdutils "github.com/jfrog/jfrog-cli-core/v2/artifactory/commands/utils"
"github.com/jfrog/jfrog-cli-core/v2/artifactory/utils"
"github.com/jfrog/jfrog-cli-core/v2/common/project"
"github.com/jfrog/jfrog-cli-core/v2/utils/config"
"github.com/jfrog/jfrog-cli-core/v2/utils/coreutils"
"github.com/jfrog/jfrog-client-go/artifactory/services"
"github.com/jfrog/jfrog-client-go/utils/log"
)

type PythonLoginCommand struct {
ר
}

func NewPythonLoginCommand() *PythonLoginCommand {
return &PythonLoginCommand{commandName: "rt_python_login"}
}

// Run configures python to use the specified or selected JFrog Artifactory repository
// for package management, setting up registry and authentication.
func (plc *PythonLoginCommand) Run() (err error) {
// If no repository is specified, prompt the user to select a pypi-compatible repository.
if plc.repo == "" {
// Define filter parameters to select virtual repositories of npm package type.
repoFilterParams := services.RepositoriesFilterParams{
RepoType: utils.Virtual.String(),
PackageType: repository.Npm,
}

// Select repository interactively based on filter parameters and server details.
plc.repo, err = utils.SelectRepositoryInteractively(plc.serverDetails, repoFilterParams)
if err != nil {
return err
}
}

// Initialize NpmrcYarnrcManager for npm to manage registry and authentication configurations.
npmrcManager := cmdutils.NewNpmrcYarnrcManager(project.Npm, plc.repo, plc.serverDetails)

// Configure the registry URL for npm in the npm configuration.
if err = npmrcManager.ConfigureRegistry(); err != nil {
return err
}

// Configure authentication settings, handling token or basic auth as needed.
if err = npmrcManager.ConfigureAuth(); err != nil {
return err
}

// Output success message indicating successful npm configuration.
log.Output(coreutils.PrintTitle("Successfully configured npm client to work with your JFrog Artifactory repository: " + plc.repo))
return nil
}

func (plc *PythonLoginCommand) CommandName() string {
return plc.commandName
}

func (plc *PythonLoginCommand) ServerDetails() (*config.ServerDetails, error) {
return plc.serverDetails, nil
}
142 changes: 142 additions & 0 deletions artifactory/commands/utils/buildtoolconfig.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
package utils

import (
"encoding/base64"
"fmt"
"github.com/jfrog/jfrog-cli-core/v2/artifactory/utils/npm"
"github.com/jfrog/jfrog-cli-core/v2/artifactory/utils/yarn"
"github.com/jfrog/jfrog-cli-core/v2/common/project"
"github.com/jfrog/jfrog-cli-core/v2/utils/config"
"github.com/jfrog/jfrog-client-go/utils/errorutils"
"strings"
)

// NpmrcYarnrcManager is responsible for configuring npm and Yarn registries
// and authentication settings based on the specified project type.
type NpmrcYarnrcManager struct {
// buildTool represents the project type, either NPM or Yarn.
buildTool project.ProjectType
// repoUrl holds the URL to the npm or Yarn repository.
repoUrl string
// serverDetails contains configuration details for the Artifactory server.
serverDetails *config.ServerDetails
}

// NewNpmrcYarnrcManager initializes a new NpmrcYarnrcManager with the given project type,
// repository name, and Artifactory server details.
func NewNpmrcYarnrcManager(buildTool project.ProjectType, repoName string, serverDetails *config.ServerDetails) *NpmrcYarnrcManager {
repoUrl := GetNpmRepositoryUrl(repoName, serverDetails.ArtifactoryUrl)
return &NpmrcYarnrcManager{
buildTool: buildTool,
repoUrl: repoUrl,
serverDetails: serverDetails,
}
}

// ConfigureRegistry sets the registry URL in the npmrc or yarnrc file.
func (nm *NpmrcYarnrcManager) Run() error {
switch nm.buildTool {
case project.Npm, project.Yarn:
if err := nm.ConfigureRegistry(); err != nil {
return err
}
return nm.ConfigureAuth()
default:
return errorutils.CheckError(fmt.Errorf("unsupported build tool: %s", nm.buildTool))
}
}

// ConfigureRegistry sets the registry URL in the npmrc or yarnrc file.
func (nm *NpmrcYarnrcManager) ConfigureRegistry() error {
return nm.configSet(NpmConfigRegistryKey, nm.repoUrl)
}

// ConfigureAuth configures authentication in npmrc or yarnrc using token or basic auth,
// or clears authentication for anonymous access.
func (nm *NpmrcYarnrcManager) ConfigureAuth() error {
authArtDetails, err := nm.serverDetails.CreateArtAuthConfig()
if err != nil {
return err
}

// Configure authentication based on available credentials.
switch {
case authArtDetails.GetAccessToken() != "":
return nm.handleNpmrcTokenAuth(authArtDetails.GetAccessToken())
case authArtDetails.GetUser() != "" && authArtDetails.GetPassword() != "":
return nm.handleNpmrcBasicAuth(authArtDetails.GetUser(), authArtDetails.GetPassword())
default:
return nm.handleNpmAnonymousAccess()
}
}

// handleNpmrcTokenAuth sets the token in the npmrc or yarnrc file and clears basic auth if it exists.
func (nm *NpmrcYarnrcManager) handleNpmrcTokenAuth(token string) error {
authKey := nm.createAuthKey(NpmConfigAuthTokenKey)
if err := nm.configSet(authKey, token); err != nil {
return err
}
return nm.removeNpmrcBasicAuthIfExists()
}

// handleNpmrcBasicAuth sets basic auth credentials and clears any token-based auth.
func (nm *NpmrcYarnrcManager) handleNpmrcBasicAuth(user, password string) error {
authKey := nm.createAuthKey(NpmConfigAuthKey)
authValue := basicAuthBase64Encode(user, password)
if err := nm.configSet(authKey, authValue); err != nil {
return err
}
return nm.removeNpmrcTokenAuthIfExists()
}

// handleNpmAnonymousAccess removes any existing authentication settings for anonymous access.
func (nm *NpmrcYarnrcManager) handleNpmAnonymousAccess() error {
if err := nm.removeNpmrcBasicAuthIfExists(); err != nil {
return err
}
return nm.removeNpmrcTokenAuthIfExists()
}

// removeNpmrcBasicAuthIfExists deletes basic auth credentials if present.
func (nm *NpmrcYarnrcManager) removeNpmrcBasicAuthIfExists() error {
return nm.configDelete(nm.createAuthKey(NpmConfigAuthKey))
}

// removeNpmrcTokenAuthIfExists deletes token auth credentials if present.
func (nm *NpmrcYarnrcManager) removeNpmrcTokenAuthIfExists() error {
return nm.configDelete(nm.createAuthKey(NpmConfigAuthTokenKey))
}

// configSet applies a configuration setting in npmrc or yarnrc, based on the build tool type.
func (nm *NpmrcYarnrcManager) configSet(key, value string) error {
switch nm.buildTool {
case project.Npm:
return npm.ConfigSet(key, value, nm.buildTool.String())
case project.Yarn:
return yarn.ConfigSet(key, value, nm.buildTool.String(), false)
default:
return errorutils.CheckError(fmt.Errorf("unsupported build tool: %s", nm.buildTool))
}
}

// configDelete removes a configuration setting from npmrc or yarnrc, based on the build tool type.
func (nm *NpmrcYarnrcManager) configDelete(key string) error {
switch nm.buildTool {
case project.Npm:
return npm.ConfigDelete(key, nm.buildTool.String())
case project.Yarn:
return yarn.ConfigDelete(key, nm.buildTool.String())
default:
return errorutils.CheckError(fmt.Errorf("unsupported build tool: %s", nm.buildTool))
}
}

// createAuthKey generates the correct authentication key for npm or Yarn, based on the repo URL.
func (nm *NpmrcYarnrcManager) createAuthKey(keySuffix string) string {
return fmt.Sprintf("//%s:%s", strings.TrimPrefix(nm.repoUrl, "https://"), keySuffix)
}

// basicAuthBase64Encode encodes user credentials in Base64 for basic authentication.
func basicAuthBase64Encode(user, password string) string {
return base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%s:%s", user, password)))
}
122 changes: 0 additions & 122 deletions artifactory/commands/utils/npmcmdutils.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
package utils

import (
"encoding/base64"
"fmt"
"github.com/jfrog/jfrog-cli-core/v2/artifactory/utils"
"github.com/jfrog/jfrog-cli-core/v2/artifactory/utils/npm"
"github.com/jfrog/jfrog-cli-core/v2/artifactory/utils/yarn"
"github.com/jfrog/jfrog-cli-core/v2/common/build"
outFormat "github.com/jfrog/jfrog-cli-core/v2/common/format"
"github.com/jfrog/jfrog-cli-core/v2/common/project"
"github.com/jfrog/jfrog-cli-core/v2/utils/config"
"github.com/jfrog/jfrog-cli-core/v2/utils/coreutils"
"github.com/jfrog/jfrog-client-go/auth"
"github.com/jfrog/jfrog-client-go/http/httpclient"
@@ -128,120 +123,3 @@ func ExtractNpmOptionsFromArgs(args []string) (detailedSummary, xrayScan bool, s
cleanArgs, buildConfig, err = build.ExtractBuildDetailsFromArgs(cleanArgs)
return
}

// NpmrcYarnrcManager is responsible for configuring npm and Yarn registries
// and authentication settings based on the specified project type.
type NpmrcYarnrcManager struct {
// buildTool represents the project type, either NPM or Yarn.
buildTool project.ProjectType
// repoUrl holds the URL to the npm or Yarn repository.
repoUrl string
// serverDetails contains configuration details for the Artifactory server.
serverDetails *config.ServerDetails
}

// NewNpmrcYarnrcManager initializes a new NpmrcYarnrcManager with the given project type,
// repository name, and Artifactory server details.
func NewNpmrcYarnrcManager(buildTool project.ProjectType, repoName string, serverDetails *config.ServerDetails) *NpmrcYarnrcManager {
repoUrl := GetNpmRepositoryUrl(repoName, serverDetails.ArtifactoryUrl)
return &NpmrcYarnrcManager{
buildTool: buildTool,
repoUrl: repoUrl,
serverDetails: serverDetails,
}
}

// ConfigureRegistry sets the registry URL in the npmrc or yarnrc file.
func (nm *NpmrcYarnrcManager) ConfigureRegistry() error {
return nm.configSet(NpmConfigRegistryKey, nm.repoUrl)
}

// ConfigureAuth configures authentication in npmrc or yarnrc using token or basic auth,
// or clears authentication for anonymous access.
func (nm *NpmrcYarnrcManager) ConfigureAuth() error {
authArtDetails, err := nm.serverDetails.CreateArtAuthConfig()
if err != nil {
return err
}

// Configure authentication based on available credentials.
switch {
case authArtDetails.GetAccessToken() != "":
return nm.handleNpmrcTokenAuth(authArtDetails.GetAccessToken())
case authArtDetails.GetUser() != "" && authArtDetails.GetPassword() != "":
return nm.handleNpmrcBasicAuth(authArtDetails.GetUser(), authArtDetails.GetPassword())
default:
return nm.handleNpmAnonymousAccess()
}
}

// handleNpmrcTokenAuth sets the token in the npmrc or yarnrc file and clears basic auth if it exists.
func (nm *NpmrcYarnrcManager) handleNpmrcTokenAuth(token string) error {
authKey := nm.createAuthKey(NpmConfigAuthTokenKey)
if err := nm.configSet(authKey, token); err != nil {
return err
}
return nm.removeNpmrcBasicAuthIfExists()
}

// handleNpmrcBasicAuth sets basic auth credentials and clears any token-based auth.
func (nm *NpmrcYarnrcManager) handleNpmrcBasicAuth(user, password string) error {
authKey := nm.createAuthKey(NpmConfigAuthKey)
authValue := basicAuthBase64Encode(user, password)
if err := nm.configSet(authKey, authValue); err != nil {
return err
}
return nm.removeNpmrcTokenAuthIfExists()
}

// handleNpmAnonymousAccess removes any existing authentication settings for anonymous access.
func (nm *NpmrcYarnrcManager) handleNpmAnonymousAccess() error {
if err := nm.removeNpmrcBasicAuthIfExists(); err != nil {
return err
}
return nm.removeNpmrcTokenAuthIfExists()
}

// removeNpmrcBasicAuthIfExists deletes basic auth credentials if present.
func (nm *NpmrcYarnrcManager) removeNpmrcBasicAuthIfExists() error {
return nm.configDelete(nm.createAuthKey(NpmConfigAuthKey))
}

// removeNpmrcTokenAuthIfExists deletes token auth credentials if present.
func (nm *NpmrcYarnrcManager) removeNpmrcTokenAuthIfExists() error {
return nm.configDelete(nm.createAuthKey(NpmConfigAuthTokenKey))
}

// configSet applies a configuration setting in npmrc or yarnrc, based on the build tool type.
func (nm *NpmrcYarnrcManager) configSet(key, value string) error {
switch nm.buildTool {
case project.Npm:
return npm.ConfigSet(key, value, nm.buildTool.String())
case project.Yarn:
return yarn.ConfigSet(key, value, nm.buildTool.String(), false)
default:
return errorutils.CheckError(fmt.Errorf("unsupported build tool: %s", nm.buildTool))
}
}

// configDelete removes a configuration setting from npmrc or yarnrc, based on the build tool type.
func (nm *NpmrcYarnrcManager) configDelete(key string) error {
switch nm.buildTool {
case project.Npm:
return npm.ConfigDelete(key, nm.buildTool.String())
case project.Yarn:
return yarn.ConfigDelete(key, nm.buildTool.String())
default:
return errorutils.CheckError(fmt.Errorf("unsupported build tool: %s", nm.buildTool))
}
}

// createAuthKey generates the correct authentication key for npm or Yarn, based on the repo URL.
func (nm *NpmrcYarnrcManager) createAuthKey(keySuffix string) string {
return fmt.Sprintf("//%s:%s", strings.TrimPrefix(nm.repoUrl, "https://"), keySuffix)
}

// basicAuthBase64Encode encodes user credentials in Base64 for basic authentication.
func basicAuthBase64Encode(user, password string) string {
return base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%s:%s", user, password)))
}