From 7ee429274bea8d67561b0eedc7ef07606d14c289 Mon Sep 17 00:00:00 2001 From: Hank Donnay Date: Tue, 22 Aug 2023 17:02:43 -0500 Subject: [PATCH] docs: move injecturls helper to dedicated package Signed-off-by: Hank Donnay --- book.toml | 2 +- docs/contributor/misc.md | 11 ++++--- .../cmd/mdbook-injecturls/main.go | 29 ++++--------------- 3 files changed, 11 insertions(+), 31 deletions(-) rename docs/injecturls.go => internal/cmd/mdbook-injecturls/main.go (85%) diff --git a/book.toml b/book.toml index 596c92f29..b87a0a08e 100644 --- a/book.toml +++ b/book.toml @@ -19,4 +19,4 @@ command = "go run github.com/quay/claircore/internal/cmd/mdbook-godoc" command = "go run docs/make_target.go" [preprocessor.injecturls] -command = "go run docs/injecturls.go" +command = "go run github.com/quay/claircore/internal/cmd/mdbook-injecturls" diff --git a/docs/contributor/misc.md b/docs/contributor/misc.md index fe5f4138d..09bb98e45 100644 --- a/docs/contributor/misc.md +++ b/docs/contributor/misc.md @@ -1,9 +1,8 @@ Here's various codebase conventions that don't have dedicated pages: - URLs - URLs in code should be annotated with a `//doc:url` directive comment. See the - the `docs/injecturls.go` file for documentation on how the preprocessor works. - The list of keywords isn't an allowlist, so an invocation like the following - should list the ones actually used in the documentation: - - git grep injecturls -- :/docs/*.md + URLs in code should be annotated with a `//doc:url` directive comment. See + the the `internal/cmd/mdbook-injecturls` command for documentation on how the + preprocessor works. The list of keywords isn't an allowlist, so an invocation + like the following should list the ones actually used in the documentation + using a command like `git grep injecturls -- :/docs/*.md`. diff --git a/docs/injecturls.go b/internal/cmd/mdbook-injecturls/main.go similarity index 85% rename from docs/injecturls.go rename to internal/cmd/mdbook-injecturls/main.go index 679a0b60e..7cef2131c 100644 --- a/docs/injecturls.go +++ b/internal/cmd/mdbook-injecturls/main.go @@ -1,6 +1,4 @@ -//go:build ignore - -// Injecturls is a helper meant to collect urls via a comment directive. +// Mdbook-injecturls is a helper meant to collect urls via a comment directive. // // Any string declaration with a directive like // @@ -17,15 +15,12 @@ package main import ( "context" - "encoding/json" "fmt" "go/ast" "go/parser" "go/token" "io/fs" "log" - "os" - "os/signal" "path/filepath" "regexp" "strconv" @@ -40,18 +35,10 @@ var ( ) func main() { - log.SetFlags(log.LstdFlags | log.Lmsgprefix) - log.SetPrefix("injecturls: ") - mdbook.Args(os.Args) - - cfg, book, err := mdbook.Decode(os.Stdin) - if err != nil { - panic(err) - } - ctx := context.Background() - ctx, cancel := signal.NotifyContext(ctx, os.Interrupt, os.Kill) - defer cancel() + mdbook.Main("injecturls", newProc) +} +func newProc(ctx context.Context, cfg *mdbook.Context) (*mdbook.Proc, error) { proc := mdbook.Proc{ Chapter: func(ctx context.Context, b *strings.Builder, c *mdbook.Chapter) error { if c.Path == nil { @@ -165,11 +152,5 @@ func main() { return nil }, } - if err := proc.Walk(ctx, book); err != nil { - panic(err) - } - - if err := json.NewEncoder(os.Stdout).Encode(&book); err != nil { - panic(err) - } + return &proc, nil }