Add template-as-code sync (campaigns template NAME PATH)

Answers "can the unsubscribe template be added via git+API too": commit
listmonk's own stock campaign template (already unsubscribe-capable) as
email-templates/campaign.html, add a small find-or-create-or-update
Template client (confirmed against knadh/listmonk's actual model/handlers),
a new `template` subcommand, and a manual-dispatch-only workflow to push it
in as the default campaign template. Unlike campaign sync there's no
draft/live status to protect, so this is always a safe overwrite.
This commit is contained in:
2026-07-10 08:54:31 -04:00
parent bcf8df5579
commit a630a8f8ab
9 changed files with 476 additions and 6 deletions
+38
View File
@@ -0,0 +1,38 @@
package campaign
import (
"fmt"
"os"
"reground.org/eec-campaigns/internal/listmonk"
)
// SyncTemplate pushes a template file's content into listmonk under name,
// creating it (as the default campaign template) if missing or overwriting
// its body if it already exists. Unlike campaign sync, a template has no
// send-lifecycle status to protect — there's no "draft vs. live" distinction
// to guard, so this is always a safe create-or-update, no rejection path.
func SyncTemplate(lm *listmonk.Client, name, path string) (*listmonk.Template, error) {
body, err := os.ReadFile(path)
if err != nil {
return nil, err
}
existing, err := lm.FindTemplateByName(name)
if err != nil {
return nil, err
}
if existing == nil {
created, err := lm.CreateTemplate(name, string(body), true)
if err != nil {
return nil, fmt.Errorf("creating template %q: %w", name, err)
}
return created, nil
}
if err := lm.UpdateTemplate(existing.ID, name, string(body), true); err != nil {
return nil, fmt.Errorf("updating template %q: %w", name, err)
}
existing.Body = string(body)
existing.IsDefault = true
return existing, nil
}