add output packing

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

1
.gitignore vendored
View File

@ -1 +1,2 @@
cmd/pbuild/pbuild
cmd/pbuild/*.zip

View File

@ -8,4 +8,15 @@ type Config struct {
// Post message in Gitea format, only taking what we're interested in.
type GiteaPost struct {
Before string `json:"before"`
After string `json:"after"`
Repository struct {
ID int `json:"id"`
Name string `json:"name"`
FullName string `json:"full_name"`
Description string `json:"description"`
SSHURL string `json:"ssh_url"`
} `json:"repostiory"`
}

View File

@ -21,7 +21,7 @@ func main() {
panic(err)
}
logs, _, _ := profile.Run(".")
logs, _, _ := profile.Run(".", "out.zip")
fmt.Print(logs)
}

View File

@ -4,5 +4,9 @@
"Commands": [
"go clean",
"go build -v"
],
"Output": [
"pbuild"
]
}

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
}