Skip to content

Commit

Permalink
fix: correctly set env var val & dir paths
Browse files Browse the repository at this point in the history
  • Loading branch information
York Chen committed Sep 19, 2023
1 parent 4b6d8a1 commit da8fb57
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
23 changes: 17 additions & 6 deletions pkg/collect/host_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,27 @@ func (c *CollectHostRun) Collect(progressChan chan<- interface{}) (map[string][]
bundleOutputRelativePath string
cmdOutputTempDir string
cmdInputTempDir string
err error
)

cmd := exec.Command(runHostCollector.Command, runHostCollector.Args...)

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),
)
Expand All @@ -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))
}
Expand All @@ -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 ")
Expand Down
4 changes: 2 additions & 2 deletions pkg/collect/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down

0 comments on commit da8fb57

Please sign in to comment.