Skip to content

Commit

Permalink
Increase code duplication
Browse files Browse the repository at this point in the history
Signed-off-by: David Gageot <[email protected]>
  • Loading branch information
dgageot committed Jan 11, 2019
1 parent c19ace2 commit e3387c5
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 24 deletions.
18 changes: 12 additions & 6 deletions pkg/skaffold/jib/jib_gradle.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
31 changes: 17 additions & 14 deletions pkg/skaffold/jib/jib_maven.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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:<goal>`
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
}
8 changes: 4 additions & 4 deletions pkg/skaffold/jib/jib_maven_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"})
},
},
{
Expand All @@ -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"})
},
},
{
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit e3387c5

Please sign in to comment.