Skip to content

Commit

Permalink
Fix commands for windows (#53)
Browse files Browse the repository at this point in the history
* fix commands for windows

* update analyzer output test data

remove test file
  • Loading branch information
eemcmullan authored Sep 12, 2023
1 parent 86c6fca commit aca5035
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 36 deletions.
62 changes: 29 additions & 33 deletions cmd/analyze.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ import (

var (
// application source path inside the container
SourceMountPath = filepath.Join(InputPath, "source")
SourceMountPath = path.Join(InputPath, "source")
// analyzer config files
ConfigMountPath = filepath.Join(InputPath, "config")
ConfigMountPath = path.Join(InputPath, "config")
// user provided rules path
RulesMountPath = filepath.Join(RulesetPath, "input")
RulesMountPath = path.Join(RulesetPath, "input")
// paths to files in the container
AnalysisOutputMountPath = filepath.Join(OutputPath, "output.yaml")
DepsOutputMountPath = filepath.Join(OutputPath, "dependencies.yaml")
ProviderSettingsMountPath = filepath.Join(ConfigMountPath, "settings.json")
AnalysisOutputMountPath = path.Join(OutputPath, "output.yaml")
DepsOutputMountPath = path.Join(OutputPath, "dependencies.yaml")
ProviderSettingsMountPath = path.Join(ConfigMountPath, "settings.json")
)

// kantra analyze flags
Expand Down Expand Up @@ -119,7 +119,7 @@ func NewAnalyzeCmd(log logr.Logger) *cobra.Command {
}
err := analyzeCmd.Clean(cmd.Context())
if err != nil {
log.Error(err, "failed to generate static report")
log.Error(err, "failed to clean temporary container resources")
return err
}
return nil
Expand Down Expand Up @@ -170,7 +170,7 @@ func (a *analyzeCommand) Validate() error {
return fmt.Errorf("%w failed to get absolute path for input file %s", err, a.input)
}
// make sure we mount a file and not a dir
SourceMountPath = filepath.Join(SourceMountPath, filepath.Base(a.input))
SourceMountPath = path.Join(SourceMountPath, filepath.Base(a.input))
a.isFileInput = true
}
if a.mode != string(provider.FullAnalysisMode) &&
Expand Down Expand Up @@ -333,7 +333,7 @@ func (a *analyzeCommand) getConfigVolumes() (map[string]string, error) {
// only java provider can work with binaries, all others
// continue pointing to the directory instead of file
if a.isFileInput {
otherProvsMountPath = filepath.Dir(otherProvsMountPath)
otherProvsMountPath = path.Dir(otherProvsMountPath)
}

javaConfig := provider.Config{
Expand Down Expand Up @@ -436,7 +436,7 @@ func (a *analyzeCommand) getRulesVolumes() (map[string]string, error) {
}

} else {
rulesVolumes[r] = filepath.Join(RulesMountPath, filepath.Base(r))
rulesVolumes[r] = path.Join(RulesMountPath, filepath.Base(r))
}
}
if rulesetNeeded {
Expand All @@ -446,7 +446,7 @@ func (a *analyzeCommand) getRulesVolumes() (map[string]string, error) {
a.log.V(1).Error(err, "failed to create temp ruleset", "path", tempRulesetPath)
return nil, err
}
rulesVolumes[tempDir] = filepath.Join(RulesMountPath, filepath.Base(tempDir))
rulesVolumes[tempDir] = path.Join(RulesMountPath, filepath.Base(tempDir))
}
return rulesVolumes, nil
}
Expand Down Expand Up @@ -494,7 +494,7 @@ func (a *analyzeCommand) RunAnalysis(ctx context.Context, xmlOutputDir string) e
}

if xmlOutputDir != "" {
convertPath := filepath.Join(RulesetPath, "convert")
convertPath := path.Join(RulesetPath, "convert")
volumes[xmlOutputDir] = convertPath
// for cleanup purposes
a.tempDirs = append(a.tempDirs, xmlOutputDir)
Expand Down Expand Up @@ -584,44 +584,40 @@ func (a *analyzeCommand) GenerateStaticReport(ctx context.Context) error {
if a.skipStaticReport {
return nil
}

volumes := map[string]string{
a.input: SourceMountPath,
a.output: OutputPath,
}

args := []string{
args := []string{}
staticReportArgs := []string{"/usr/local/bin/js-bundle-generator",
fmt.Sprintf("--analysis-output-list=%s", AnalysisOutputMountPath),
fmt.Sprintf("--deps-output-list=%s", DepsOutputMountPath),
fmt.Sprintf("--output-path=%s", filepath.Join("/usr/local/static-report/output.js")),
fmt.Sprintf("--output-path=%s", path.Join("/usr/local/static-report/output.js")),
fmt.Sprintf("--application-name-list=%s", filepath.Base(a.input)),
}
cpArgs := []string{"&& cp -r",
"/usr/local/static-report", OutputPath}

args = append(args, staticReportArgs...)
args = append(args, cpArgs...)

joinedArgs := strings.Join(args, " ")
staticReportCmd := []string{joinedArgs}

a.log.Info("generating static report",
"output", a.output, "args", strings.Join(args, " "))
container := NewContainer(a.log)
a.log.Info("generating static report",
"output", a.output, "args", strings.Join(staticReportCmd, " "))
err := container.Run(
ctx,
WithEntrypointBin("/usr/local/bin/js-bundle-generator"),
WithEntrypointArgs(args...),
WithEntrypointBin("/bin/sh"),
WithEntrypointArgs(staticReportCmd...),
WithVolumes(volumes),
// keep container to copy static report
WithCleanup(false),
WithcFlag(true),
)
if err != nil {
return err
}

err = container.Cp(ctx, "/usr/local/static-report", a.output)
if err != nil {
return err
}

err = container.Rm(ctx)
if err != nil {
return err
}

return nil
}

Expand Down Expand Up @@ -712,7 +708,7 @@ func (a *analyzeCommand) getXMLRulesVolumes(tempRuleDir string) (map[string]stri
return nil, err
}
} else {
rulesVolumes[r] = filepath.Join(XMLRulePath, filepath.Base(r))
rulesVolumes[r] = path.Join(XMLRulePath, filepath.Base(r))
}
}
if mountTempDir {
Expand Down
16 changes: 14 additions & 2 deletions cmd/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"math/rand"
"os"
"os/exec"
"path"
"path/filepath"
"runtime"
"strings"
Expand All @@ -29,6 +30,7 @@ type container struct {
cleanup bool
// map of source -> dest paths to mount
volumes map[string]string
cFlag bool
log logr.Logger
}

Expand Down Expand Up @@ -82,6 +84,12 @@ func WithStderr(e ...io.Writer) Option {
}
}

func WithcFlag(cl bool) Option {
return func(c *container) {
c.cFlag = cl
}
}

func WithCleanup(cl bool) Option {
return func(c *container) {
c.cleanup = cl
Expand Down Expand Up @@ -115,6 +123,7 @@ func NewContainer(log logr.Logger) *container {
name: randomName(),
// by default, remove the container after run()
cleanup: true,
cFlag: false,
log: log,
}
}
Expand Down Expand Up @@ -169,17 +178,20 @@ func (c *container) Run(ctx context.Context, opts ...Option) error {
// TODO: check this on windows
if os == "linux" {
args = append(args, fmt.Sprintf("%s:%s:Z",
filepath.Clean(sourcePath), filepath.Clean(destPath)))
filepath.Clean(sourcePath), path.Clean(destPath)))
} else {
args = append(args, fmt.Sprintf("%s:%s",
filepath.Clean(sourcePath), filepath.Clean(destPath)))
filepath.Clean(sourcePath), path.Clean(destPath)))
}
}
for k, v := range c.env {
args = append(args, "--env")
args = append(args, fmt.Sprintf("%s=%s", k, v))
}
args = append(args, c.image)
if c.cFlag {
args = append(args, "-c")
}
if len(c.entrypointArgs) > 0 {
args = append(args, c.entrypointArgs...)
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/shimconvert.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"os"
"path"
"path/filepath"
"strings"

Expand Down Expand Up @@ -108,7 +109,7 @@ func (w *windupShimCommand) getRulesVolumes(tempRuleDir string) (map[string]stri
return nil, err
}
} else {
rulesVolumes[r] = filepath.Join(XMLRulePath, filepath.Base(r))
rulesVolumes[r] = path.Join(XMLRulePath, filepath.Base(r))
}
}
if mountTempDir {
Expand Down

0 comments on commit aca5035

Please sign in to comment.