feat(playbooks): standalone lint tool for playbook binding dirs

Fourth slice of docs/prds/eda-playbook-engine.md (t2db.12): the design
doc's pre-merge collision layer, as a gopls-shaped standalone binary
(cmd/archie-playbooks) rather than a Taskfile wrapper -- one binary, one
schema source, mode-based entrypoints.

- cmd/archie-playbooks: subcommand dispatch table from day one (lint now;
  an lsp/serve mode later slots in without restructuring), flag parsing
  only per organisation.md's cmd/ rule.
- internal/app/archieplaybooks: the lint mode's composition -- collects
  directories, calls workflow.LoadPlaybookDirs (the daemon's own loader),
  reports findings. No second validation path: the linter agrees with
  runtime startup validation by construction.
- Reuses LoadKindWorkflowsYAML/LoadLabelWorkflowsYAML/LoadPlaybookDirs
  directly; collision/malformed/invalid bindings exit non-zero.
- Known limitation recorded: findings are file-granular, not
  line-granular (yaml.Unmarshal into map discards line numbers; a
  file:line diagnostic needs a yaml.Node upgrade, out of scope here).
- Taskfile lint:playbooks target; PRD records the shipped shape and the
  file-granular caveat.
This commit is contained in:
sam committed 2026-09-03 03:07:27 +10:00
1 parent 556038e7cb
commit 8c19555b4a
6 files changed
+353

No files matched your search

+50
View File
@@ -0,0 +1,50 @@
// Package archieplaybooks is the application composition for the standalone
// playbook-validation CLI (cmd/archie-playbooks). It owns the lint mode's
// orchestration: collect the configured directories, run the domain
// validator, and shape the outcome for human consumption. All validation
// rules live in internal/domain/workflow (LoadKindWorkflowsYAML,
// LoadLabelWorkflowsYAML, LoadPlaybookDirs) -- this package only calls them
// and formats results; there is deliberately no second validation path
// here (per docs/prds/eda-playbook-engine.md and t2db.12).
package archieplaybooks
import (
"fmt"
"io"
"github.com/samcharles93/archie-core/internal/domain/workflow"
)
// Result is the outcome of one lint run: the exit code for the process and
// the human-readable findings.
type Result struct {
ExitCode int
Findings []string
}
// Lint validates one or more playbook directories against the domain
// loaders and reports every collision / malformed file / invalid binding as
// a finding. It returns exit code 0 on a clean set, 1 when any finding is
// reported. Directories and single-file fields share one validation source:
//
// workflow.LoadPlaybookDirs
//
// which already applies the same rules LoadKindWorkflowsYAML and
// LoadLabelWorkflowsYAML do, per file and across files -- so the linter can
// never disagree with the daemon's startup validation.
func Lint(dirs []string, stderr io.Writer) Result {
var findings []string
_, _, err := workflow.LoadPlaybookDirs(dirs)
if err != nil {
findings = append(findings, err.Error())
}
if len(findings) > 0 {
for _, f := range findings {
fmt.Fprintln(stderr, f)
}
return Result{ExitCode: 1, Findings: findings}
}
return Result{ExitCode: 0, Findings: nil}
}
+115
View File
@@ -0,0 +1,115 @@
package archieplaybooks
import (
"os"
"path/filepath"
"strings"
"testing"
)
// TestLintCleanExitsZero: a set of directories with no collisions lints
// cleanly -- exit 0, no findings.
func TestLintCleanExitsZero(t *testing.T) {
dir := t.TempDir()
writeFile(t, filepath.Join(dir, "routes.yaml"), "bug: tdd\n")
writeFile(t, filepath.Join(dir, "labels.yaml"), "security: security-review\n")
result := Lint([]string{dir}, os.Stdout)
if result.ExitCode != 0 {
t.Fatalf("Lint() exit = %d, want 0; findings: %#v", result.ExitCode, result.Findings)
}
if len(result.Findings) != 0 {
t.Fatalf("Lint() findings = %#v, want none", result.Findings)
}
}
// TestLintCrossDirectoryCollisionExitsNonZero is the load-bearing case: two
// directories binding the same label is a lint failure with a clear message
// naming the colliding key, and exit non-zero.
func TestLintCrossDirectoryCollisionExitsNonZero(t *testing.T) {
dirA := t.TempDir()
dirB := t.TempDir()
writeFile(t, filepath.Join(dirA, "a.yaml"), "security: security-review\n")
writeFile(t, filepath.Join(dirB, "b.yaml"), "security: other-security\n")
result := Lint([]string{dirA, dirB}, os.Stdout)
if result.ExitCode == 0 {
t.Fatal("Lint() exit = 0, want non-zero for collision")
}
if len(result.Findings) == 0 {
t.Fatal("Lint() findings = none, want the collision reported")
}
joined := strings.Join(result.Findings, "\n")
if !strings.Contains(joined, "security") {
t.Fatalf("Lint() findings do not name the colliding label: %q", joined)
}
}
// TestLintWithinDirectoryCollisionExitsNonZero: the more common real case --
// two files in one directory binding the same key.
func TestLintWithinDirectoryCollisionExitsNonZero(t *testing.T) {
dir := t.TempDir()
writeFile(t, filepath.Join(dir, "a.yaml"), "security: security-review\n")
writeFile(t, filepath.Join(dir, "b.yaml"), "security: other-security\n")
result := Lint([]string{dir}, os.Stdout)
if result.ExitCode == 0 {
t.Fatal("Lint() exit = 0, want non-zero for within-dir collision")
}
}
// TestLintMalformedFileIsReported: a malformed YAML file is a lint finding.
func TestLintMalformedFileIsReported(t *testing.T) {
dir := t.TempDir()
writeFile(t, filepath.Join(dir, "broken.yaml"), "\t: : :\n")
result := Lint([]string{dir}, os.Stdout)
if result.ExitCode == 0 {
t.Fatal("Lint() exit = 0, want non-zero for malformed file")
}
}
// TestLintEmptyDirsAreClean: no directories configured (or none exist) is a
// clean lint, not an error -- matching the loaders' empty-means-defaults
// convention.
func TestLintEmptyDirsAreClean(t *testing.T) {
result := Lint(nil, os.Stdout)
if result.ExitCode != 0 {
t.Fatalf("Lint(nil) exit = %d, want 0", result.ExitCode)
}
missing := filepath.Join(t.TempDir(), "does-not-exist")
result = Lint([]string{missing}, os.Stdout)
if result.ExitCode != 0 {
t.Fatalf("Lint(missing dir) exit = %d, want 0", result.ExitCode)
}
}
// TestLintReportsLoadErrorAsFinding: the linter surfaces the loader's
// definition failure verbatim -- it agrees with the daemon's startup
// validation because it IS the same validation, not a parallel one.
func TestLintReportsLoadErrorAsFinding(t *testing.T) {
dir := t.TempDir()
writeFile(t, filepath.Join(dir, "a.yaml"), "security: security-review\nbug: tdd\n")
writeFile(t, filepath.Join(dir, "b.yaml"), "security: other-security\nbug: custom-bug\n")
result := Lint([]string{dir}, os.Stdout)
if result.ExitCode == 0 {
t.Fatal("Lint() exit = 0, want non-zero")
}
joined := strings.Join(result.Findings, "\n")
if !strings.Contains(joined, "security") {
t.Fatalf("Lint() findings = %q, want the colliding key named", joined)
}
// Fail-fast like the daemon: one definition failure is reported (the
// loader returns the first), not an enumeration of every collision.
if len(result.Findings) != 1 {
t.Fatalf("Lint() findings = %d, want exactly 1 (loader fail-fast contract)", len(result.Findings))
}
}
func writeFile(t *testing.T, path, content string) {
t.Helper()
if err := os.WriteFile(path, []byte(content), 0o600); err != nil {
t.Fatalf("write %s: %v", path, err)
}
}