generated from konveyor-ecosystem/template-repo
-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Pranav Gaikwad <pgaikwad@redhat.com>
- Loading branch information
1 parent
e2573c7
commit 8e5490f
Showing
10 changed files
with
322 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.vscode/ | ||
|
||
env.local |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
package cmd | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
"os" | ||
"os/exec" | ||
) | ||
|
||
type containerCommand struct { | ||
stdout io.Writer | ||
stderr io.Writer | ||
containerName string | ||
containerImage string | ||
entrypointBin string | ||
entrypointArgs []string | ||
workdir string | ||
// map of source -> dest paths to mount | ||
volumes map[string]string | ||
} | ||
|
||
type Option func(c *containerCommand) | ||
|
||
func WithContainerImage(i string) Option { | ||
return func(c *containerCommand) { | ||
c.containerImage = i | ||
} | ||
} | ||
|
||
func WithContainerName(n string) Option { | ||
return func(c *containerCommand) { | ||
c.containerName = n | ||
} | ||
} | ||
|
||
func WithEntrypointBin(b string) Option { | ||
return func(c *containerCommand) { | ||
c.entrypointBin = b | ||
} | ||
} | ||
|
||
func WithEntrypointArgs(args ...string) Option { | ||
return func(c *containerCommand) { | ||
c.entrypointArgs = args | ||
} | ||
} | ||
|
||
func WithWorkDir(w string) Option { | ||
return func(c *containerCommand) { | ||
c.workdir = w | ||
} | ||
} | ||
|
||
func WithVolumes(m map[string]string) Option { | ||
return func(c *containerCommand) { | ||
c.volumes = m | ||
} | ||
} | ||
|
||
func WithStdout(o io.Writer) Option { | ||
return func(c *containerCommand) { | ||
c.stdout = o | ||
} | ||
} | ||
|
||
func WithStderr(e io.Writer) Option { | ||
return func(c *containerCommand) { | ||
c.stderr = e | ||
} | ||
} | ||
|
||
func NewContainerCommand(ctx context.Context, opts ...Option) *exec.Cmd { | ||
c := &containerCommand{ | ||
containerImage: Settings.RunnerImage, | ||
entrypointArgs: []string{}, | ||
volumes: make(map[string]string), | ||
stdout: os.Stdout, | ||
stderr: os.Stderr, | ||
} | ||
|
||
for _, opt := range opts { | ||
opt(c) | ||
} | ||
|
||
args := []string{"run", "-it"} | ||
if c.containerName != "" { | ||
args = append(args, "--name") | ||
args = append(args, c.containerName) | ||
} | ||
|
||
if c.entrypointBin != "" { | ||
args = append(args, "--entrypoint") | ||
args = append(args, c.entrypointBin) | ||
} | ||
|
||
if c.workdir != "" { | ||
args = append(args, "--workdir") | ||
args = append(args, c.workdir) | ||
} | ||
|
||
for sourcePath, destPath := range c.volumes { | ||
args = append(args, "-v") | ||
args = append(args, fmt.Sprintf("%s:%s:Z", sourcePath, destPath)) | ||
} | ||
|
||
args = append(args, c.containerImage) | ||
if len(c.entrypointArgs) > 0 { | ||
args = append(args, c.entrypointArgs...) | ||
} | ||
|
||
cmd := exec.CommandContext(ctx, Settings.PodmanBinary, args...) | ||
cmd.Stdout = c.stdout | ||
cmd.Stderr = c.stderr | ||
return cmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
package cmd | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
"strings" | ||
|
||
"github.com/apex/log" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
type openRewriteCommand struct { | ||
listTargets bool | ||
input string | ||
target string | ||
goal string | ||
miscOpts string | ||
} | ||
|
||
func NewOpenRewriteCommand() *cobra.Command { | ||
openRewriteCmd := &openRewriteCommand{} | ||
|
||
openRewriteCommand := &cobra.Command{ | ||
Use: "openrewrite", | ||
|
||
Short: "Transform application source code using OpenRewrite recipes", | ||
PreRun: func(cmd *cobra.Command, args []string) { | ||
if !cmd.Flags().Lookup("list-targets").Changed { | ||
cmd.MarkFlagRequired("input") | ||
cmd.MarkFlagRequired("target") | ||
} | ||
}, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
err := openRewriteCmd.Validate() | ||
if err != nil { | ||
return err | ||
} | ||
err = openRewriteCmd.Run(cmd.Context()) | ||
if err != nil { | ||
log.Errorf("failed to execute openrewrite command", err) | ||
return err | ||
} | ||
return nil | ||
}, | ||
} | ||
openRewriteCommand.Flags().BoolVarP(&openRewriteCmd.listTargets, "list-targets", "l", false, "list all available OpenRewrite recipes") | ||
openRewriteCommand.Flags().StringVarP(&openRewriteCmd.target, "target", "t", "", "target openrewrite recipe to use. Run --list-targets to get a list of packaged recipes.") | ||
openRewriteCommand.Flags().StringVarP(&openRewriteCmd.goal, "goal", "g", "dryRun", "target goal") | ||
openRewriteCommand.Flags().StringVarP(&openRewriteCmd.input, "input", "i", "", "path to application source code directory") | ||
|
||
return openRewriteCommand | ||
} | ||
|
||
func (o *openRewriteCommand) Validate() error { | ||
if o.listTargets { | ||
return nil | ||
} | ||
|
||
stat, err := os.Stat(o.input) | ||
if err != nil { | ||
return err | ||
} | ||
if !stat.IsDir() { | ||
log.Errorf("input path %s is not a directory", o.input) | ||
return err | ||
} | ||
|
||
if o.target == "" { | ||
return fmt.Errorf("target recipe must be specified") | ||
} | ||
|
||
if _, found := recipes[o.target]; !found { | ||
return fmt.Errorf("unsupported target recipe. use --list-targets to get list of all recipes") | ||
} | ||
return nil | ||
} | ||
|
||
type recipe struct { | ||
names []string | ||
path string | ||
description string | ||
} | ||
|
||
var recipes = map[string]recipe{ | ||
"eap8-xml": { | ||
names: []string{"org.jboss.windup.eap8.FacesWebXml"}, | ||
path: "eap8/xml/rewrite.yml", | ||
description: "Transform Faces Web XML for EAP8 migration", | ||
}, | ||
"jakarta-xml": { | ||
names: []string{"org.jboss.windup.jakarta.javax.PersistenceXml"}, | ||
path: "jakarta/javax/xml/rewrite.yml", | ||
description: "Transform Persistence XML for Jakarta migration", | ||
}, | ||
"jakarta-bootstrapping": { | ||
names: []string{"org.jboss.windup.jakarta.javax.BootstrappingFiles"}, | ||
path: "jakarta/javax/bootstrapping/rewrite.yml", | ||
description: "Transform bootstrapping files for Jakarta migration", | ||
}, | ||
"jakarta-imports": { | ||
names: []string{"org.jboss.windup.JavaxToJakarta"}, | ||
path: "jakarta/javax/imports/rewrite.yml", | ||
description: "Transform dependencies and imports for Jakarta migration", | ||
}, | ||
"quarkus-properties": { | ||
names: []string{"org.jboss.windup.sb-quarkus.Properties"}, | ||
path: "quarkus/springboot/properties/rewrite.yml", | ||
description: "Migrate Springboot properties to Quarkus", | ||
}, | ||
} | ||
|
||
func (o *openRewriteCommand) Run(ctx context.Context) error { | ||
if o.listTargets { | ||
fmt.Printf("%-20s\t%s\n", "NAME", "DESCRIPTION") | ||
for name, recipe := range recipes { | ||
fmt.Printf("%-20s\t%s\n", name, recipe.description) | ||
} | ||
return nil | ||
} | ||
|
||
volumes := map[string]string{ | ||
o.input: InputPath, | ||
} | ||
args := []string{ | ||
"-U", "org.openrewrite.maven:rewrite-maven-plugin:run", | ||
fmt.Sprintf("-Drewrite.configLocation=%s/%s", | ||
OpenRewriteRecipesPath, recipes[o.target].path), | ||
fmt.Sprintf("-Drewrite.activeRecipes=%s", | ||
strings.Join(recipes[o.target].names, ",")), | ||
} | ||
cmd := NewContainerCommand( | ||
ctx, | ||
WithEntrypointArgs(args...), | ||
WithEntrypointBin("/usr/bin/mvn"), | ||
WithVolumes(volumes), | ||
WithWorkDir(InputPath), | ||
) | ||
err := cmd.Run() | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package cmd | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func NewTransformCommand() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "transform", | ||
|
||
Short: "Transform application source code or windup XML rules", | ||
PreRunE: func(cmd *cobra.Command, args []string) error { | ||
return nil | ||
}, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
cmd.Help() | ||
}, | ||
} | ||
cmd.AddCommand(NewOpenRewriteCommand()) | ||
return cmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#!/bin/bash | ||
|
||
# this is a sample file that contains environment variables used by kantra | ||
# by default, values for these variables are set to work in the docker image | ||
# when running locally, you will need to tweak these values as per your env | ||
# copy this file to `env.local` and source it in your bash session to use CLI | ||
|
||
PODMAN_BIN= | ||
RUNNER_IMG= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters