Skip to content

Commit

Permalink
Improve upload archive progress bar
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Sverdlov <[email protected]>
  • Loading branch information
sverdlov93 committed Nov 11, 2024
1 parent e6afac1 commit be5cdcb
Show file tree
Hide file tree
Showing 12 changed files with 91 additions and 173 deletions.
102 changes: 51 additions & 51 deletions artifactory/commands/buildtoollogin/buildtoollogin.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ import (
"github.com/jfrog/jfrog-client-go/utils/log"
)

// BuildToolLoginCommand configures registries and authentication for various build tools (npm, Yarn, Pip, Pipenv, Poetry, Go)
// PackageManagerLoginCommand configures registries and authentication for various package managers (npm, Yarn, Pip, Pipenv, Poetry, Go)
// based on the specified project type.
type BuildToolLoginCommand struct {
// buildTool represents the type of project (e.g., NPM, Yarn).
buildTool project.ProjectType
type PackageManagerLoginCommand struct {
// packageManager represents the type of project (e.g., NPM, Yarn).
packageManager project.ProjectType
// repoName is the name of the repository used for configuration.
repoName string
// serverDetails contains Artifactory server configuration.
Expand All @@ -30,81 +30,81 @@ type BuildToolLoginCommand struct {
commandName string
}

// NewBuildToolLoginCommand initializes a new BuildToolLoginCommand for the specified project type
// NewPackageManagerLoginCommand initializes a new PackageManagerLoginCommand for the specified project type
// and automatically sets a command name for the login operation.
func NewBuildToolLoginCommand(buildTool project.ProjectType) *BuildToolLoginCommand {
return &BuildToolLoginCommand{
buildTool: buildTool,
commandName: buildTool.String() + "_login",
func NewPackageManagerLoginCommand(packageManager project.ProjectType) *PackageManagerLoginCommand {
return &PackageManagerLoginCommand{
packageManager: packageManager,
commandName: packageManager.String() + "_login",
}
}

// buildToolToPackageType maps project types to corresponding Artifactory package types (e.g., npm, pypi).
func buildToolToPackageType(buildTool project.ProjectType) (string, error) {
switch buildTool {
// packageManagerToPackageType maps project types to corresponding Artifactory package types (e.g., npm, pypi).
func packageManagerToPackageType(packageManager project.ProjectType) (string, error) {
switch packageManager {
case project.Npm, project.Yarn:
return repository.Npm, nil
case project.Pip, project.Pipenv, project.Poetry:
return repository.Pypi, nil
case project.Go:
return repository.Go, nil
default:
return "", errorutils.CheckErrorf("unsupported build tool: %s", buildTool)
return "", errorutils.CheckErrorf("unsupported package manager: %s", packageManager)
}
}

// CommandName returns the name of the login command.
func (btlc *BuildToolLoginCommand) CommandName() string {
return btlc.commandName
func (pmlc *PackageManagerLoginCommand) CommandName() string {
return pmlc.commandName
}

// SetServerDetails assigns the server configuration details to the command.
func (btlc *BuildToolLoginCommand) SetServerDetails(serverDetails *config.ServerDetails) *BuildToolLoginCommand {
btlc.serverDetails = serverDetails
return btlc
func (pmlc *PackageManagerLoginCommand) SetServerDetails(serverDetails *config.ServerDetails) *PackageManagerLoginCommand {
pmlc.serverDetails = serverDetails
return pmlc
}

// ServerDetails returns the stored server configuration details.
func (btlc *BuildToolLoginCommand) ServerDetails() (*config.ServerDetails, error) {
return btlc.serverDetails, nil
func (pmlc *PackageManagerLoginCommand) ServerDetails() (*config.ServerDetails, error) {
return pmlc.serverDetails, nil
}

// Run executes the configuration method corresponding to the project type specified for the command.
func (btlc *BuildToolLoginCommand) Run() (err error) {
if btlc.repoName == "" {
func (pmlc *PackageManagerLoginCommand) Run() (err error) {
if pmlc.repoName == "" {
// Prompt the user to select a virtual repository that matches the project type.
if err = btlc.GetRepositoryNameFromUserInteractively(); err != nil {
if err = pmlc.getRepositoryNameFromUserInteractively(); err != nil {
return err
}
}

// Configure the appropriate tool based on the project type.
switch btlc.buildTool {
// Configure the appropriate package manager based on the project type.
switch pmlc.packageManager {
case project.Npm:
err = btlc.configureNpm()
err = pmlc.configureNpm()
case project.Yarn:
err = btlc.configureYarn()
err = pmlc.configureYarn()
case project.Pip, project.Pipenv:
err = btlc.configurePip()
err = pmlc.configurePip()
case project.Poetry:
err = btlc.configurePoetry()
err = pmlc.configurePoetry()
case project.Go:
err = btlc.configureGo()
err = pmlc.configureGo()
default:
err = errorutils.CheckErrorf("unsupported build tool: %s", btlc.buildTool)
err = errorutils.CheckErrorf("unsupported package manager: %s", pmlc.packageManager)
}
if err != nil {
return fmt.Errorf("failed to configure %s: %w", btlc.buildTool.String(), err)
return fmt.Errorf("failed to configure %s: %w", pmlc.packageManager.String(), err)
}

log.Info(fmt.Sprintf("Successfully configured %s to use JFrog Artifactory repository '%s'.", btlc.buildTool.String(), btlc.repoName))
log.Info(fmt.Sprintf("Successfully configured %s to use JFrog Artifactory repository '%s'.", pmlc.packageManager.String(), pmlc.repoName))
return nil
}

// GetRepositoryNameFromUserInteractively prompts the user to select a compatible virtual repository.
func (btlc *BuildToolLoginCommand) GetRepositoryNameFromUserInteractively() error {
// Map the build tool to its corresponding package type.
packageType, err := buildToolToPackageType(btlc.buildTool)
// getRepositoryNameFromUserInteractively prompts the user to select a compatible virtual repository.
func (pmlc *PackageManagerLoginCommand) getRepositoryNameFromUserInteractively() error {
// Map the package manager to its corresponding package type.
packageType, err := packageManagerToPackageType(pmlc.packageManager)
if err != nil {
return err
}
Expand All @@ -114,16 +114,16 @@ func (btlc *BuildToolLoginCommand) GetRepositoryNameFromUserInteractively() erro
}

// Prompt for repository selection based on filter parameters.
btlc.repoName, err = utils.SelectRepositoryInteractively(btlc.serverDetails, repoFilterParams)
pmlc.repoName, err = utils.SelectRepositoryInteractively(pmlc.serverDetails, repoFilterParams)
return err
}

// configurePip sets the global index-url for pip and pipenv to use the Artifactory PyPI repository.
// Runs the following command:
//
// pip config set global.index-url https://<user>:<token>@<your-artifactory-url>/artifactory/api/pypi/<repo-name>/simple
func (btlc *BuildToolLoginCommand) configurePip() error {
repoWithCredsUrl, err := pythoncommands.GetPypiRepoUrl(btlc.serverDetails, btlc.repoName, false)
func (pmlc *PackageManagerLoginCommand) configurePip() error {
repoWithCredsUrl, err := pythoncommands.GetPypiRepoUrl(pmlc.serverDetails, pmlc.repoName, false)
if err != nil {
return err
}
Expand All @@ -135,12 +135,12 @@ func (btlc *BuildToolLoginCommand) configurePip() error {
//
// poetry config repositories.<repo-name> https://<your-artifactory-url>/artifactory/api/pypi/<repo-name>/simple
// poetry config http-basic.<repo-name> <user> <password/token>
func (btlc *BuildToolLoginCommand) configurePoetry() error {
repoUrl, username, password, err := pythoncommands.GetPypiRepoUrlWithCredentials(btlc.serverDetails, btlc.repoName, false)
func (pmlc *PackageManagerLoginCommand) configurePoetry() error {
repoUrl, username, password, err := pythoncommands.GetPypiRepoUrlWithCredentials(pmlc.serverDetails, pmlc.repoName, false)
if err != nil {
return err
}
return pythoncommands.RunPoetryConfig(repoUrl.String(), username, password, btlc.repoName)
return pythoncommands.RunPoetryConfig(repoUrl.String(), username, password, pmlc.repoName)
}

// configureNpm configures npm to use the Artifactory repository URL and sets authentication.
Expand All @@ -155,14 +155,14 @@ func (btlc *BuildToolLoginCommand) configurePoetry() error {
// For basic auth:
//
// npm config set //your-artifactory-url/artifactory/api/npm/<repo-name>/:_auth "<base64-encoded-username:password>"
func (btlc *BuildToolLoginCommand) configureNpm() error {
repoUrl := commandsutils.GetNpmRepositoryUrl(btlc.repoName, btlc.serverDetails.ArtifactoryUrl)
func (pmlc *PackageManagerLoginCommand) configureNpm() error {
repoUrl := commandsutils.GetNpmRepositoryUrl(pmlc.repoName, pmlc.serverDetails.ArtifactoryUrl)

if err := npm.ConfigSet(commandsutils.NpmConfigRegistryKey, repoUrl, "npm"); err != nil {
return err
}

authKey, authValue := commandsutils.GetNpmAuthKeyValue(btlc.serverDetails, repoUrl)
authKey, authValue := commandsutils.GetNpmAuthKeyValue(pmlc.serverDetails, repoUrl)
if authKey != "" && authValue != "" {
return npm.ConfigSet(authKey, authValue, "npm")
}
Expand All @@ -181,14 +181,14 @@ func (btlc *BuildToolLoginCommand) configureNpm() error {
// For basic auth:
//
// yarn config set //your-artifactory-url/artifactory/api/npm/<repo-name>/:_auth "<base64-encoded-username:password>"
func (btlc *BuildToolLoginCommand) configureYarn() error {
repoUrl := commandsutils.GetNpmRepositoryUrl(btlc.repoName, btlc.serverDetails.ArtifactoryUrl)
func (pmlc *PackageManagerLoginCommand) configureYarn() error {
repoUrl := commandsutils.GetNpmRepositoryUrl(pmlc.repoName, pmlc.serverDetails.ArtifactoryUrl)

if err := yarn.ConfigSet(commandsutils.NpmConfigRegistryKey, repoUrl, "yarn", false); err != nil {
return err
}

authKey, authValue := commandsutils.GetNpmAuthKeyValue(btlc.serverDetails, repoUrl)
authKey, authValue := commandsutils.GetNpmAuthKeyValue(pmlc.serverDetails, repoUrl)
if authKey != "" && authValue != "" {
return yarn.ConfigSet(authKey, authValue, "yarn", false)
}
Expand All @@ -199,8 +199,8 @@ func (btlc *BuildToolLoginCommand) configureYarn() error {
// Runs the following command:
//
// go env -w GOPROXY=https://<user>:<token>@<your-artifactory-url>/artifactory/go/<repo-name>,direct
func (btlc *BuildToolLoginCommand) configureGo() error {
repoWithCredsUrl, err := gocommands.GetArtifactoryRemoteRepoUrl(btlc.serverDetails, btlc.repoName, gocommands.GoProxyUrlParams{Direct: true})
func (pmlc *PackageManagerLoginCommand) configureGo() error {
repoWithCredsUrl, err := gocommands.GetArtifactoryRemoteRepoUrl(pmlc.serverDetails, pmlc.repoName, gocommands.GoProxyUrlParams{Direct: true})
if err != nil {
return err
}
Expand Down
9 changes: 4 additions & 5 deletions artifactory/commands/python/poetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,17 +187,16 @@ func poetryUpdate() (err error) {
func addRepoToPyprojectFile(filepath, poetryRepoName, repoUrl string) error {
viper.SetConfigType("toml")
viper.SetConfigFile(filepath)
err := viper.ReadInConfig()
if err != nil {
if err := viper.ReadInConfig(); err != nil {
return errorutils.CheckErrorf("Failed to read pyproject.toml: %s", err.Error())
}
viper.Set("tool.poetry.source", []map[string]string{{"name": poetryRepoName, "url": repoUrl}})
err = viper.WriteConfig()
if err != nil {
if err := viper.WriteConfig(); err != nil {
return errorutils.CheckErrorf("Failed to add tool.poetry.source to pyproject.toml: %s", err.Error())

}
log.Info(fmt.Sprintf("Added tool.poetry.source name:%q url:%q", poetryRepoName, repoUrl))
return err
return nil
}

func (pc *PoetryCommand) CommandName() string {
Expand Down
3 changes: 1 addition & 2 deletions artifactory/commands/python/python.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,7 @@ func GetPypiRepoUrl(serverDetails *config.ServerDetails, repository string, isCu
func RunConfigCommand(buildTool project.ProjectType, args []string) error {
log.Debug("Running", buildTool.String(), "config command...")
configCmd := gofrogcmd.NewCommand(buildTool.String(), "config", args)
err := gofrogcmd.RunCmd(configCmd)
if err != nil {
if err := gofrogcmd.RunCmd(configCmd); err != nil {
return errorutils.CheckErrorf("%s config command failed with: %q", buildTool.String(), err)
}
return nil
Expand Down
24 changes: 0 additions & 24 deletions artifactory/utils/npm/config-delete.go

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/jfrog/jfrog-client-go/utils/errorutils"
)

// This method runs "npm config get" command and returns the value of the specified npm configuration.
func ConfigGet(npmFlags []string, confName, executablePath string) (string, error) {
configGetCmdConfig := createConfigGetCmdConfig(executablePath, confName, npmFlags)
output, err := gofrogcmd.RunCmdOutput(configGetCmdConfig)
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/jfrog/jfrog-client-go/utils/errorutils"
)

// This method runs "npm config set" command and sets the npm configuration.
func ConfigSet(key, value, executablePath string) error {
configGetCmdConfig := createConfigSetCmdConfig(executablePath, key, value)
_, err := gofrogcmd.RunCmdOutput(configGetCmdConfig)
Expand Down
21 changes: 0 additions & 21 deletions artifactory/utils/yarn/configdelete.go

This file was deleted.

1 change: 1 addition & 0 deletions artifactory/utils/yarn/configget.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strings"
)

// This method runs "yarn config set" command and sets the yarn configuration.
func ConfigGet(key, executablePath string, jsonOutput bool) (string, error) {
var flags []string = nil
if jsonOutput {
Expand Down
1 change: 1 addition & 0 deletions artifactory/utils/yarn/configset.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/jfrog/jfrog-client-go/utils/errorutils"
)

// This method runs "yarn config set" command and sets the yarn configuration.
func ConfigSet(key, value, executablePath string, jsonInput bool) error {
var flags []string = nil
if jsonInput {
Expand Down
22 changes: 11 additions & 11 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/jfrog/jfrog-cli-core/v2

go 1.22.7
go 1.22.9

require github.com/c-bata/go-prompt v0.2.5 // Should not be updated to 0.2.6 due to a bug (https://github.com/jfrog/jfrog-cli-core/pull/372)

Expand All @@ -22,11 +22,11 @@ require (
github.com/stretchr/testify v1.9.0
github.com/urfave/cli v1.22.16
github.com/vbauerster/mpb/v8 v8.8.3
golang.org/x/exp v0.0.0-20241009180824-f66d83c29e7c
golang.org/x/mod v0.21.0
golang.org/x/sync v0.8.0
golang.org/x/term v0.25.0
golang.org/x/text v0.19.0
golang.org/x/exp v0.0.0-20241108190413-2d47ceb2692f
golang.org/x/mod v0.22.0
golang.org/x/sync v0.9.0
golang.org/x/term v0.26.0
golang.org/x/text v0.20.0
gopkg.in/yaml.v3 v3.0.1
)

Expand All @@ -35,7 +35,7 @@ require (
github.com/BurntSushi/toml v1.4.0 // indirect
github.com/CycloneDX/cyclonedx-go v0.9.0 // indirect
github.com/Microsoft/go-winio v0.6.1 // indirect
github.com/ProtonMail/go-crypto v1.0.0 // indirect
github.com/ProtonMail/go-crypto v1.1.2 // indirect
github.com/VividCortex/ewma v1.2.0 // indirect
github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d // indirect
github.com/andybalholm/brotli v1.1.0 // indirect
Expand Down Expand Up @@ -88,10 +88,10 @@ require (
github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778 // indirect
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.9.0 // indirect
golang.org/x/crypto v0.28.0 // indirect
golang.org/x/net v0.30.0 // indirect
golang.org/x/sys v0.26.0 // indirect
golang.org/x/tools v0.26.0 // indirect
golang.org/x/crypto v0.29.0 // indirect
golang.org/x/net v0.31.0 // indirect
golang.org/x/sys v0.27.0 // indirect
golang.org/x/tools v0.27.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
)
Expand Down
Loading

0 comments on commit be5cdcb

Please sign in to comment.