Skip to content

Commit 0960c2b

Browse files
committed
Add constant for altDeploymentRepository property
- Replace magic string 'altDeploymentRepository' with AltDeploymentRepositoryProperty constant - Improves code maintainability and follows Go best practices - All tests passing
1 parent 7e95a4f commit 0960c2b

File tree

1 file changed

+48
-43
lines changed

1 file changed

+48
-43
lines changed

artifactory/utils/maven/settingsxml.go

Lines changed: 48 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,16 @@ import (
1111
"github.com/apache/camel-k/v2/pkg/util/maven"
1212
)
1313

14-
// ArtifactoryMirrorID is the ID used for the Artifactory mirror.
15-
const ArtifactoryMirrorID = "artifactory-mirror"
14+
const (
15+
// ArtifactoryMirrorID is the ID used for the Artifactory mirror.
16+
ArtifactoryMirrorID = "artifactory-mirror"
1617

17-
// ArtifactoryDeployProfileID is the ID used for the Artifactory deployment profile.
18-
const ArtifactoryDeployProfileID = "artifactory-deploy"
18+
// ArtifactoryDeployProfileID is the ID used for the Artifactory deployment profile.
19+
ArtifactoryDeployProfileID = "artifactory-deploy"
20+
21+
// AltDeploymentRepositoryProperty is the Maven property for overriding deployment repository.
22+
AltDeploymentRepositoryProperty = "altDeploymentRepository"
23+
)
1924

2025
// SettingsXmlManager manages the maven settings file (`settings.xml`).
2126
type SettingsXmlManager struct {
@@ -43,43 +48,6 @@ func NewSettingsXmlManager() (*SettingsXmlManager, error) {
4348
return manager, nil
4449
}
4550

46-
// ConfigureArtifactoryRepository configures both downloading and deployment to Artifactory
47-
// This is the main public API that sets up complete Artifactory integration using the same repository
48-
// for both download (via mirrors) and deployment (via altDeploymentRepository)
49-
func (sxm *SettingsXmlManager) ConfigureArtifactoryRepository(artifactoryUrl, repoName, username, password string) error {
50-
// Load settings once at the beginning
51-
err := sxm.loadSettings()
52-
if err != nil {
53-
return fmt.Errorf("failed to load settings: %w", err)
54-
}
55-
56-
// Build repository URL once for both mirror and deployment
57-
repoUrl := strings.TrimRight(artifactoryUrl, "/") + "/" + repoName
58-
59-
// Set server credentials once (used by both mirror and deployment)
60-
if username != "" && password != "" {
61-
err = sxm.updateServerCredentials(username, password)
62-
if err != nil {
63-
return fmt.Errorf("failed to configure server credentials: %w", err)
64-
}
65-
}
66-
67-
// Configure download mirror (without credentials)
68-
err = sxm.configureArtifactoryMirror(repoUrl, repoName)
69-
if err != nil {
70-
return fmt.Errorf("failed to configure Artifactory download mirror: %w", err)
71-
}
72-
73-
// Configure deployment to the same repository (without credentials)
74-
err = sxm.configureArtifactoryDeployment(repoUrl)
75-
if err != nil {
76-
return fmt.Errorf("failed to configure Artifactory deployment: %w", err)
77-
}
78-
79-
// Write settings once at the end
80-
return sxm.writeSettingsToFile()
81-
}
82-
8351
// loadSettings reads the settings.xml file and unmarshals it into the Settings struct.
8452
func (sxm *SettingsXmlManager) loadSettings() error {
8553
file, err := os.ReadFile(sxm.path)
@@ -120,7 +88,7 @@ func (sxm *SettingsXmlManager) configureArtifactoryDeployment(repoUrl string) er
12088
deployProfile := maven.Profile{
12189
ID: ArtifactoryDeployProfileID,
12290
Properties: &mavenv1.Properties{
123-
"altDeploymentRepository": altDeploymentRepo,
91+
AltDeploymentRepositoryProperty: altDeploymentRepo,
12492
},
12593
Activation: &maven.Activation{
12694
ActiveByDefault: true, // Auto-activate this profile
@@ -136,7 +104,7 @@ func (sxm *SettingsXmlManager) configureArtifactoryDeployment(repoUrl string) er
136104
profile.Properties = &mavenv1.Properties{}
137105
}
138106
// Set/update only our deployment property, preserve others
139-
(*profile.Properties)["altDeploymentRepository"] = altDeploymentRepo
107+
(*profile.Properties)[AltDeploymentRepositoryProperty] = altDeploymentRepo
140108

141109
// Set activation if not already set
142110
if profile.Activation == nil {
@@ -161,6 +129,43 @@ func (sxm *SettingsXmlManager) configureArtifactoryDeployment(repoUrl string) er
161129
return nil
162130
}
163131

132+
// ConfigureArtifactoryRepository configures both downloading and deployment to Artifactory
133+
// This is the main public API that sets up complete Artifactory integration using the same repository
134+
// for both download (via mirrors) and deployment (via altDeploymentRepository)
135+
func (sxm *SettingsXmlManager) ConfigureArtifactoryRepository(artifactoryUrl, repoName, username, password string) error {
136+
// Load settings once at the beginning
137+
err := sxm.loadSettings()
138+
if err != nil {
139+
return fmt.Errorf("failed to load settings: %w", err)
140+
}
141+
142+
// Build repository URL once for both mirror and deployment
143+
repoUrl := strings.TrimRight(artifactoryUrl, "/") + "/" + repoName
144+
145+
// Set server credentials once (used by both mirror and deployment)
146+
if username != "" && password != "" {
147+
err = sxm.updateServerCredentials(username, password)
148+
if err != nil {
149+
return fmt.Errorf("failed to configure server credentials: %w", err)
150+
}
151+
}
152+
153+
// Configure download mirror (without credentials)
154+
err = sxm.configureArtifactoryMirror(repoUrl, repoName)
155+
if err != nil {
156+
return fmt.Errorf("failed to configure Artifactory download mirror: %w", err)
157+
}
158+
159+
// Configure deployment to the same repository (without credentials)
160+
err = sxm.configureArtifactoryDeployment(repoUrl)
161+
if err != nil {
162+
return fmt.Errorf("failed to configure Artifactory deployment: %w", err)
163+
}
164+
165+
// Write settings once at the end
166+
return sxm.writeSettingsToFile()
167+
}
168+
164169
// updateMirror finds the existing mirror or creates a new one and updates it with the provided details.
165170
func (sxm *SettingsXmlManager) updateMirror(repoUrl, repoName string) error {
166171
// Create the new mirror with the provided details

0 commit comments

Comments
 (0)