From 35092fc7c1d461bde58adf19d586bbb504a2f90e Mon Sep 17 00:00:00 2001 From: Edgaru089 Date: Tue, 15 Aug 2023 21:42:18 +0800 Subject: [PATCH] add output packing --- .gitignore | 1 + cmd/pbuild/config.go | 11 +++++++++++ cmd/pbuild/main.go | 2 +- cmd/pbuild/profile.json | 4 ++++ internal/runner/profile.go | 2 +- internal/runner/run.go | 27 +++++++++++++++++++++++++-- 6 files changed, 43 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index bf85505..e8e036b 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ cmd/pbuild/pbuild +cmd/pbuild/*.zip diff --git a/cmd/pbuild/config.go b/cmd/pbuild/config.go index 621accd..acb24cc 100644 --- a/cmd/pbuild/config.go +++ b/cmd/pbuild/config.go @@ -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"` } diff --git a/cmd/pbuild/main.go b/cmd/pbuild/main.go index 290d8af..4b2cbe0 100644 --- a/cmd/pbuild/main.go +++ b/cmd/pbuild/main.go @@ -21,7 +21,7 @@ func main() { panic(err) } - logs, _, _ := profile.Run(".") + logs, _, _ := profile.Run(".", "out.zip") fmt.Print(logs) } diff --git a/cmd/pbuild/profile.json b/cmd/pbuild/profile.json index 8cc531d..6a3436b 100644 --- a/cmd/pbuild/profile.json +++ b/cmd/pbuild/profile.json @@ -4,5 +4,9 @@ "Commands": [ "go clean", "go build -v" + ], + + "Output": [ + "pbuild" ] } diff --git a/internal/runner/profile.go b/internal/runner/profile.go index 88fa8bc..822ac08 100644 --- a/internal/runner/profile.go +++ b/internal/runner/profile.go @@ -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. } diff --git a/internal/runner/run.go b/internal/runner/run.go index d42fe05..c52845b 100644 --- a/internal/runner/run.go +++ b/internal/runner/run.go @@ -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 }