Skip to content

Commit 407ca04

Browse files
committed
Increase code duplication
Signed-off-by: David Gageot <[email protected]>
1 parent c19ace2 commit 407ca04

File tree

3 files changed

+33
-24
lines changed

3 files changed

+33
-24
lines changed

pkg/skaffold/jib/jib_gradle.go

+12-6
Original file line numberDiff line numberDiff line change
@@ -42,22 +42,28 @@ func GetDependenciesGradle(ctx context.Context, workspace string, a *latest.JibG
4242
}
4343

4444
func getCommandGradle(ctx context.Context, workspace string, a *latest.JibGradleArtifact) *exec.Cmd {
45-
args := []string{"_jibSkaffoldFiles", "-q"}
46-
if a.Project != "" {
45+
task := "_jibSkaffoldFiles"
46+
47+
var command string
48+
if a.Project == "" {
49+
command = task
50+
} else {
4751
// multi-module
48-
args[0] = fmt.Sprintf(":%s:%s", a.Project, args[0])
52+
command = fmt.Sprintf(":%s:%s", a.Project, task)
4953
}
54+
args := []string{command, "-q"}
55+
5056
return GradleCommand.CreateCommand(ctx, workspace, args)
5157
}
5258

5359
// GenerateGradleArgs generates the arguments to Gradle for building the project as an image.
54-
func GenerateGradleArgs(task string, imageName string, artifact *latest.JibGradleArtifact) []string {
60+
func GenerateGradleArgs(task string, imageName string, a *latest.JibGradleArtifact) []string {
5561
var command string
56-
if artifact.Project == "" {
62+
if a.Project == "" {
5763
command = ":" + task
5864
} else {
5965
// multi-module
60-
command = fmt.Sprintf(":%s:%s", artifact.Project, task)
66+
command = fmt.Sprintf(":%s:%s", a.Project, task)
6167
}
6268

6369
return []string{command, "--image=" + imageName}

pkg/skaffold/jib/jib_maven.go

+17-14
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,11 @@ func GetDependenciesMaven(ctx context.Context, workspace string, a *latest.JibMa
4040
}
4141

4242
func getCommandMaven(ctx context.Context, workspace string, a *latest.JibMavenArtifact) *exec.Cmd {
43-
args := []string{"--quiet"}
43+
var args []string
44+
args = append(args, "--quiet")
45+
if a.Profile != "" {
46+
args = append(args, "--activate-profiles", a.Profile)
47+
}
4448
if a.Module == "" {
4549
// single-module project
4650
args = append(args, "--non-recursive")
@@ -49,27 +53,26 @@ func getCommandMaven(ctx context.Context, workspace string, a *latest.JibMavenAr
4953
args = append(args, "--projects", a.Module, "--also-make")
5054
}
5155
args = append(args, "jib:_skaffold-files")
52-
if a.Profile != "" {
53-
args = append(args, "--activate-profiles", a.Profile)
54-
}
5556

5657
return MavenCommand.CreateCommand(ctx, workspace, args)
5758
}
5859

5960
// GenerateMavenArgs generates the arguments to Maven for building the project as an image.
60-
func GenerateMavenArgs(goal string, imageName string, artifact *latest.JibMavenArtifact) []string {
61-
var command []string
62-
if artifact.Module == "" {
61+
func GenerateMavenArgs(goal string, imageName string, a *latest.JibMavenArtifact) []string {
62+
var args []string
63+
if a.Profile != "" {
64+
args = append(args, "--activate-profiles", a.Profile)
65+
}
66+
if a.Module == "" {
6367
// single-module project
64-
command = []string{"--non-recursive", "prepare-package", "jib:" + goal}
68+
args = append(args, "--non-recursive")
69+
args = append(args, "prepare-package", "jib:"+goal)
6570
} else {
6671
// multi-module project: we assume `package` is bound to `jib:<goal>`
67-
command = []string{"--projects", artifact.Module, "--also-make", "package"}
68-
}
69-
command = append(command, "-Dimage="+imageName)
70-
if artifact.Profile != "" {
71-
command = append(command, "--activate-profiles", artifact.Profile)
72+
args = append(args, "--projects", a.Module, "--also-make")
73+
args = append(args, "package")
7274
}
75+
args = append(args, "-Dimage="+imageName)
7376

74-
return command
77+
return args
7578
}

pkg/skaffold/jib/jib_maven_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ func TestGetCommandMaven(t *testing.T) {
104104
jibMavenArtifact: latest.JibMavenArtifact{Profile: "profile"},
105105
filesInWorkspace: []string{},
106106
expectedCmd: func(workspace string) *exec.Cmd {
107-
return MavenCommand.CreateCommand(ctx, workspace, []string{"--quiet", "--non-recursive", "jib:_skaffold-files", "--activate-profiles", "profile"})
107+
return MavenCommand.CreateCommand(ctx, workspace, []string{"--quiet", "--activate-profiles", "profile", "--non-recursive", "jib:_skaffold-files"})
108108
},
109109
},
110110
{
@@ -128,7 +128,7 @@ func TestGetCommandMaven(t *testing.T) {
128128
jibMavenArtifact: latest.JibMavenArtifact{Profile: "profile"},
129129
filesInWorkspace: []string{"mvnw", "mvnw.bat"},
130130
expectedCmd: func(workspace string) *exec.Cmd {
131-
return MavenCommand.CreateCommand(ctx, workspace, []string{"--quiet", "--non-recursive", "jib:_skaffold-files", "--activate-profiles", "profile"})
131+
return MavenCommand.CreateCommand(ctx, workspace, []string{"--quiet", "--activate-profiles", "profile", "--non-recursive", "jib:_skaffold-files"})
132132
},
133133
},
134134
{
@@ -165,9 +165,9 @@ func TestGenerateMavenArgs(t *testing.T) {
165165
out []string
166166
}{
167167
{latest.JibMavenArtifact{}, []string{"--non-recursive", "prepare-package", "jib:goal", "-Dimage=image"}},
168-
{latest.JibMavenArtifact{Profile: "profile"}, []string{"--non-recursive", "prepare-package", "jib:goal", "-Dimage=image", "--activate-profiles", "profile"}},
168+
{latest.JibMavenArtifact{Profile: "profile"}, []string{"--activate-profiles", "profile", "--non-recursive", "prepare-package", "jib:goal", "-Dimage=image"}},
169169
{latest.JibMavenArtifact{Module: "module"}, []string{"--projects", "module", "--also-make", "package", "-Dimage=image"}},
170-
{latest.JibMavenArtifact{Module: "module", Profile: "profile"}, []string{"--projects", "module", "--also-make", "package", "-Dimage=image", "--activate-profiles", "profile"}},
170+
{latest.JibMavenArtifact{Module: "module", Profile: "profile"}, []string{"--activate-profiles", "profile", "--projects", "module", "--also-make", "package", "-Dimage=image"}},
171171
}
172172

173173
for _, tt := range testCases {

0 commit comments

Comments
 (0)