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:
1 parent
556038e7cb
commit
8c19555b4a
6 files changed
+353
No files matched your search
@@ -0,0 +1,85 @@
|
||||
// Command archie-playbooks is the standalone playbook tool for archie-core,
|
||||
// shaped like gopls: one binary, multiple invocation modes. It validates
|
||||
// playbook YAML binding files against the same schema the daemon loads at
|
||||
// startup, so a pre-merge check can never disagree with runtime validation.
|
||||
//
|
||||
// Today it ships one mode (lint). The command dispatch is a table from day
|
||||
// one, so a future serve/lsp mode slots in without restructuring -- both
|
||||
// modes are entrypoints into the same validation source in
|
||||
// internal/domain/workflow.
|
||||
//
|
||||
// Known limitation (as of this slice): findings are file-granular, not
|
||||
// line-granular. The loader decodes playbooks with yaml.Unmarshal into a
|
||||
// plain map, which discards line numbers; a compiler-style file:line
|
||||
// diagnostic needs a yaml.Node decoding upgrade, which is out of scope
|
||||
// here and tracked separately.
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/app/archieplaybooks"
|
||||
)
|
||||
|
||||
func main() {
|
||||
os.Exit(run(os.Args[1:], os.Stderr))
|
||||
}
|
||||
|
||||
func run(args []string, stderr io.Writer) int {
|
||||
// Subcommand table from day one (gopls shape): each mode owns its flag
|
||||
// set and returns an exit code. Adding serve/lsp later is a new entry
|
||||
// here, not a restructuring.
|
||||
commands := map[string]func([]string, io.Writer) int{
|
||||
"lint": runLint,
|
||||
}
|
||||
|
||||
if len(args) == 0 {
|
||||
fmt.Fprintln(stderr, "usage: archie-playbooks <command> [args]")
|
||||
fmt.Fprintln(stderr, "commands:")
|
||||
for name := range commands {
|
||||
fmt.Fprintf(stderr, " %s\n", name)
|
||||
}
|
||||
return 2
|
||||
}
|
||||
|
||||
cmd, ok := commands[args[0]]
|
||||
if !ok {
|
||||
fmt.Fprintf(stderr, "archie-playbooks: unknown command %q\n", args[0])
|
||||
return 2
|
||||
}
|
||||
return cmd(args[1:], stderr)
|
||||
}
|
||||
|
||||
// runLint validates one or more playbook directories against the domain
|
||||
// loaders and reports collisions / malformed files / invalid bindings.
|
||||
// Exit codes: 0 clean, 1 findings, 2 usage error.
|
||||
func runLint(args []string, stderr io.Writer) int {
|
||||
flags := flag.NewFlagSet("archie-playbooks lint", flag.ContinueOnError)
|
||||
flags.SetOutput(stderr)
|
||||
var dirs multiFlag
|
||||
flags.Var(&dirs, "dir", "playbook directory to lint (repeatable)")
|
||||
if err := flags.Parse(args); err != nil {
|
||||
return 2 // flag.ContinueOnError already printed the message
|
||||
}
|
||||
if len(dirs) == 0 {
|
||||
fmt.Fprintln(stderr, "lint: at least one -dir is required")
|
||||
flags.Usage()
|
||||
return 2
|
||||
}
|
||||
|
||||
result := archieplaybooks.Lint(dirs, stderr)
|
||||
return result.ExitCode
|
||||
}
|
||||
|
||||
// multiFlag collects repeated -dir flags.
|
||||
type multiFlag []string
|
||||
|
||||
func (m *multiFlag) String() string { return fmt.Sprint([]string(*m)) }
|
||||
|
||||
func (m *multiFlag) Set(v string) error {
|
||||
*m = append(*m, v)
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// TestRunUnknownCommandExits2: an unrecognised subcommand is a usage error.
|
||||
func TestRunUnknownCommandExits2(t *testing.T) {
|
||||
var stderr bytes.Buffer
|
||||
if code := run([]string{"frobnicate"}, &stderr); code != 2 {
|
||||
t.Fatalf("run(unknown) = %d, want 2", code)
|
||||
}
|
||||
if !strings.Contains(stderr.String(), "unknown command") {
|
||||
t.Fatalf("stderr = %q, want unknown-command message", stderr.String())
|
||||
}
|
||||
}
|
||||
|
||||
// TestRunNoCommandExits2: no subcommand is a usage error with the command
|
||||
// table printed (the gopls-shaped dispatch surface).
|
||||
func TestRunNoCommandExits2(t *testing.T) {
|
||||
var stderr bytes.Buffer
|
||||
if code := run(nil, &stderr); code != 2 {
|
||||
t.Fatalf("run(nil) = %d, want 2", code)
|
||||
}
|
||||
if !strings.Contains(stderr.String(), "lint") {
|
||||
t.Fatalf("stderr = %q, want lint listed in usage", stderr.String())
|
||||
}
|
||||
}
|
||||
|
||||
// TestRunLintCleanExits0: lint over a valid directory exits 0.
|
||||
func TestRunLintCleanExits0(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
writeFile(t, filepath.Join(dir, "routes.yaml"), "bug: tdd\n")
|
||||
|
||||
var stderr bytes.Buffer
|
||||
if code := run([]string{"lint", "-dir", dir}, &stderr); code != 0 {
|
||||
t.Fatalf("run(lint clean) = %d, want 0; stderr=%q", code, stderr.String())
|
||||
}
|
||||
}
|
||||
|
||||
// TestRunLintCollisionExits1: a cross-directory collision exits 1 and names
|
||||
// the colliding key.
|
||||
func TestRunLintCollisionExits1(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")
|
||||
|
||||
var stderr bytes.Buffer
|
||||
if code := run([]string{"lint", "-dir", dirA, "-dir", dirB}, &stderr); code != 1 {
|
||||
t.Fatalf("run(lint collision) = %d, want 1; stderr=%q", code, stderr.String())
|
||||
}
|
||||
if !strings.Contains(stderr.String(), "security") {
|
||||
t.Fatalf("stderr = %q, want the colliding label named", stderr.String())
|
||||
}
|
||||
}
|
||||
|
||||
// TestRunLintNoDirsExits2: lint without any -dir is a usage error.
|
||||
func TestRunLintNoDirsExits2(t *testing.T) {
|
||||
var stderr bytes.Buffer
|
||||
if code := run([]string{"lint"}, &stderr); code != 2 {
|
||||
t.Fatalf("run(lint no dirs) = %d, want 2", code)
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user