From add22d8744da9ce14aa3d9e0355c69ea4693f7dc Mon Sep 17 00:00:00 2001 From: David Gageot Date: Fri, 11 Jan 2019 18:51:42 +0100 Subject: [PATCH] Increase code duplication Signed-off-by: David Gageot --- pkg/skaffold/jib/jib_gradle.go | 18 +++++++++++------ pkg/skaffold/jib/jib_maven.go | 31 ++++++++++++++++-------------- pkg/skaffold/jib/jib_maven_test.go | 8 ++++---- 3 files changed, 33 insertions(+), 24 deletions(-) diff --git a/pkg/skaffold/jib/jib_gradle.go b/pkg/skaffold/jib/jib_gradle.go index bb9548c183d..aff2053e972 100644 --- a/pkg/skaffold/jib/jib_gradle.go +++ b/pkg/skaffold/jib/jib_gradle.go @@ -42,22 +42,28 @@ func GetDependenciesGradle(ctx context.Context, workspace string, a *latest.JibG } func getCommandGradle(ctx context.Context, workspace string, a *latest.JibGradleArtifact) *exec.Cmd { - args := []string{"_jibSkaffoldFiles", "-q"} - if a.Project != "" { + task := "_jibSkaffoldFiles" + + var command string + if a.Project == "" { + command = task + } else { // multi-module - args[0] = fmt.Sprintf(":%s:%s", a.Project, args[0]) + command = fmt.Sprintf(":%s:%s", a.Project, task) } + args := []string{command, "-q"} + return GradleCommand.CreateCommand(ctx, workspace, args) } // GenerateGradleArgs generates the arguments to Gradle for building the project as an image. -func GenerateGradleArgs(task string, imageName string, artifact *latest.JibGradleArtifact) []string { +func GenerateGradleArgs(task string, imageName string, a *latest.JibGradleArtifact) []string { var command string - if artifact.Project == "" { + if a.Project == "" { command = ":" + task } else { // multi-module - command = fmt.Sprintf(":%s:%s", artifact.Project, task) + command = fmt.Sprintf(":%s:%s", a.Project, task) } return []string{command, "--image=" + imageName} diff --git a/pkg/skaffold/jib/jib_maven.go b/pkg/skaffold/jib/jib_maven.go index fcf91ae8a47..887137d6791 100644 --- a/pkg/skaffold/jib/jib_maven.go +++ b/pkg/skaffold/jib/jib_maven.go @@ -40,7 +40,11 @@ func GetDependenciesMaven(ctx context.Context, workspace string, a *latest.JibMa } func getCommandMaven(ctx context.Context, workspace string, a *latest.JibMavenArtifact) *exec.Cmd { - args := []string{"--quiet"} + var args []string + args = append(args, "--quiet") + if a.Profile != "" { + args = append(args, "--activate-profiles", a.Profile) + } if a.Module == "" { // single-module project args = append(args, "--non-recursive") @@ -49,27 +53,26 @@ func getCommandMaven(ctx context.Context, workspace string, a *latest.JibMavenAr args = append(args, "--projects", a.Module, "--also-make") } args = append(args, "jib:_skaffold-files") - if a.Profile != "" { - args = append(args, "--activate-profiles", a.Profile) - } return MavenCommand.CreateCommand(ctx, workspace, args) } // GenerateMavenArgs generates the arguments to Maven for building the project as an image. -func GenerateMavenArgs(goal string, imageName string, artifact *latest.JibMavenArtifact) []string { - var command []string - if artifact.Module == "" { +func GenerateMavenArgs(goal string, imageName string, a *latest.JibMavenArtifact) []string { + var args []string + if a.Profile != "" { + args = append(args, "--activate-profiles", a.Profile) + } + if a.Module == "" { // single-module project - command = []string{"--non-recursive", "prepare-package", "jib:" + goal} + args = append(args, "--non-recursive") + args = append(args, "prepare-package", "jib:"+goal) } else { // multi-module project: we assume `package` is bound to `jib:` - command = []string{"--projects", artifact.Module, "--also-make", "package"} - } - command = append(command, "-Dimage="+imageName) - if artifact.Profile != "" { - command = append(command, "--activate-profiles", artifact.Profile) + args = append(args, "--projects", a.Module, "--also-make") + args = append(args, "package") } + args = append(args, "-Dimage="+imageName) - return command + return args } diff --git a/pkg/skaffold/jib/jib_maven_test.go b/pkg/skaffold/jib/jib_maven_test.go index 44ddb0cd75d..56c9892901b 100644 --- a/pkg/skaffold/jib/jib_maven_test.go +++ b/pkg/skaffold/jib/jib_maven_test.go @@ -104,7 +104,7 @@ func TestGetCommandMaven(t *testing.T) { jibMavenArtifact: latest.JibMavenArtifact{Profile: "profile"}, filesInWorkspace: []string{}, expectedCmd: func(workspace string) *exec.Cmd { - return MavenCommand.CreateCommand(ctx, workspace, []string{"--quiet", "--non-recursive", "jib:_skaffold-files", "--activate-profiles", "profile"}) + return MavenCommand.CreateCommand(ctx, workspace, []string{"--quiet", "--activate-profiles", "profile", "--non-recursive", "jib:_skaffold-files"}) }, }, { @@ -128,7 +128,7 @@ func TestGetCommandMaven(t *testing.T) { jibMavenArtifact: latest.JibMavenArtifact{Profile: "profile"}, filesInWorkspace: []string{"mvnw", "mvnw.bat"}, expectedCmd: func(workspace string) *exec.Cmd { - return MavenCommand.CreateCommand(ctx, workspace, []string{"--quiet", "--non-recursive", "jib:_skaffold-files", "--activate-profiles", "profile"}) + return MavenCommand.CreateCommand(ctx, workspace, []string{"--quiet", "--activate-profiles", "profile", "--non-recursive", "jib:_skaffold-files"}) }, }, { @@ -165,9 +165,9 @@ func TestGenerateMavenArgs(t *testing.T) { out []string }{ {latest.JibMavenArtifact{}, []string{"--non-recursive", "prepare-package", "jib:goal", "-Dimage=image"}}, - {latest.JibMavenArtifact{Profile: "profile"}, []string{"--non-recursive", "prepare-package", "jib:goal", "-Dimage=image", "--activate-profiles", "profile"}}, + {latest.JibMavenArtifact{Profile: "profile"}, []string{"--activate-profiles", "profile", "--non-recursive", "prepare-package", "jib:goal", "-Dimage=image"}}, {latest.JibMavenArtifact{Module: "module"}, []string{"--projects", "module", "--also-make", "package", "-Dimage=image"}}, - {latest.JibMavenArtifact{Module: "module", Profile: "profile"}, []string{"--projects", "module", "--also-make", "package", "-Dimage=image", "--activate-profiles", "profile"}}, + {latest.JibMavenArtifact{Module: "module", Profile: "profile"}, []string{"--activate-profiles", "profile", "--projects", "module", "--also-make", "package", "-Dimage=image"}}, } for _, tt := range testCases {