Skip to content

Commit

Permalink
Setup command improvments (#1332)
Browse files Browse the repository at this point in the history
  • Loading branch information
sverdlov93 authored Jan 29, 2025
1 parent 4458626 commit d5f4218
Show file tree
Hide file tree
Showing 6 changed files with 303 additions and 38 deletions.
6 changes: 4 additions & 2 deletions artifactory/commands/utils/npmcmdutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,10 @@ func GetNpmAuthKeyValue(serverDetails *config.ServerDetails, repoUrl string) (ke
default:
return "", ""
}

return fmt.Sprintf("//%s:%s", strings.TrimPrefix(repoUrl, "https://"), keySuffix), value
// Parse the URL to remove the scheme (https:// or http://)
urlWithoutScheme := strings.TrimPrefix(repoUrl, "https://")
urlWithoutScheme = strings.TrimPrefix(urlWithoutScheme, "http://")
return fmt.Sprintf("//%s:%s", urlWithoutScheme, keySuffix), value
}

// basicAuthBase64Encode encodes user credentials in Base64 for basic authentication.
Expand Down
1 change: 0 additions & 1 deletion artifactory/utils/container/containermanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,6 @@ func ContainerManagerLogin(imageRegistry string, config *ContainerManagerLoginCo
password := config.ServerDetails.Password
// If access-token exists, perform login with it.
if config.ServerDetails.AccessToken != "" {
log.Debug("Using access-token details in " + containerManager.String() + "-login command.")
if username == "" {
username = auth.ExtractUsernameFromAccessToken(config.ServerDetails.AccessToken)
}
Expand Down
160 changes: 160 additions & 0 deletions artifactory/utils/maven/settingsxml.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
package maven

import (
"encoding/xml"
"fmt"
mavenv1 "github.com/apache/camel-k/v2/pkg/apis/camel/v1"
"github.com/apache/camel-k/v2/pkg/util/maven"
"os"
"path/filepath"
"strings"
)

// ArtifactoryMirrorID is the ID used for the Artifactory mirror.
const ArtifactoryMirrorID = "artifactory-mirror"

// SettingsXmlManager manages the maven settings file (`settings.xml`).
type SettingsXmlManager struct {
path string
settings maven.Settings
}

// NewSettingsXmlManager creates a new SettingsXmlManager instance.
// It automatically loads the existing settings from the `settings.xml` file if it exists.
func NewSettingsXmlManager() (*SettingsXmlManager, error) {
homeDir, err := os.UserHomeDir()
if err != nil {
return nil, fmt.Errorf("failed to get user home directory: %w", err)
}
manager := &SettingsXmlManager{
path: filepath.Join(homeDir, ".m2", "settings.xml"),
}

// Load existing settings from file
err = manager.loadSettings()
if err != nil {
return nil, fmt.Errorf("failed to load settings from %s: %w", manager.path, err)
}

return manager, nil
}

// loadSettings reads the settings.xml file and unmarshals it into the Settings struct.
func (sxm *SettingsXmlManager) loadSettings() error {
file, err := os.ReadFile(sxm.path)
if err != nil {
if os.IsNotExist(err) {
// If file does not exist, initialize with empty settings
sxm.settings = maven.Settings{
XMLName: xml.Name{Local: "settings"},
}
return nil
}
return fmt.Errorf("failed to read settings file %s: %w", sxm.path, err)
}

// Unmarshal the file contents into the settings
err = xml.Unmarshal(file, &sxm.settings)
if err != nil {
return fmt.Errorf("failed to unmarshal settings from file %s: %w", sxm.path, err)
}
return nil
}

// ConfigureArtifactoryMirror updates or adds the Artifactory mirror and its credentials in the settings.
func (sxm *SettingsXmlManager) ConfigureArtifactoryMirror(artifactoryUrl, repoName, username, password string) error {
// Find or create the mirror and update it with the provided details
if err := sxm.updateMirror(artifactoryUrl, repoName); err != nil {
return err
}

// Update server credentials if needed
if username != "" && password != "" {
if err := sxm.updateServerCredentials(username, password); err != nil {
return err
}
}

// Write the updated settings back to the settings.xml file
return sxm.writeSettingsToFile()
}

// updateMirror finds the existing mirror or creates a new one and updates it with the provided details.
func (sxm *SettingsXmlManager) updateMirror(artifactoryUrl, repoName string) error {
// Create the new mirror with the provided details
updatedMirror := maven.Mirror{
ID: ArtifactoryMirrorID,
Name: repoName,
MirrorOf: "*",
URL: strings.TrimSuffix(artifactoryUrl, "/") + "/" + repoName,
}

// Find if the mirror already exists
var foundMirror bool
for i, mirror := range sxm.settings.Mirrors {
if mirror.ID == ArtifactoryMirrorID {
// Override the existing mirror with the updated one
sxm.settings.Mirrors[i] = updatedMirror
foundMirror = true
break
}
}

// If the mirror doesn't exist, add it
if !foundMirror {
sxm.settings.Mirrors = append(sxm.settings.Mirrors, updatedMirror)
}

return nil
}

// updateServerCredentials updates or adds server credentials in the settings.
func (sxm *SettingsXmlManager) updateServerCredentials(username, password string) error {
// Create the new server with the provided credentials
updatedServer := mavenv1.Server{
ID: ArtifactoryMirrorID,
Username: username,
Password: password,
}

// Find if the server already exists
var foundServer bool
for i, s := range sxm.settings.Servers {
if s.ID == ArtifactoryMirrorID {
// Override the existing server with the updated one
sxm.settings.Servers[i] = updatedServer
foundServer = true
break
}
}

// If the server doesn't exist, add it
if !foundServer {
sxm.settings.Servers = append(sxm.settings.Servers, updatedServer)
}

return nil
}

// writeSettingsToFile writes the updated settings to the settings.xml file.
func (sxm *SettingsXmlManager) writeSettingsToFile() error {
// Marshal the updated settings back to XML
data, err := xml.MarshalIndent(&sxm.settings, "", " ")
if err != nil {
return fmt.Errorf("failed to marshal settings to XML: %w", err)
}

// Add XML header and write to file
data = append([]byte(xml.Header), data...)
err = os.MkdirAll(filepath.Dir(sxm.path), 0755)
if err != nil {
return fmt.Errorf("failed to create directory for settings file: %w", err)
}

err = os.WriteFile(sxm.path, data, 0644)
if err != nil {
return fmt.Errorf("failed to write settings to file %s: %w", sxm.path, err)
}

return nil
}
6 changes: 6 additions & 0 deletions artifactory/utils/repositoryutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ package utils
import (
"github.com/jfrog/gofrog/datastructures"
"github.com/jfrog/jfrog-cli-core/v2/utils/config"
"github.com/jfrog/jfrog-cli-core/v2/utils/coreutils"
"github.com/jfrog/jfrog-cli-core/v2/utils/ioutils"
"github.com/jfrog/jfrog-client-go/utils/log"
"golang.org/x/exp/slices"
"os"
"path"
"strings"

Expand Down Expand Up @@ -100,6 +103,9 @@ func SelectRepositoryInteractively(serverDetails *config.ServerDetails, repoFilt
// Automatically select the repository if only one exists.
return filteredRepos[0], nil
}
if !log.IsStdOutTerminal() || strings.ToLower(os.Getenv(coreutils.CI)) == "true" {
return "", errorutils.CheckErrorf("multiple repositories were found that match the following criteria: %v. Please provide the repository name using '--repo' flag.", repoFilterParams)
}
// Prompt the user to select a repository.
return ioutils.AskFromListWithMismatchConfirmation(promptMessage, "Repository not found.", ioutils.ConvertToSuggests(filteredRepos)), nil
}
Expand Down
40 changes: 30 additions & 10 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
module github.com/jfrog/jfrog-cli-core/v2

go 1.23.4
go 1.23.5

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)

require (
github.com/apache/camel-k/v2 v2.5.0
github.com/buger/jsonparser v1.1.1
github.com/chzyer/readline v1.5.1
github.com/forPelevin/gomoji v1.2.0
Expand All @@ -22,7 +23,7 @@ require (
github.com/stretchr/testify v1.10.0
github.com/urfave/cli v1.22.16
github.com/vbauerster/mpb/v8 v8.9.1
golang.org/x/exp v0.0.0-20250106191152-7588d65b2ba8
golang.org/x/exp v0.0.0-20250128182459-e0ece0dbea4c
golang.org/x/sync v0.10.0
golang.org/x/term v0.28.0
golang.org/x/text v0.21.0
Expand All @@ -40,30 +41,38 @@ require (
github.com/andybalholm/brotli v1.1.0 // indirect
github.com/cloudflare/circl v1.3.7 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.5 // indirect
github.com/cyphar/filepath-securejoin v0.2.5 // indirect
github.com/cyphar/filepath-securejoin v0.2.4 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/dsnet/compress v0.0.1 // indirect
github.com/emirpasic/gods v1.18.1 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 // indirect
github.com/go-git/go-billy/v5 v5.6.0 // indirect
github.com/go-git/go-git/v5 v5.13.0 // indirect
github.com/go-git/go-billy/v5 v5.5.0 // indirect
github.com/go-git/go-git/v5 v5.12.0 // indirect
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-logr/zapr v1.3.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang-jwt/jwt/v4 v4.5.1 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/imdario/mergo v0.3.16 // indirect
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
github.com/jfrog/archiver/v3 v3.6.1 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/kevinburke/ssh_config v1.2.0 // indirect
github.com/klauspost/compress v1.17.9 // indirect
github.com/klauspost/cpuid/v2 v2.2.3 // indirect
github.com/klauspost/pgzip v1.2.6 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-isatty v0.0.17 // indirect
github.com/mattn/go-runewidth v0.0.16 // indirect
github.com/mattn/go-tty v0.0.3 // indirect
github.com/minio/sha256-simd v1.0.1 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/nwaples/rardecode v1.1.3 // indirect
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
github.com/pierrec/lz4/v4 v4.1.21 // indirect
Expand All @@ -75,28 +84,39 @@ require (
github.com/sagikazarmark/locafero v0.4.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 // indirect
github.com/skeema/knownhosts v1.3.0 // indirect
github.com/skeema/knownhosts v1.2.2 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/spf13/afero v1.11.0 // indirect
github.com/spf13/cast v1.6.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/stoewer/go-strcase v1.3.0 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
github.com/ulikunitz/xz v0.5.12 // indirect
github.com/xanzy/ssh-agent v0.3.3 // indirect
github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8 // indirect
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
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.27.0 // indirect
golang.org/x/crypto v0.32.0 // indirect
golang.org/x/mod v0.22.0 // indirect
golang.org/x/net v0.34.0 // indirect
golang.org/x/sys v0.29.0 // indirect
golang.org/x/tools v0.29.0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
k8s.io/api v0.29.7 // indirect
k8s.io/apimachinery v0.29.7 // indirect
k8s.io/klog/v2 v2.130.1 // indirect
k8s.io/utils v0.0.0-20240310230437-4693a0247e57 // indirect
sigs.k8s.io/controller-runtime v0.17.5 // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect
sigs.k8s.io/yaml v1.4.0 // indirect
)

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

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

Expand Down
Loading

0 comments on commit d5f4218

Please sign in to comment.