add output packing

This commit is contained in:
2023-08-15 21:42:18 +08:00
parent 57fc43ff9c
commit 35092fc7c1
6 changed files with 43 additions and 4 deletions

View File

@ -13,7 +13,7 @@ type Profile struct {
Commands []string // Commands to run on the profile.
Environ map[string]string // Environment variables
Output []string // List of globs to match output files.
Output []string // List of globs to match output files, fed directly into the shell.
OutputDir string // The globs will be matched in this relative directory.
}

View File

@ -4,6 +4,7 @@ import (
"fmt"
"os"
"os/exec"
"strings"
"sync"
"time"
)
@ -27,7 +28,7 @@ func (l *logsWriter) Write(data []byte) (len int, err error) {
}
// Run runs the profile on the target directory, preserving output.
func (p *Profile) Run(dir string) (logs Logs, exitcode int, err error) {
func (p *Profile) Run(dir string, outfile string) (logs Logs, exitcode int, err error) {
// Try if the directory exists
_, err = os.ReadDir(dir)
@ -71,12 +72,34 @@ runcmds:
switch err.(type) {
case *exec.ExitError:
fmt.Fprintf(logman, "!!!!! command exit with code %d !!!!!", err.(*exec.ExitError).ExitCode())
fmt.Fprintf(logman, "!!!!! command exit with code %d: %s", err.(*exec.ExitError).ExitCode(), err.Error())
break runcmds
}
}
fmt.Fprintf(logman, "system %.4f, user %.4f", systime.Seconds(), usrtime.Seconds())
// package the files
writer, err := os.OpenFile(outfile, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0644)
defer writer.Close()
if err != nil {
fmt.Fprintf(logman, "!!!!! error opening output pack: %s", err.Error())
return
}
packstr := "zip -r - " + strings.Join(p.Output, " ")
fmt.Fprintf(logman, "[fin] packing output: \"%s\"", packstr)
packcmd := exec.Command("sh", "-c", packstr)
packcmd.Dir = p.OutputDir
packcmd.Stdout = writer
packcmd.Stderr = logerr
err = packcmd.Run()
if err != nil {
fmt.Fprintf(logman, "!!!!! pack subprocess return %d: %s", packcmd.ProcessState.ExitCode(), err.Error())
return
}
return
}