]> ruderich.org/simon Gitweb - punyci/punyci.git/commitdiff
Refactor Config struct by adding Job struct
authorSimon Ruderich <simon@ruderich.org>
Sun, 22 Feb 2026 16:43:55 +0000 (17:43 +0100)
committerSimon Ruderich <simon@ruderich.org>
Sun, 22 Feb 2026 16:43:55 +0000 (17:43 +0100)
job.go
mail.go
main.go

diff --git a/job.go b/job.go
index e533bc22119d685cd08cf3fbe19ef81bf7a42d7c..95de35edbbec1be3f45360900049763a860151f3 100644 (file)
--- a/job.go
+++ b/job.go
@@ -18,15 +18,14 @@ import (
        "sync"
 )
 
-func RunJob(wg *sync.WaitGroup, tmp string, info Info,
-       id int, image, script string,
+func RunJob(wg *sync.WaitGroup, tmp string, info Info, job Job,
        mail *bool, outres *[]byte, uuidres *string) error {
 
        cwd, err := os.Getwd()
        if err != nil {
                return err
        }
-       repoPath := filepath.Join(tmp, strconv.Itoa(id))
+       repoPath := filepath.Join(tmp, strconv.Itoa(job.id))
 
        err = RepoClone(cwd, repoPath, info.Branch, info.Oid)
        if err != nil {
@@ -35,7 +34,7 @@ func RunJob(wg *sync.WaitGroup, tmp string, info Info,
        }
 
        scriptPath := repoPath + ".script"
-       err = os.WriteFile(scriptPath, []byte(script), 0700)
+       err = os.WriteFile(scriptPath, []byte(job.Script), 0700)
        if err != nil {
                return err
        }
@@ -51,7 +50,7 @@ func RunJob(wg *sync.WaitGroup, tmp string, info Info,
                "--volume", scriptPath+":/punyci-script:ro",
                "--entrypoint", "sh", // overwrite if set
                "--",
-               image,
+               job.Image,
                "-c", "while :; do sleep 86400; done", // sleep forever
        )
        out, err := cmd.Output()
@@ -102,8 +101,8 @@ func RunJob(wg *sync.WaitGroup, tmp string, info Info,
                        return fmt.Errorf("script failed in container %s: %v", uuid, err)
                }
        } else {
-               logPath := "punyci-log-" + strconv.Itoa(id)
-               failedPath := "punyci-failed-" + strconv.Itoa(id)
+               logPath := "punyci-log-" + strconv.Itoa(job.id)
+               failedPath := "punyci-failed-" + strconv.Itoa(job.id)
 
                f, err := os.Create(logPath)
                if err != nil {
diff --git a/mail.go b/mail.go
index 23f1fcefc7d8d503530d42a812c22abd509e8919..22951115c77479c34a00cf4c0fb0b66d90f6fc53 100644 (file)
--- a/mail.go
+++ b/mail.go
@@ -13,7 +13,7 @@ import (
        "strings"
 )
 
-func SendFailMail(to, name, image, script string,
+func SendFailMail(to, name string, job Job,
        err error, output []byte, container string) error {
 
        subject := fmt.Sprintf("punyci: %s FAILED", name)
@@ -21,21 +21,21 @@ func SendFailMail(to, name, image, script string,
        fmt.Fprintf(&msg, "Attach to the failed container for one hour: %s\n",
                container)
        fmt.Fprintf(&msg, "\n")
-       fmt.Fprintf(&msg, "Image: %s\n", image)
+       fmt.Fprintf(&msg, "Image: %s\n", job.Image)
        fmt.Fprintf(&msg, "Error: %s\n", err)
        fmt.Fprintf(&msg, "\n")
-       fmt.Fprintf(&msg, "Script:\n%s\n", strings.TrimSpace(script))
+       fmt.Fprintf(&msg, "Script:\n%s\n", strings.TrimSpace(job.Script))
        fmt.Fprintf(&msg, "\n")
        fmt.Fprintf(&msg, "Output:\n%s\n", output)
        return SendMail(to, subject, msg.String())
 }
 
-func SendFixMail(to, name, image, script string, output []byte) error {
+func SendFixMail(to, name string, job Job, output []byte) error {
        subject := fmt.Sprintf("punyci: %s FIXED", name)
        var msg bytes.Buffer
-       fmt.Fprintf(&msg, "Image: %s\n", image)
+       fmt.Fprintf(&msg, "Image: %s\n", job.Image)
        fmt.Fprintf(&msg, "\n")
-       fmt.Fprintf(&msg, "Script:\n%s\n", strings.TrimSpace(script))
+       fmt.Fprintf(&msg, "Script:\n%s\n", strings.TrimSpace(job.Script))
        fmt.Fprintf(&msg, "\n")
        fmt.Fprintf(&msg, "Output:\n%s\n", output)
        return SendMail(to, subject, msg.String())
diff --git a/main.go b/main.go
index bf61447ae58bf4462c78c9516267c847ed8921b2..77efd3473c177e42007490d8d1f926650b3b11db 100644 (file)
--- a/main.go
+++ b/main.go
@@ -20,10 +20,13 @@ import (
 )
 
 type Config struct {
-       Jobs []struct {
-               Image  string
-               Script string
-       }
+       Jobs []Job
+}
+
+type Job struct {
+       id     int
+       Image  string
+       Script string
 }
 
 func main() {
@@ -79,8 +82,9 @@ func postReceive() error {
                return nil
        }
 
-       // Add shebang to script if not present
        for i, job := range cfg.Jobs {
+               cfg.Jobs[i].id = i
+               // Add shebang to script if not present
                if !strings.HasPrefix(job.Script, "#!") {
                        cfg.Jobs[i].Script = "#!/bin/sh\n" + job.Script
                }
@@ -153,24 +157,22 @@ func internalPostReceive() error {
 
        var failed bool
        var wg sync.WaitGroup
-       for i, job := range info.Config.Jobs {
+       for _, job := range info.Config.Jobs {
                x := job.Script
                if len(x) > 20 {
                        x = x[:20] + "[...]"
                }
                log.Printf("punyci: running job %d with image %q and script %q",
-                       i, job.Image, x)
+                       job.id, job.Image, x)
 
                var mail bool
                var out []byte
                var uuid string
-               err := RunJob(&wg, tmp, info, i, job.Image, job.Script,
-                       &mail, &out, &uuid)
+               err := RunJob(&wg, tmp, info, job, &mail, &out, &uuid)
                if err == nil {
                        if mail {
                                // Fixed a previously failed job, send email
-                               err := SendFixMail(info.User, name, job.Image, job.Script,
-                                       out)
+                               err := SendFixMail(info.User, name, job, out)
                                if err != nil {
                                        log.Print(err)
                                }
@@ -179,8 +181,7 @@ func internalPostReceive() error {
                        failed = true
                        if mail {
                                // Failed job, send mail
-                               err := SendFailMail(info.User, name, job.Image, job.Script,
-                                       err, out, uuid)
+                               err := SendFailMail(info.User, name, job, err, out, uuid)
                                if err != nil {
                                        log.Print(err)
                                }