Compare commits

..

2 Commits

Author SHA1 Message Date
4054ca3b2e fixup zip creating 2023-08-15 22:06:44 +08:00
35092fc7c1 add output packing 2023-08-15 21:42:18 +08:00
6 changed files with 58 additions and 6 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"
)
@ -14,20 +15,29 @@ type logsWriter struct {
outfd int // 0(manual message) 1(stdout) or 2(stderr)
}
func (l *logsWriter) Write(data []byte) (len int, err error) {
func (l *logsWriter) Write(data []byte) (written int, err error) {
datalen := len(data)
written = datalen
l.lock.Lock()
defer l.lock.Unlock()
for data[datalen-1] == byte('\n') {
datalen--
}
data = data[:datalen]
*l.logs = append(*l.logs, LogEntry{
Time: time.Now(),
Text: string(data),
OutFd: l.outfd,
})
return
}
// 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)
@ -52,13 +62,15 @@ func (p *Profile) Run(dir string) (logs Logs, exitcode int, err error) {
var systime, usrtime time.Duration
fmt.Fprintf(logman, "building %s", p.Name)
// Run commands
runcmds:
for i, str := range p.Commands {
fmt.Fprintf(logman, "[%d/%d] running \"%s\"", i, len(p.Commands), str)
cmd := exec.Command("sh", "-c", str)
cmd := exec.Command("sh", "-c", "exec "+str)
cmd.Env = append(cmd.Env, env...)
cmd.Stdout = logout
cmd.Stderr = logerr
@ -71,12 +83,36 @@ 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", "exec "+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
}
fmt.Fprintf(logman, "output is at %s", outfile)
return
}