Skip to content

Commit

Permalink
CP #421: Fix openrewrite mount mvn settings (#425)
Browse files Browse the repository at this point in the history
Fix openrewrite mount mvn settings (#421)

openrewrite mount mvn settings

Signed-off-by: Emily McMullan <[email protected]>
  • Loading branch information
eemcmullan authored Feb 6, 2025
1 parent 04469f6 commit d97d984
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 13 deletions.
22 changes: 11 additions & 11 deletions cmd/analyze.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ func NewAnalyzeCmd(log logr.Logger) *cobra.Command {

return nil
}
log.Info("--run-local not set. running analysis in container mode")
log.Info("--run-local set to false. Running analysis in container mode")

// ******* RUN CONTAINERS ******
if analyzeCmd.overrideProviderSettings == "" {
Expand Down Expand Up @@ -795,7 +795,7 @@ func (a *analyzeCommand) getConfigVolumes() (map[string]string, error) {
}

if a.mavenSettingsFile != "" {
err := copyFileContents(a.mavenSettingsFile, filepath.Join(tempDir, "settings.xml"))
err := CopyFileContents(a.mavenSettingsFile, filepath.Join(tempDir, "settings.xml"))
if err != nil {
a.log.V(1).Error(err, "failed copying maven settings file", "path", a.mavenSettingsFile)
return nil, err
Expand Down Expand Up @@ -980,7 +980,7 @@ func (a *analyzeCommand) getRulesVolumes() (map[string]string, error) {
continue
}
destFile := filepath.Join(tempDir, fmt.Sprintf("rules%d.yaml", i))
err := copyFileContents(r, destFile)
err := CopyFileContents(r, destFile)
if err != nil {
a.log.V(1).Error(err, "failed to move rules file", "src", r, "dest", destFile)
return nil, err
Expand Down Expand Up @@ -1012,7 +1012,7 @@ func (a *analyzeCommand) getRulesVolumes() (map[string]string, error) {
}
destFile := filepath.Join(tempDir, relpath)
a.log.V(5).Info("copying file main", "source", path, "dest", destFile)
err = copyFileContents(path, destFile)
err = CopyFileContents(path, destFile)
if err != nil {
a.log.V(1).Error(err, "failed to move rules file", "src", r, "dest", destFile)
return err
Expand Down Expand Up @@ -1078,7 +1078,7 @@ func copyFolderContents(src string, dst string) error {
}
} else {
// Copy file
if err := copyFileContents(sourcePath, destinationPath); err != nil {
if err := CopyFileContents(sourcePath, destinationPath); err != nil {
return err
}
}
Expand All @@ -1087,7 +1087,7 @@ func copyFolderContents(src string, dst string) error {
return nil
}

func copyFileContents(src string, dst string) (err error) {
func CopyFileContents(src string, dst string) (err error) {
source, err := os.Open(src)
if err != nil {
return nil
Expand Down Expand Up @@ -1683,23 +1683,23 @@ func (a *analyzeCommand) moveResults() error {
outputPath := filepath.Join(a.output, "output.yaml")
analysisLogFilePath := filepath.Join(a.output, "analysis.log")
depsPath := filepath.Join(a.output, "dependencies.yaml")
err := copyFileContents(outputPath, fmt.Sprintf("%s.%s", outputPath, a.inputShortName()))
err := CopyFileContents(outputPath, fmt.Sprintf("%s.%s", outputPath, a.inputShortName()))
if err != nil {
return err
}
err = os.Remove(outputPath)
if err != nil {
return err
}
err = copyFileContents(analysisLogFilePath, fmt.Sprintf("%s.%s", analysisLogFilePath, a.inputShortName()))
err = CopyFileContents(analysisLogFilePath, fmt.Sprintf("%s.%s", analysisLogFilePath, a.inputShortName()))
if err != nil {
return err
}
err = os.Remove(analysisLogFilePath)
if err != nil {
return err
}
err = copyFileContents(depsPath, fmt.Sprintf("%s.%s", depsPath, a.inputShortName()))
err = CopyFileContents(depsPath, fmt.Sprintf("%s.%s", depsPath, a.inputShortName()))
if err == nil { // dependencies file presence is optional
err = os.Remove(depsPath)
if err != nil {
Expand Down Expand Up @@ -1792,7 +1792,7 @@ func (a *analyzeCommand) getXMLRulesVolumes(tempRuleDir string) (map[string]stri
mountTempDir = true
xmlFileName := filepath.Base(r)
destFile := filepath.Join(tempRuleDir, xmlFileName)
err := copyFileContents(r, destFile)
err := CopyFileContents(r, destFile)
if err != nil {
a.log.V(1).Error(err, "failed to move rules file from source to destination", "src", r, "dest", destFile)
return nil, err
Expand Down Expand Up @@ -1977,7 +1977,7 @@ func (a *analyzeCommand) mergeProviderSpecificConfig(optionsConf, seenConf map[s
seenConf[k] = absPath
}
// copy file to mount path
err := copyFileContents(v.(string), filepath.Join(tempDir, "settings.xml"))
err := CopyFileContents(v.(string), filepath.Join(tempDir, "settings.xml"))
if err != nil {
a.log.V(1).Error(err, "failed copying maven settings file", "path", v)
return nil, err
Expand Down
22 changes: 21 additions & 1 deletion cmd/openrewrite.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"os"
"path"
"path/filepath"
"strings"

Expand All @@ -12,6 +13,11 @@ import (
"github.com/spf13/cobra"
)

// path in container for maven settings file
const (
settingsFileMountPath = "/tmp/source-app/input"
)

type openRewriteCommand struct {
listTargets bool
input string
Expand Down Expand Up @@ -148,8 +154,22 @@ func (o *openRewriteCommand) Run(ctx context.Context) error {
"recipe", o.target, "input", o.input, "args", strings.Join(args, " "))

if o.mavenSettingsFile != "" {
tempDir, err := os.MkdirTemp("", "openrewrite-settings-")
if err != nil {
o.log.V(1).Error(err, "failed creating temp dir", "dir", tempDir)
return err
}
o.log.V(1).Info("created directory for maven settings file", "dir", tempDir)
defer os.RemoveAll(tempDir)

err = CopyFileContents(o.mavenSettingsFile, filepath.Join(tempDir, "settings.xml"))
if err != nil {
o.log.V(1).Error(err, "failed copying maven settings file", "path", o.mavenSettingsFile)
return err
}
volumes[tempDir] = settingsFileMountPath
o.log.Info("using custom maven settings file", "path", o.mavenSettingsFile)
args = append(args, "-s", o.mavenSettingsFile)
args = append(args, "-s", path.Join(settingsFileMountPath, "settings.xml"))
}

err := container.NewContainer().Run(
Expand Down
2 changes: 1 addition & 1 deletion cmd/shimconvert.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func (w *windupShimCommand) getRulesVolumes(tempRuleDir string) (map[string]stri
mountTempDir = true
xmlFileName := filepath.Base(r)
destFile := filepath.Join(tempRuleDir, xmlFileName)
err := copyFileContents(r, destFile)
err := CopyFileContents(r, destFile)
if err != nil {
w.log.V(1).Error(err, "failed to move rules file from source to destination", "src", r, "dest", destFile)
return nil, err
Expand Down

0 comments on commit d97d984

Please sign in to comment.