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
Show file tree
Hide file tree
Changes from 77 commits
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
4 changes: 4 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ jobs:
with:
dotnet-version: '6.x'

- name: Install pnpm
uses: pnpm/action-setup@v4
with:
version: 9
sverdlov93 marked this conversation as resolved.
Show resolved Hide resolved
# Install Mono on Ubuntu to run nuget.exe (due to this issue on Ubuntu 24 that hasn't been fixed yet - https://github.com/NuGet/setup-nuget/issues/168)
- name: Install Mono on Ubuntu
if: matrix.os == 'ubuntu'
Expand Down
54 changes: 50 additions & 4 deletions artifactory/commands/dotnet/dotnetcommand.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"net/url"
"os"
"path"
"path/filepath"
"strings"
)

Expand Down Expand Up @@ -168,7 +169,7 @@ func changeWorkingDir(newWorkingDir string) (string, error) {
}

// Runs nuget/dotnet source add command
func AddSourceToNugetConfig(cmdType dotnet.ToolchainType, sourceUrl, user, password string) error {
func AddSourceToNugetConfig(cmdType dotnet.ToolchainType, sourceUrl, user, password, customConfigPath string) error {
cmd, err := dotnet.CreateDotnetAddSourceCmd(cmdType, sourceUrl)
if err != nil {
return err
Expand All @@ -178,15 +179,21 @@ func AddSourceToNugetConfig(cmdType dotnet.ToolchainType, sourceUrl, user, passw
cmd.CommandFlags = append(cmd.CommandFlags, flagPrefix+"name", SourceName)
cmd.CommandFlags = append(cmd.CommandFlags, flagPrefix+"username", user)
cmd.CommandFlags = append(cmd.CommandFlags, flagPrefix+"password", password)
Comment on lines 180 to 181
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be problematic if users execute this command in a CI environment. Consider concealing the credentials in the command, similar to how it's done here:
https://github.com/jfrog/build-info-go/blob/v1.10.8/utils/goutils.go#L52


if customConfigPath != "" {
addConfigFileFlag(cmd, customConfigPath)
}

stdOut, errorOut, _, err := frogio.RunCmdWithOutputParser(cmd, false)
if err != nil {
return fmt.Errorf("failed to add source: %w\n%s", err, strings.TrimSpace(stdOut+errorOut))
}
return nil
}

// Runs nuget/dotnet source remove command
func RemoveSourceFromNugetConfigIfExists(cmdType dotnet.ToolchainType) error {
// RemoveSourceFromNugetConfigIfExists runs the nuget/dotnet source remove command.
// Removes the source if it exists in the configuration.
func RemoveSourceFromNugetConfigIfExists(cmdType dotnet.ToolchainType, customConfigPath string) error {
cmd, err := dotnet.NewToolchainCmd(cmdType)
if err != nil {
return err
Expand All @@ -197,16 +204,55 @@ func RemoveSourceFromNugetConfigIfExists(cmdType dotnet.ToolchainType) error {
cmd.Command = append(cmd.Command, "sources", "remove")
cmd.CommandFlags = append(cmd.CommandFlags, "-name", SourceName)
}

if customConfigPath != "" {
addConfigFileFlag(cmd, customConfigPath)
}

stdOut, stdErr, _, err := frogio.RunCmdWithOutputParser(cmd, false)
if err != nil {
if strings.Contains(stdOut+stdErr, "Unable to find") {
if strings.Contains(stdOut+stdErr, "Unable to find") || strings.Contains(stdOut+stdErr, "does not exist") {
return nil
}
return fmt.Errorf("failed to remove source: %w\n%s", err, strings.TrimSpace(stdOut+stdErr))
sverdlov93 marked this conversation as resolved.
Show resolved Hide resolved
}
return nil
}

// GetConfigPathFromEnvIfProvided returns the path to the custom NuGet.Config file if it was provided by the user.
func GetConfigPathFromEnvIfProvided(cmdType dotnet.ToolchainType) string {
if cmdType == dotnet.DotnetCore {
if customDotnetDir := os.Getenv("DOTNET_CLI_HOME"); customDotnetDir != "" {
return filepath.Join(customDotnetDir, "NuGet.Config")
}
}
return os.Getenv("NUGET_CONFIG_FILE")
}

// CreateConfigFileIfNeeded creates a new config file if it does not exist.
func CreateConfigFileIfNeeded(customConfigPath string) error {
// Ensure the file exists, create an empty one it if not
if _, err := os.Stat(customConfigPath); os.IsNotExist(err) {
sverdlov93 marked this conversation as resolved.
Show resolved Hide resolved
if err = os.MkdirAll(filepath.Dir(customConfigPath), 0755); err != nil {
return err
}
// Write the default config content to the file
if err = os.WriteFile(customConfigPath, []byte("<configuration></configuration>"), 0644); err != nil {
return err
}
}
return nil
}

func addConfigFileFlag(cmd *dotnet.Cmd, configFilePath string) {
// Add the config file flag if needed.
if cmd.GetToolchain() == dotnet.DotnetCore {
cmd.CommandFlags = append(cmd.CommandFlags, "--configfile", configFilePath)
} else {
cmd.CommandFlags = append(cmd.CommandFlags, "-ConfigFile", configFilePath)
}
}

// Checks if the user provided input such as -configfile flag or -Source flag.
// If those flags were provided, NuGet will use the provided configs (default config file or the one with -configfile)
// If neither provided, we are initializing our own config.
Expand Down
127 changes: 127 additions & 0 deletions artifactory/commands/dotnet/dotnetcommand_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dotnet

import (
"github.com/jfrog/jfrog-cli-core/v2/utils/ioutils"
"os"
"path/filepath"
"reflect"
Expand Down Expand Up @@ -223,3 +224,129 @@ func createNewDotnetModule(t *testing.T, tmpDir string) *build.DotnetModule {
assert.NoError(t, err)
return module
}

func TestGetConfigPathFromEnvIfProvided(t *testing.T) {
testCases := []struct {
name string
mockEnv map[string]string
cmdType dotnet.ToolchainType
expectedPath string
}{
{
name: "DotnetCore with DOTNET_CLI_HOME",
mockEnv: map[string]string{
"DOTNET_CLI_HOME": "/custom/dotnet",
},
cmdType: dotnet.DotnetCore,
expectedPath: "/custom/dotnet/NuGet.Config",
},
{
name: "NuGet with NUGET_CONFIG_FILE",
mockEnv: map[string]string{
"NUGET_CONFIG_FILE": "/custom/nuget.config",
},
cmdType: dotnet.Nuget,
expectedPath: "/custom/nuget.config",
},
{
name: "No env variable",
mockEnv: map[string]string{},
cmdType: dotnet.Nuget,
expectedPath: "",
},
}

// Test the function with different environment variable settings
for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
restoreEnv := testsutils.SetEnvWithCallbackAndAssert(t, "DOTNET_CLI_HOME", testCase.mockEnv["DOTNET_CLI_HOME"])
defer restoreEnv()
sverdlov93 marked this conversation as resolved.
Show resolved Hide resolved

// Set other environment variables if needed
if testCase.mockEnv["NUGET_CONFIG_FILE"] != "" {
restoreEnvNuGet := testsutils.SetEnvWithCallbackAndAssert(t, "NUGET_CONFIG_FILE", testCase.mockEnv["NUGET_CONFIG_FILE"])
defer restoreEnvNuGet()
sverdlov93 marked this conversation as resolved.
Show resolved Hide resolved
}
result := GetConfigPathFromEnvIfProvided(testCase.cmdType)
assert.Equal(t, testCase.expectedPath, ioutils.WinToUnixPathSeparator(result))
})
}
}

func TestCreateConfigFileIfNeeded(t *testing.T) {
tests := []struct {
name string
configPath string
fileExists bool
expectedError error
}{
{
name: "File does not exist, create file with default content",
configPath: "/custom/path/NuGet.Config",
fileExists: false,
},
{
name: "File exists, no changes",
configPath: "/custom/path/NuGet.Config",
fileExists: true,
},
}

// Setup for testing file existence and creation
for _, tt := range tests {
sverdlov93 marked this conversation as resolved.
Show resolved Hide resolved
t.Run(tt.name, func(t *testing.T) {
configPath := filepath.Join(t.TempDir(), tt.configPath)
if tt.fileExists {
assert.NoError(t, os.MkdirAll(filepath.Dir(configPath), 0777))
sverdlov93 marked this conversation as resolved.
Show resolved Hide resolved
assert.NoError(t, os.WriteFile(configPath, []byte{}, 0644))
}
err := CreateConfigFileIfNeeded(configPath)
assert.NoError(t, err)

if !tt.fileExists {
// Read the content of the file
content, err := os.ReadFile(configPath)
assert.NoError(t, err)

// Assert the content is the default config content
assert.Equal(t, "<configuration></configuration>", string(content))
}
})
}
}

func TestAddConfigFileFlag(t *testing.T) {
tests := []struct {
name string
toolchainType dotnet.ToolchainType
configFilePath string
sverdlov93 marked this conversation as resolved.
Show resolved Hide resolved
expectedFlags []string
}{
{
name: "DotnetCore toolchain",
toolchainType: dotnet.DotnetCore,
configFilePath: "/path/to/NuGet.Config",
expectedFlags: []string{"--configfile", "/path/to/NuGet.Config"},
},
{
name: "NuGet toolchain",
toolchainType: dotnet.Nuget,
configFilePath: "/path/to/NuGet.Config",
expectedFlags: []string{"-ConfigFile", "/path/to/NuGet.Config"},
},
}

for _, tt := range tests {
sverdlov93 marked this conversation as resolved.
Show resolved Hide resolved
t.Run(tt.name, func(t *testing.T) {
// Create a mock command object
cmd, err := dotnet.NewToolchainCmd(tt.toolchainType)
assert.NoError(t, err)

// Call the function
addConfigFileFlag(cmd, tt.configFilePath)

// Assert that the flags are as expected
assert.Equal(t, tt.expectedFlags, cmd.CommandFlags)
})
}
}
12 changes: 12 additions & 0 deletions artifactory/commands/python/pip.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package python

import (
"fmt"
"io"
"os"
"os/exec"
"path/filepath"

"github.com/jfrog/build-info-go/entities"
"github.com/jfrog/build-info-go/utils/pythonutils"
Expand Down Expand Up @@ -46,6 +49,15 @@ func (pc *PipCommand) SetCommandName(commandName string) *PipCommand {
return pc
}

func CreatePipConfigManually(customPipConfigPath, repoWithCredsUrl string) error {
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 (pc *PipCommand) CommandName() string {
return "rt_python_pip"
}
Expand Down
27 changes: 27 additions & 0 deletions artifactory/commands/python/pip_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
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")
// #nosec G101 -- False positive - no hardcoded credentials.
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))
}
Loading
Loading