From 1f47370b16f99c420db9eaf66fdb1cb12f896f93 Mon Sep 17 00:00:00 2001 From: kthatipally <99229993+kthatipally@users.noreply.github.com> Date: Wed, 30 Oct 2024 12:57:06 -0500 Subject: [PATCH] Update copy functionality that works on all the platforms Signed-off-by: kthatipally <99229993+kthatipally@users.noreply.github.com> --- cmd/analyze-bin.go | 11 ++++------- cmd/analyze.go | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 7 deletions(-) diff --git a/cmd/analyze-bin.go b/cmd/analyze-bin.go index dbcd057..0c63f0b 100644 --- a/cmd/analyze-bin.go +++ b/cmd/analyze-bin.go @@ -8,7 +8,6 @@ import ( "io" "log" "os" - "os/exec" "path/filepath" "runtime" "sort" @@ -559,16 +558,14 @@ func (a *analyzeCommand) buildStaticReportFile(ctx context.Context, staticReport } func (a *analyzeCommand) buildStaticReportOutput(ctx context.Context, log *os.File) error { - outputFileDestPath := filepath.Join(a.kantraDir, "static-report") + outputFolderSrcPath := filepath.Join(a.kantraDir, "static-report") + outputFolderDestPath := filepath.Join(a.output, "static-report") - // move build dir to user output dir - cmd := exec.Command("cp", "-r", outputFileDestPath, a.output) - cmd.Stdout = log - err := cmd.Run() + //copy static report files to output folder + err := copyFolderContents(outputFolderSrcPath, outputFolderDestPath) if err != nil { return err } - return nil } diff --git a/cmd/analyze.go b/cmd/analyze.go index 8b852c2..eea81d3 100644 --- a/cmd/analyze.go +++ b/cmd/analyze.go @@ -1074,6 +1074,42 @@ func (a *analyzeCommand) handleDir(p string, tempDir string, basePath string) er return err } +func copyFolderContents(src string, dst string) error { + err := os.MkdirAll(dst, os.ModePerm) + if err != nil { + return err + } + source, err := os.Open(src) + if err != nil { + return err + } + defer source.Close() + + contents, err := source.Readdir(-1) + if err != nil { + return err + } + + for _, item := range contents { + sourcePath := filepath.Join(src, item.Name()) + destinationPath := filepath.Join(dst, item.Name()) + + if item.IsDir() { + // Recursively copy subdirectories + if err := copyFolderContents(sourcePath, destinationPath); err != nil { + return err + } + } else { + // Copy file + if err := copyFileContents(sourcePath, destinationPath); err != nil { + return err + } + } + } + + return nil +} + func copyFileContents(src string, dst string) (err error) { source, err := os.Open(src) if err != nil {