diff --git a/pkg/collect/host_run.go b/pkg/collect/host_run.go index 5acf3d377..924d82f62 100644 --- a/pkg/collect/host_run.go +++ b/pkg/collect/host_run.go @@ -45,6 +45,7 @@ func (c *CollectHostRun) Collect(progressChan chan<- interface{}) (map[string][] bundleOutputRelativePath string cmdOutputTempDir string cmdInputTempDir string + err error ) cmd := exec.Command(runHostCollector.Command, runHostCollector.Args...) @@ -52,12 +53,19 @@ func (c *CollectHostRun) Collect(progressChan chan<- interface{}) (map[string][] collectorRelativePath := filepath.Join("host-collectors/run-host", collectorName) if runHostCollector.Env != nil { - cmd.Env = append(cmd.Env, runHostCollector.Env...) + for i := range runHostCollector.Env { + cmd.Env = append(cmd.Env, + fmt.Sprintf("%s=%s", runHostCollector.Env[i], os.Getenv(runHostCollector.Env[i]))) + } + } // if we choose to save result for the command run if runHostCollector.OutputDir != "" { - cmdOutputTempDir = os.TempDir() + cmdOutputTempDir, err = os.MkdirTemp("", runHostCollector.OutputDir) defer os.RemoveAll(cmdOutputTempDir) + if err != nil { + return nil, errors.New(fmt.Sprintf("failed to created temp dir for: %s", runHostCollector.OutputDir)) + } cmd.Env = append(cmd.Env, fmt.Sprintf("TS_WORKSPACE_DIR=%s", cmdOutputTempDir), ) @@ -66,14 +74,17 @@ func (c *CollectHostRun) Collect(progressChan chan<- interface{}) (map[string][] output := NewResult() if runHostCollector.Input != nil { - cmdInputTempDir = os.TempDir() - defer os.RemoveAll(cmdOutputTempDir) + cmdInputTempDir, err = os.MkdirTemp("", "input") + defer os.RemoveAll(cmdInputTempDir) + if err != nil { + return nil, errors.New("failed to created temp dir for host run input") + } for inFilename, inFileContent := range runHostCollector.Input { if strings.Contains(inFileContent, "/") { return nil, errors.New("Input filename contains '/'") } cmdInputFilePath := filepath.Join(cmdInputTempDir, inFilename) - err := os.WriteFile(cmdInputFilePath, []byte(inFileContent), 0644) + err = os.WriteFile(cmdInputFilePath, []byte(inFileContent), 0644) if err != nil { return nil, errors.Wrap(err, fmt.Sprintf("failed to write input file: %s to temp directory", inFilename)) } @@ -92,7 +103,7 @@ func (c *CollectHostRun) Collect(progressChan chan<- interface{}) (map[string][] ExitCode: "0", } - err := cmd.Run() + err = cmd.Run() if err != nil { if werr, ok := err.(*exec.ExitError); ok { runInfo.ExitCode = strings.TrimPrefix(werr.Error(), "exit status ") diff --git a/pkg/collect/result.go b/pkg/collect/result.go index 9306bc34e..225362539 100644 --- a/pkg/collect/result.go +++ b/pkg/collect/result.go @@ -160,8 +160,8 @@ func (r CollectorResult) SaveResults(bundlePath, relativePath, targetDir string) if err != nil { return errors.Wrap(err, fmt.Sprintf("failed to read file: %s", path)) } - - err = r.SaveResult(bundlePath, strings.TrimPrefix(path, bundlePath+"/"), bytes.NewBuffer(fileBytes)) + bundleRelativePath := filepath.Join(relativePath, strings.TrimPrefix(path, targetDir+"/")) + err = r.SaveResult(bundlePath, bundleRelativePath, bytes.NewBuffer(fileBytes)) if err != nil { return errors.Wrap(err, "error from SaveResult call") }