refactor: archieplaybooks is not required for some time now, webui is where playbooks are defined
This commit is contained in:
4 files changed
-222
No files matched your search
@@ -7,7 +7,6 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/app/archied"
|
||||
"github.com/samcharles93/archie-core/internal/app/archieplaybooks"
|
||||
"github.com/samcharles93/archie-core/internal/app/prbench"
|
||||
)
|
||||
|
||||
@@ -35,8 +34,6 @@ func run(args []string) int {
|
||||
return archied.RunStatus(args[1:], os.Stdout, os.Stderr)
|
||||
case "prbench":
|
||||
return prbench.Main(args[1:], os.Stdout, os.Stderr)
|
||||
case "playbooks":
|
||||
return archieplaybooks.Run(args[1:], os.Stderr)
|
||||
}
|
||||
// A word that is not a subcommand is refused rather than ignored: a
|
||||
// stale `archied update-topology` would otherwise start a second
|
||||
|
||||
@@ -1,78 +0,0 @@
|
||||
package archieplaybooks
|
||||
|
||||
import (
|
||||
"context"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
// Run runs `archied playbooks <command>`: lint for CI, serve for editors.
|
||||
// Both use the loaders the daemon runs, so a check here cannot disagree with
|
||||
// what the daemon accepts at startup.
|
||||
func Run(args []string, stderr io.Writer) int {
|
||||
commands := map[string]func([]string, io.Writer) int{
|
||||
"lint": runLint,
|
||||
"serve": runServe,
|
||||
}
|
||||
|
||||
if len(args) == 0 {
|
||||
fmt.Fprintln(stderr, "usage: archied 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, "archied playbooks: unknown command %q\n", args[0])
|
||||
return 2
|
||||
}
|
||||
return cmd(args[1:], stderr)
|
||||
}
|
||||
|
||||
// runLint validates an EDA playbook directory against the compiler the daemon
|
||||
// runs. Exit codes: 0 clean, 1 findings, 2 usage error.
|
||||
func runLint(args []string, stderr io.Writer) int {
|
||||
flags := flag.NewFlagSet("archied playbooks lint", flag.ContinueOnError)
|
||||
flags.SetOutput(stderr)
|
||||
edaDir := flags.String("eda-dir", "", "EDA playbook directory to lint")
|
||||
if err := flags.Parse(args); err != nil {
|
||||
return 2 // flag.ContinueOnError already printed the message
|
||||
}
|
||||
if *edaDir == "" {
|
||||
fmt.Fprintln(stderr, "lint: -eda-dir is required")
|
||||
flags.Usage()
|
||||
return 2
|
||||
}
|
||||
return LintEDA(*edaDir, stderr).ExitCode
|
||||
}
|
||||
|
||||
// runServe runs the language server on stdin/stdout until the editor
|
||||
// disconnects or the process is signalled.
|
||||
func runServe(args []string, stderr io.Writer) int {
|
||||
if len(args) > 0 {
|
||||
fmt.Fprintln(stderr, "serve: takes no arguments; the editor speaks LSP on stdin/stdout")
|
||||
return 2
|
||||
}
|
||||
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
|
||||
defer stop()
|
||||
if err := Serve(ctx, stdio{}); err != nil {
|
||||
fmt.Fprintln(stderr, "serve:", err)
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
// stdio joins stdin and stdout into the one stream the language server reads
|
||||
// and writes.
|
||||
type stdio struct{}
|
||||
|
||||
func (stdio) Read(p []byte) (int, error) { return os.Stdin.Read(p) }
|
||||
func (stdio) Write(p []byte) (int, error) { return os.Stdout.Write(p) }
|
||||
func (stdio) Close() error { return os.Stdin.Close() }
|
||||
@@ -1,29 +0,0 @@
|
||||
// Package archieplaybooks implements the `archied playbooks` command, using the
|
||||
// daemon's own playbook loaders.
|
||||
package archieplaybooks
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"log/slog"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/eda/module"
|
||||
"github.com/samcharles93/archie-core/internal/domain/eda/playbook"
|
||||
)
|
||||
|
||||
// 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
|
||||
}
|
||||
|
||||
// LintEDA validates a directory of EDA playbook documents with the compiler
|
||||
// the eda-playbooks resource validates against.
|
||||
func LintEDA(dir string, stderr io.Writer) Result {
|
||||
if _, err := playbook.Load(dir, module.New(slog.New(slog.DiscardHandler))); err != nil {
|
||||
fmt.Fprintln(stderr, err)
|
||||
return Result{ExitCode: 1, Findings: []string{err.Error()}}
|
||||
}
|
||||
return Result{}
|
||||
}
|
||||
@@ -1,112 +0,0 @@
|
||||
package archieplaybooks
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"go.lsp.dev/jsonrpc2"
|
||||
"go.lsp.dev/protocol"
|
||||
"go.lsp.dev/uri"
|
||||
)
|
||||
|
||||
// Serve runs the language server over rwc until the client disconnects or ctx
|
||||
// ends, publishing diagnostics for the saved directory when a playbook is
|
||||
// opened or saved.
|
||||
func Serve(ctx context.Context, rwc io.ReadWriteCloser) error {
|
||||
s := &server{exited: make(chan struct{})}
|
||||
_, conn, client := protocol.NewServer(ctx, s, jsonrpc2.NewStream(rwc))
|
||||
s.client = client
|
||||
select {
|
||||
case <-conn.Done():
|
||||
return nil
|
||||
case <-s.exited:
|
||||
case <-ctx.Done():
|
||||
}
|
||||
return conn.Close()
|
||||
}
|
||||
|
||||
type server struct {
|
||||
protocol.UnimplementedServer
|
||||
client protocol.Client
|
||||
// exited is closed by the exit notification. Serve closes the connection:
|
||||
// closing it from inside the handler would wait on that handler.
|
||||
exited chan struct{}
|
||||
exitOnce sync.Once
|
||||
}
|
||||
|
||||
func (s *server) Initialize(context.Context, *protocol.InitializeParams) (*protocol.InitializeResult, error) {
|
||||
// The server reads files from disk, so it needs opens, closes and saves,
|
||||
// not content changes.
|
||||
openClose, change := true, protocol.TextDocumentSyncKindNone
|
||||
return &protocol.InitializeResult{
|
||||
Capabilities: protocol.ServerCapabilities{
|
||||
TextDocumentSync: &protocol.TextDocumentSyncOptions{
|
||||
OpenClose: &openClose,
|
||||
Change: &change,
|
||||
Save: protocol.Boolean(true),
|
||||
},
|
||||
},
|
||||
ServerInfo: protocol.ServerInfo{Name: "archied-playbooks"},
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *server) Shutdown(context.Context) error { return nil }
|
||||
|
||||
func (s *server) Exit(context.Context) error {
|
||||
s.exitOnce.Do(func() { close(s.exited) })
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *server) DidOpen(ctx context.Context, p *protocol.DidOpenTextDocumentParams) error {
|
||||
return s.client.PublishDiagnostics(ctx, diagnose(p.TextDocument.URI))
|
||||
}
|
||||
|
||||
func (s *server) DidSave(ctx context.Context, p *protocol.DidSaveTextDocumentParams) error {
|
||||
return s.client.PublishDiagnostics(ctx, diagnose(p.TextDocument.URI))
|
||||
}
|
||||
|
||||
func (s *server) DidClose(ctx context.Context, p *protocol.DidCloseTextDocumentParams) error {
|
||||
return s.client.PublishDiagnostics(ctx, &protocol.PublishDiagnosticsParams{URI: p.TextDocument.URI, Diagnostics: []protocol.Diagnostic{}})
|
||||
}
|
||||
|
||||
// diagnose lints the directory holding u's file and returns the findings that
|
||||
// belong to that file: a finding naming another file of the directory belongs
|
||||
// there, and one naming this file without a line (a file-level error) goes on
|
||||
// its first line.
|
||||
func diagnose(u uri.URI) *protocol.PublishDiagnosticsParams {
|
||||
out := &protocol.PublishDiagnosticsParams{URI: u, Diagnostics: []protocol.Diagnostic{}}
|
||||
if !u.IsFile() {
|
||||
return out
|
||||
}
|
||||
path := u.FsPath()
|
||||
result := LintEDA(filepath.Dir(path), io.Discard)
|
||||
for _, finding := range result.Findings {
|
||||
line, ok := findingLine(finding, path)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
out.Diagnostics = append(out.Diagnostics, protocol.Diagnostic{
|
||||
Range: protocol.Range{Start: protocol.Position{Line: line}, End: protocol.Position{Line: line + 1}},
|
||||
Severity: protocol.DiagnosticSeverityError,
|
||||
Source: protocol.NewOptional("archied-playbooks"),
|
||||
Message: protocol.String(finding),
|
||||
})
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// findingLine places a finding on path. A compiler-style `path:line:` prefix
|
||||
// gives the 0-based line; a finding naming path without a line goes on line
|
||||
// 0; a finding naming another file is not path's.
|
||||
func findingLine(finding, path string) (uint32, bool) {
|
||||
if m := regexp.MustCompile(regexp.QuoteMeta(path) + `:(\d+):`).FindStringSubmatch(finding); m != nil {
|
||||
n, _ := strconv.ParseUint(m[1], 10, 32)
|
||||
return uint32(max(n, 1) - 1), true
|
||||
}
|
||||
return 0, strings.Contains(finding, path)
|
||||
}
|
||||
Reference in new issue
Block a user