Skip to content

Commit d5f4218

Browse files
authored
Setup command improvments (#1332)
1 parent 4458626 commit d5f4218

File tree

6 files changed

+303
-38
lines changed

6 files changed

+303
-38
lines changed

artifactory/commands/utils/npmcmdutils.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,10 @@ func GetNpmAuthKeyValue(serverDetails *config.ServerDetails, repoUrl string) (ke
114114
default:
115115
return "", ""
116116
}
117-
118-
return fmt.Sprintf("//%s:%s", strings.TrimPrefix(repoUrl, "https://"), keySuffix), value
117+
// Parse the URL to remove the scheme (https:// or http://)
118+
urlWithoutScheme := strings.TrimPrefix(repoUrl, "https://")
119+
urlWithoutScheme = strings.TrimPrefix(urlWithoutScheme, "http://")
120+
return fmt.Sprintf("//%s:%s", urlWithoutScheme, keySuffix), value
119121
}
120122

121123
// basicAuthBase64Encode encodes user credentials in Base64 for basic authentication.

artifactory/utils/container/containermanager.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,6 @@ func ContainerManagerLogin(imageRegistry string, config *ContainerManagerLoginCo
194194
password := config.ServerDetails.Password
195195
// If access-token exists, perform login with it.
196196
if config.ServerDetails.AccessToken != "" {
197-
log.Debug("Using access-token details in " + containerManager.String() + "-login command.")
198197
if username == "" {
199198
username = auth.ExtractUsernameFromAccessToken(config.ServerDetails.AccessToken)
200199
}
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
package maven
2+
3+
import (
4+
"encoding/xml"
5+
"fmt"
6+
mavenv1 "github.com/apache/camel-k/v2/pkg/apis/camel/v1"
7+
"github.com/apache/camel-k/v2/pkg/util/maven"
8+
"os"
9+
"path/filepath"
10+
"strings"
11+
)
12+
13+
// ArtifactoryMirrorID is the ID used for the Artifactory mirror.
14+
const ArtifactoryMirrorID = "artifactory-mirror"
15+
16+
// SettingsXmlManager manages the maven settings file (`settings.xml`).
17+
type SettingsXmlManager struct {
18+
path string
19+
settings maven.Settings
20+
}
21+
22+
// NewSettingsXmlManager creates a new SettingsXmlManager instance.
23+
// It automatically loads the existing settings from the `settings.xml` file if it exists.
24+
func NewSettingsXmlManager() (*SettingsXmlManager, error) {
25+
homeDir, err := os.UserHomeDir()
26+
if err != nil {
27+
return nil, fmt.Errorf("failed to get user home directory: %w", err)
28+
}
29+
manager := &SettingsXmlManager{
30+
path: filepath.Join(homeDir, ".m2", "settings.xml"),
31+
}
32+
33+
// Load existing settings from file
34+
err = manager.loadSettings()
35+
if err != nil {
36+
return nil, fmt.Errorf("failed to load settings from %s: %w", manager.path, err)
37+
}
38+
39+
return manager, nil
40+
}
41+
42+
// loadSettings reads the settings.xml file and unmarshals it into the Settings struct.
43+
func (sxm *SettingsXmlManager) loadSettings() error {
44+
file, err := os.ReadFile(sxm.path)
45+
if err != nil {
46+
if os.IsNotExist(err) {
47+
// If file does not exist, initialize with empty settings
48+
sxm.settings = maven.Settings{
49+
XMLName: xml.Name{Local: "settings"},
50+
}
51+
return nil
52+
}
53+
return fmt.Errorf("failed to read settings file %s: %w", sxm.path, err)
54+
}
55+
56+
// Unmarshal the file contents into the settings
57+
err = xml.Unmarshal(file, &sxm.settings)
58+
if err != nil {
59+
return fmt.Errorf("failed to unmarshal settings from file %s: %w", sxm.path, err)
60+
}
61+
return nil
62+
}
63+
64+
// ConfigureArtifactoryMirror updates or adds the Artifactory mirror and its credentials in the settings.
65+
func (sxm *SettingsXmlManager) ConfigureArtifactoryMirror(artifactoryUrl, repoName, username, password string) error {
66+
// Find or create the mirror and update it with the provided details
67+
if err := sxm.updateMirror(artifactoryUrl, repoName); err != nil {
68+
return err
69+
}
70+
71+
// Update server credentials if needed
72+
if username != "" && password != "" {
73+
if err := sxm.updateServerCredentials(username, password); err != nil {
74+
return err
75+
}
76+
}
77+
78+
// Write the updated settings back to the settings.xml file
79+
return sxm.writeSettingsToFile()
80+
}
81+
82+
// updateMirror finds the existing mirror or creates a new one and updates it with the provided details.
83+
func (sxm *SettingsXmlManager) updateMirror(artifactoryUrl, repoName string) error {
84+
// Create the new mirror with the provided details
85+
updatedMirror := maven.Mirror{
86+
ID: ArtifactoryMirrorID,
87+
Name: repoName,
88+
MirrorOf: "*",
89+
URL: strings.TrimSuffix(artifactoryUrl, "/") + "/" + repoName,
90+
}
91+
92+
// Find if the mirror already exists
93+
var foundMirror bool
94+
for i, mirror := range sxm.settings.Mirrors {
95+
if mirror.ID == ArtifactoryMirrorID {
96+
// Override the existing mirror with the updated one
97+
sxm.settings.Mirrors[i] = updatedMirror
98+
foundMirror = true
99+
break
100+
}
101+
}
102+
103+
// If the mirror doesn't exist, add it
104+
if !foundMirror {
105+
sxm.settings.Mirrors = append(sxm.settings.Mirrors, updatedMirror)
106+
}
107+
108+
return nil
109+
}
110+
111+
// updateServerCredentials updates or adds server credentials in the settings.
112+
func (sxm *SettingsXmlManager) updateServerCredentials(username, password string) error {
113+
// Create the new server with the provided credentials
114+
updatedServer := mavenv1.Server{
115+
ID: ArtifactoryMirrorID,
116+
Username: username,
117+
Password: password,
118+
}
119+
120+
// Find if the server already exists
121+
var foundServer bool
122+
for i, s := range sxm.settings.Servers {
123+
if s.ID == ArtifactoryMirrorID {
124+
// Override the existing server with the updated one
125+
sxm.settings.Servers[i] = updatedServer
126+
foundServer = true
127+
break
128+
}
129+
}
130+
131+
// If the server doesn't exist, add it
132+
if !foundServer {
133+
sxm.settings.Servers = append(sxm.settings.Servers, updatedServer)
134+
}
135+
136+
return nil
137+
}
138+
139+
// writeSettingsToFile writes the updated settings to the settings.xml file.
140+
func (sxm *SettingsXmlManager) writeSettingsToFile() error {
141+
// Marshal the updated settings back to XML
142+
data, err := xml.MarshalIndent(&sxm.settings, "", " ")
143+
if err != nil {
144+
return fmt.Errorf("failed to marshal settings to XML: %w", err)
145+
}
146+
147+
// Add XML header and write to file
148+
data = append([]byte(xml.Header), data...)
149+
err = os.MkdirAll(filepath.Dir(sxm.path), 0755)
150+
if err != nil {
151+
return fmt.Errorf("failed to create directory for settings file: %w", err)
152+
}
153+
154+
err = os.WriteFile(sxm.path, data, 0644)
155+
if err != nil {
156+
return fmt.Errorf("failed to write settings to file %s: %w", sxm.path, err)
157+
}
158+
159+
return nil
160+
}

artifactory/utils/repositoryutils.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@ package utils
33
import (
44
"github.com/jfrog/gofrog/datastructures"
55
"github.com/jfrog/jfrog-cli-core/v2/utils/config"
6+
"github.com/jfrog/jfrog-cli-core/v2/utils/coreutils"
67
"github.com/jfrog/jfrog-cli-core/v2/utils/ioutils"
8+
"github.com/jfrog/jfrog-client-go/utils/log"
79
"golang.org/x/exp/slices"
10+
"os"
811
"path"
912
"strings"
1013

@@ -100,6 +103,9 @@ func SelectRepositoryInteractively(serverDetails *config.ServerDetails, repoFilt
100103
// Automatically select the repository if only one exists.
101104
return filteredRepos[0], nil
102105
}
106+
if !log.IsStdOutTerminal() || strings.ToLower(os.Getenv(coreutils.CI)) == "true" {
107+
return "", errorutils.CheckErrorf("multiple repositories were found that match the following criteria: %v. Please provide the repository name using '--repo' flag.", repoFilterParams)
108+
}
103109
// Prompt the user to select a repository.
104110
return ioutils.AskFromListWithMismatchConfirmation(promptMessage, "Repository not found.", ioutils.ConvertToSuggests(filteredRepos)), nil
105111
}

go.mod

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
module github.com/jfrog/jfrog-cli-core/v2
22

3-
go 1.23.4
3+
go 1.23.5
44

55
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)
66

77
require (
8+
github.com/apache/camel-k/v2 v2.5.0
89
github.com/buger/jsonparser v1.1.1
910
github.com/chzyer/readline v1.5.1
1011
github.com/forPelevin/gomoji v1.2.0
@@ -22,7 +23,7 @@ require (
2223
github.com/stretchr/testify v1.10.0
2324
github.com/urfave/cli v1.22.16
2425
github.com/vbauerster/mpb/v8 v8.9.1
25-
golang.org/x/exp v0.0.0-20250106191152-7588d65b2ba8
26+
golang.org/x/exp v0.0.0-20250128182459-e0ece0dbea4c
2627
golang.org/x/sync v0.10.0
2728
golang.org/x/term v0.28.0
2829
golang.org/x/text v0.21.0
@@ -40,30 +41,38 @@ require (
4041
github.com/andybalholm/brotli v1.1.0 // indirect
4142
github.com/cloudflare/circl v1.3.7 // indirect
4243
github.com/cpuguy83/go-md2man/v2 v2.0.5 // indirect
43-
github.com/cyphar/filepath-securejoin v0.2.5 // indirect
44+
github.com/cyphar/filepath-securejoin v0.2.4 // indirect
4445
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
4546
github.com/dsnet/compress v0.0.1 // indirect
4647
github.com/emirpasic/gods v1.18.1 // indirect
4748
github.com/fsnotify/fsnotify v1.7.0 // indirect
4849
github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 // indirect
49-
github.com/go-git/go-billy/v5 v5.6.0 // indirect
50-
github.com/go-git/go-git/v5 v5.13.0 // indirect
50+
github.com/go-git/go-billy/v5 v5.5.0 // indirect
51+
github.com/go-git/go-git/v5 v5.12.0 // indirect
52+
github.com/go-logr/logr v1.4.2 // indirect
53+
github.com/go-logr/zapr v1.3.0 // indirect
54+
github.com/gogo/protobuf v1.3.2 // indirect
5155
github.com/golang-jwt/jwt/v4 v4.5.1 // indirect
5256
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
5357
github.com/golang/snappy v0.0.4 // indirect
58+
github.com/google/gofuzz v1.2.0 // indirect
5459
github.com/hashicorp/hcl v1.0.0 // indirect
60+
github.com/imdario/mergo v0.3.16 // indirect
5561
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
5662
github.com/jfrog/archiver/v3 v3.6.1 // indirect
63+
github.com/json-iterator/go v1.1.12 // indirect
5764
github.com/kevinburke/ssh_config v1.2.0 // indirect
5865
github.com/klauspost/compress v1.17.9 // indirect
5966
github.com/klauspost/cpuid/v2 v2.2.3 // indirect
6067
github.com/klauspost/pgzip v1.2.6 // indirect
6168
github.com/mattn/go-colorable v0.1.13 // indirect
62-
github.com/mattn/go-isatty v0.0.20 // indirect
69+
github.com/mattn/go-isatty v0.0.17 // indirect
6370
github.com/mattn/go-runewidth v0.0.16 // indirect
6471
github.com/mattn/go-tty v0.0.3 // indirect
6572
github.com/minio/sha256-simd v1.0.1 // indirect
6673
github.com/mitchellh/mapstructure v1.5.0 // indirect
74+
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
75+
github.com/modern-go/reflect2 v1.0.2 // indirect
6776
github.com/nwaples/rardecode v1.1.3 // indirect
6877
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
6978
github.com/pierrec/lz4/v4 v4.1.21 // indirect
@@ -75,28 +84,39 @@ require (
7584
github.com/sagikazarmark/locafero v0.4.0 // indirect
7685
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
7786
github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 // indirect
78-
github.com/skeema/knownhosts v1.3.0 // indirect
87+
github.com/skeema/knownhosts v1.2.2 // indirect
7988
github.com/sourcegraph/conc v0.3.0 // indirect
8089
github.com/spf13/afero v1.11.0 // indirect
8190
github.com/spf13/cast v1.6.0 // indirect
8291
github.com/spf13/pflag v1.0.5 // indirect
92+
github.com/stoewer/go-strcase v1.3.0 // indirect
8393
github.com/subosito/gotenv v1.6.0 // indirect
8494
github.com/ulikunitz/xz v0.5.12 // indirect
8595
github.com/xanzy/ssh-agent v0.3.3 // indirect
8696
github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8 // indirect
8797
github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778 // indirect
88-
go.uber.org/atomic v1.9.0 // indirect
89-
go.uber.org/multierr v1.9.0 // indirect
98+
go.uber.org/multierr v1.11.0 // indirect
99+
go.uber.org/zap v1.27.0 // indirect
90100
golang.org/x/crypto v0.32.0 // indirect
91101
golang.org/x/mod v0.22.0 // indirect
92102
golang.org/x/net v0.34.0 // indirect
93103
golang.org/x/sys v0.29.0 // indirect
94104
golang.org/x/tools v0.29.0 // indirect
105+
gopkg.in/inf.v0 v0.9.1 // indirect
95106
gopkg.in/ini.v1 v1.67.0 // indirect
96107
gopkg.in/warnings.v0 v0.1.2 // indirect
108+
gopkg.in/yaml.v2 v2.4.0 // indirect
109+
k8s.io/api v0.29.7 // indirect
110+
k8s.io/apimachinery v0.29.7 // indirect
111+
k8s.io/klog/v2 v2.130.1 // indirect
112+
k8s.io/utils v0.0.0-20240310230437-4693a0247e57 // indirect
113+
sigs.k8s.io/controller-runtime v0.17.5 // indirect
114+
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
115+
sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect
116+
sigs.k8s.io/yaml v1.4.0 // indirect
97117
)
98118

99-
// replace github.com/jfrog/jfrog-client-go => github.com/jfrog/jfrog-client-go v1.28.1-0.20241225183733-80a5e1ba7a2c
119+
replace github.com/jfrog/jfrog-client-go => github.com/jfrog/jfrog-client-go v1.28.1-0.20250126110945-81abbdde452f
100120

101121
// replace github.com/jfrog/build-info-go => github.com/jfrog/build-info-go v1.8.9-0.20241121100855-e7a75ceee2bd
102122

0 commit comments

Comments
 (0)