diff options
| author | Paul Buetow <paul@buetow.org> | 2026-02-28 10:06:29 +0200 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2026-02-28 12:50:12 +0200 |
| commit | 72b5a6f7b2100d228a0f16171c6c08b8f311f0d4 (patch) | |
| tree | 9a1d0424d2b0943b54323cb04445178488370024 /internal/task/task.go | |
| parent | dbf8980f1bc5428d1eb463505968469617dbf3fc (diff) | |
refactor(task): move debug state into debugConfig struct
DIP/MEDIUM task (UUID 2f64277a): the two bare package-level vars
(debugWriter, debugFile) were mutable shared state that prevented
concurrent test isolation.
Introduce private debugConfig struct and a single package-level
instance (dbg). SetDebugLog and run() are updated to use dbg.writer /
dbg.file. The public API is unchanged so main.go needs no modification.
Add TestSetDebugLog covering: invalid-path error (negative), enable,
log write reaching the file, reconfiguration, and disable.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Diffstat (limited to 'internal/task/task.go')
| -rw-r--r-- | internal/task/task.go | 34 |
1 files changed, 22 insertions, 12 deletions
diff --git a/internal/task/task.go b/internal/task/task.go index 8eadd18..d8e2e72 100644 --- a/internal/task/task.go +++ b/internal/task/task.go @@ -43,17 +43,27 @@ type Task struct { Annotations []Annotation `json:"annotations"` } -var debugWriter io.Writer -var debugFile *os.File // Track the file handle to close it properly +// debugConfig groups the optional debug-logging state for the task package. +// Collecting related vars into a struct makes the mutable state explicit and +// allows the logger to be swapped or reset cleanly without touching unrelated +// package globals. +type debugConfig struct { + writer io.Writer + file *os.File // tracked separately so it can be closed on reconfiguration +} + +// dbg holds the active debug-logging configuration for this package. +// It is written only via SetDebugLog and read only in run(). +var dbg debugConfig // SetDebugLog enables logging of executed commands to the given file. -// Passing an empty path disables logging. +// Passing an empty path disables logging and closes any previously opened file. func SetDebugLog(path string) error { - // Close existing debug file if open - if debugFile != nil { - debugFile.Close() - debugFile = nil - debugWriter = nil + // Close existing debug file if open before re-configuring. + if dbg.file != nil { + dbg.file.Close() + dbg.file = nil + dbg.writer = nil } if path == "" { @@ -64,8 +74,8 @@ func SetDebugLog(path string) error { if err != nil { return err } - debugFile = f - debugWriter = f + dbg.file = f + dbg.writer = f return nil } @@ -142,8 +152,8 @@ func Export(filters ...string) ([]Task, error) { } func run(args ...string) error { - if debugWriter != nil { - fmt.Fprintln(debugWriter, "task "+strings.Join(args, " ")) + if dbg.writer != nil { + fmt.Fprintln(dbg.writer, "task "+strings.Join(args, " ")) } cmd := exec.Command("task", args...) |
