From 3a8cfe51b903d6856a22cf45b7b548d33f17a002 Mon Sep 17 00:00:00 2001 From: Michael Sverdlov Date: Mon, 6 Jan 2025 17:38:12 +0200 Subject: [PATCH] Improve upload archive progress bar Signed-off-by: Michael Sverdlov --- artifactory/commands/python/pip.go | 21 ++++++-------------- artifactory/commands/python/pip_test.go | 26 +++++++++++++++++++++++++ artifactory/commands/python/poetry.go | 4 ++-- artifactory/commands/python/python.go | 2 +- artifactory/commands/setup/setup.go | 8 +++++++- 5 files changed, 42 insertions(+), 19 deletions(-) create mode 100644 artifactory/commands/python/pip_test.go diff --git a/artifactory/commands/python/pip.go b/artifactory/commands/python/pip.go index 3356c78e8..ca6ac8dee 100644 --- a/artifactory/commands/python/pip.go +++ b/artifactory/commands/python/pip.go @@ -2,7 +2,6 @@ package python import ( "fmt" - "github.com/jfrog/jfrog-cli-core/v2/common/project" "io" "os" "os/exec" @@ -50,21 +49,13 @@ func (pc *PipCommand) SetCommandName(commandName string) *PipCommand { return pc } -// Configure the repository URL for pipenv to use Artifactory as a repository. -func RunPipConfig(repoWithCredsUrl string) error { - // If PIP_CONFIG_FILE is set, write the configuration to the custom config file manually. - // Using 'pip config set' native command is not supported together with PIP_CONFIG_FILE. - if customPipConfigPath := os.Getenv("PIP_CONFIG_FILE"); customPipConfigPath != "" { - if err := os.MkdirAll(filepath.Dir(customPipConfigPath), os.ModePerm); err != nil { - return err - } - // Write the configuration to pip.conf. - configContent := fmt.Sprintf("[global]\nindex-url = %s\n", repoWithCredsUrl) - return os.WriteFile(customPipConfigPath, []byte(configContent), 0644) +func CreatePipConfigManually(customPipConfigPath, repoWithCredsUrl string) error { + if err := os.MkdirAll(filepath.Dir(customPipConfigPath), os.ModePerm); err != nil { + return err } - - // If PIP_CONFIG_FILE is not set, use 'pip config set' native command. - return runConfigCommand(project.Pip, []string{"set", "global.index-url", repoWithCredsUrl}) + // Write the configuration to pip.conf. + configContent := fmt.Sprintf("[global]\nindex-url = %s\n", repoWithCredsUrl) + return os.WriteFile(customPipConfigPath, []byte(configContent), 0644) } func (pc *PipCommand) CommandName() string { diff --git a/artifactory/commands/python/pip_test.go b/artifactory/commands/python/pip_test.go new file mode 100644 index 000000000..321a2a074 --- /dev/null +++ b/artifactory/commands/python/pip_test.go @@ -0,0 +1,26 @@ +package python + +import ( + "github.com/stretchr/testify/assert" + "os" + "path/filepath" + "testing" +) + +func TestCreatePipConfigManually(t *testing.T) { + // Define the test parameters + customConfigPath := filepath.Join(t.TempDir(), "/tmp/test/pip.conf") + repoWithCredsUrl := "https://example.com/simple/" + expectedContent := "[global]\nindex-url = https://example.com/simple/\n" + + // Call the function under test + err := CreatePipConfigManually(customConfigPath, repoWithCredsUrl) + + // Assert no error occurred + assert.NoError(t, err) + + // Verify the file exists and has the correct content + fileContent, err := os.ReadFile(customConfigPath) + assert.NoError(t, err) + assert.Equal(t, expectedContent, string(fileContent)) +} diff --git a/artifactory/commands/python/poetry.go b/artifactory/commands/python/poetry.go index 019287cb2..ec362653e 100644 --- a/artifactory/commands/python/poetry.go +++ b/artifactory/commands/python/poetry.go @@ -164,14 +164,14 @@ func ConfigPoetryRepo(url, username, password, configRepoName string) error { func RunPoetryConfig(url, username, password, configRepoName string) error { // Add the poetry repository config // poetry config repositories. https:///artifactory/api/pypi//simple - err := runConfigCommand(project.Poetry, []string{poetryConfigRepoPrefix + configRepoName, url}) + err := RunConfigCommand(project.Poetry, []string{poetryConfigRepoPrefix + configRepoName, url}) if err != nil { return err } // Set the poetry repository credentials // poetry config http-basic. - return runConfigCommand(project.Poetry, []string{poetryConfigAuthPrefix + configRepoName, username, password}) + return RunConfigCommand(project.Poetry, []string{poetryConfigAuthPrefix + configRepoName, username, password}) } func poetryUpdate() (err error) { diff --git a/artifactory/commands/python/python.go b/artifactory/commands/python/python.go index 140745d00..3a2a94709 100644 --- a/artifactory/commands/python/python.go +++ b/artifactory/commands/python/python.go @@ -178,7 +178,7 @@ func GetPypiRepoUrl(serverDetails *config.ServerDetails, repository string, isCu return rtUrl.String(), err } -func runConfigCommand(buildTool project.ProjectType, args []string) error { +func RunConfigCommand(buildTool project.ProjectType, args []string) error { log.Debug("Running", buildTool.String(), "config command...") configCmd := gofrogcmd.NewCommand(buildTool.String(), "config", args) if err := gofrogcmd.RunCmd(configCmd); err != nil { diff --git a/artifactory/commands/setup/setup.go b/artifactory/commands/setup/setup.go index b378a2b5b..ab9118255 100644 --- a/artifactory/commands/setup/setup.go +++ b/artifactory/commands/setup/setup.go @@ -21,6 +21,7 @@ import ( "github.com/jfrog/jfrog-client-go/utils/log" "golang.org/x/exp/maps" "net/url" + "os" "slices" ) @@ -181,7 +182,12 @@ func (sc *SetupCommand) configurePip() error { if err != nil { return err } - return pythoncommands.RunPipConfig(repoWithCredsUrl) + // If PIP_CONFIG_FILE is set, write the configuration to the custom config file manually. + // Using 'pip config set' native command is not supported together with PIP_CONFIG_FILE. + if customPipConfigPath := os.Getenv("PIP_CONFIG_FILE"); customPipConfigPath != "" { + return pythoncommands.CreatePipConfigManually(customPipConfigPath, repoWithCredsUrl) + } + return pythoncommands.RunConfigCommand(project.Pip, []string{"set", "global.index-url", repoWithCredsUrl}) } // configurePoetry configures Poetry to use the specified repository and authentication credentials.