summaryrefslogtreecommitdiff
path: root/cmd
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2026-04-27 09:18:52 +0300
committerPaul Buetow <paul@buetow.org>2026-04-27 09:18:52 +0300
commit47d69cb998a447eea662ad1075f9d002dd875443 (patch)
tree1fa586fc4f7e5fae2ce9badfa05679c0fee9dcb3 /cmd
parent626ff3ae7d43cfc2ec3f2554d340b40f4a5c0586 (diff)
Add context.Context to I/O-bound public APIs (generator.Run, processor.Run, atom.Generate, syncOutput)
- generator.Run(ctx, cfg) – ctx passed through to atom.Generate - processor.Run(ctx, cfg) – signature updated for cancellation propagation - atom.Generate(ctx, posts, cfg) – accepts ctx for future cancellation - syncOutput(ctx, cfg) – rsync subprocesses now use exec.CommandContext - Updated all call sites in tests, cmd/snonux/main.go, and integration tests - All call sites pass context.Background() / context.TODO() All tests pass: go test ./...
Diffstat (limited to 'cmd')
-rw-r--r--cmd/snonux/main.go13
-rw-r--r--cmd/snonux/main_test.go5
-rw-r--r--cmd/snonux/sync.go6
-rw-r--r--cmd/snonux/sync_test.go4
4 files changed, 18 insertions, 10 deletions
diff --git a/cmd/snonux/main.go b/cmd/snonux/main.go
index 659bd19..853d807 100644
--- a/cmd/snonux/main.go
+++ b/cmd/snonux/main.go
@@ -8,6 +8,7 @@
package main
import (
+ "context"
"errors"
"flag"
"fmt"
@@ -61,12 +62,14 @@ func main() {
log.Fatalf("error: %v", err)
}
- if err := run(cfg); err != nil {
+ ctx := context.Background()
+
+ if err := run(ctx, cfg); err != nil {
log.Fatalf("error: %v", err)
}
if cfg.Sync {
- if err := syncOutput(cfg); err != nil {
+ if err := syncOutput(ctx, cfg); err != nil {
log.Fatalf("error: %v", err)
}
}
@@ -130,15 +133,15 @@ func expandHome(path string) (string, error) {
}
// run executes both pipeline phases: process inputs, then regenerate pages.
-func run(cfg *config.Config) error {
- processed, err := processor.Run(cfg)
+func run(ctx context.Context, cfg *config.Config) error {
+ processed, err := processor.Run(ctx, cfg)
if err != nil {
return fmt.Errorf("processing input files: %w", err)
}
log.Printf("processed %d new post(s) from %s", processed, cfg.InputDir)
- if err := generator.Run(cfg); err != nil {
+ if err := generator.Run(ctx, cfg); err != nil {
return fmt.Errorf("generating site: %w", err)
}
diff --git a/cmd/snonux/main_test.go b/cmd/snonux/main_test.go
index b8a5a82..06cbc1c 100644
--- a/cmd/snonux/main_test.go
+++ b/cmd/snonux/main_test.go
@@ -1,6 +1,7 @@
package main
import (
+ "context"
"errors"
"math/rand"
"os"
@@ -12,6 +13,8 @@ import (
"codeberg.org/snonux/snonux/internal/generator"
)
+var ctx = context.Background() //nolint:gochecknoglobals // test-only top-level helper used by every test in the file
+
func TestExpandHome(t *testing.T) {
t.Parallel()
@@ -291,7 +294,7 @@ func TestRun_pipeline(t *testing.T) {
BaseURL: "https://pipe.test",
Theme: "neon",
}
- if err := run(cfg); err != nil {
+ if err := run(ctx, cfg); err != nil {
t.Fatal(err)
}
data, err := os.ReadFile(filepath.Join(out, "index.html"))
diff --git a/cmd/snonux/sync.go b/cmd/snonux/sync.go
index f5d2e9e..3e4155e 100644
--- a/cmd/snonux/sync.go
+++ b/cmd/snonux/sync.go
@@ -58,7 +58,9 @@ func splitAndTrim(s string) []string {
// syncOutput rsyncs localOutput (trailing-slash source) to each sync target over SSH
// port 22. It runs only if every target answers ICMP ping (Linux iputils: ping -c 1 -W …).
-func syncOutput(cfg *config.Config) error {
+// The ctx parameter is accepted for cancellation propagation; it is wired into
+// exec.CommandContext for the rsync subprocesses.
+func syncOutput(ctx context.Context, cfg *config.Config) error {
resolveSyncConfig(cfg)
for _, host := range cfg.SyncTargets {
@@ -87,7 +89,7 @@ func syncOutput(cfg *config.Config) error {
for _, host := range cfg.SyncTargets {
dest := fmt.Sprintf("%s@%s:%s", sshUser, host, cfg.SyncRemoteDir)
log.Printf("rsync %s -> %s", src, dest)
- cmd := exec.Command("rsync", "-az", "-e", ssh, src, dest)
+ cmd := exec.CommandContext(ctx, "rsync", "-az", "-e", ssh, src, dest)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
diff --git a/cmd/snonux/sync_test.go b/cmd/snonux/sync_test.go
index d45a916..93c9233 100644
--- a/cmd/snonux/sync_test.go
+++ b/cmd/snonux/sync_test.go
@@ -87,8 +87,8 @@ func TestResolveSyncConfig_flagsOverrideEnv(t *testing.T) {
os.Setenv("SNONUX_SYNC_REMOTE_DIR", "/env/dir/")
cfg := &config.Config{
- SyncTargets: []string{"from-flag"},
- SyncRemoteDir: "/flag/dir/",
+ SyncTargets: []string{"from-flag"},
+ SyncRemoteDir: "/flag/dir/",
}
resolveSyncConfig(cfg)