2026-01-07 17:54:56 +01:00
|
|
|
// Package tools provides tools to work with the wand library. The functions in this package serve primarily as
|
|
|
|
|
// the implementation of the wand executable command.
|
2025-09-01 02:07:48 +02:00
|
|
|
package tools
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"code.squareroundforest.org/arpio/docreflect/generate"
|
|
|
|
|
"fmt"
|
2025-09-01 04:10:35 +02:00
|
|
|
"io"
|
2026-01-07 17:54:56 +01:00
|
|
|
"os"
|
2025-09-01 02:07:48 +02:00
|
|
|
)
|
|
|
|
|
|
2026-01-07 17:54:56 +01:00
|
|
|
// ManOptions represents input options for generating man pages for a command created with wand.
|
2025-12-10 20:31:10 +01:00
|
|
|
type ManOptions struct {
|
2026-01-07 17:54:56 +01:00
|
|
|
|
|
|
|
|
// DateString provides the value for the release date field in a man page.
|
2025-12-10 20:31:10 +01:00
|
|
|
DateString string
|
2026-01-07 17:54:56 +01:00
|
|
|
|
|
|
|
|
// Version provides the value for the release version field in a man page.
|
|
|
|
|
Version string
|
2025-12-10 20:31:10 +01:00
|
|
|
}
|
|
|
|
|
|
2026-01-07 17:54:56 +01:00
|
|
|
// MarkdownOptions represents input options for generating markdown documents for a command created with wand.
|
2025-09-01 02:07:48 +02:00
|
|
|
type MarkdownOptions struct {
|
2026-01-07 17:54:56 +01:00
|
|
|
|
|
|
|
|
// Level provides a default title level offset for the titles in a markdown document.
|
2025-09-01 02:07:48 +02:00
|
|
|
Level int
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-07 17:54:56 +01:00
|
|
|
// ExecOptions represents input options for executing a function from the command line using the wand mechanism.
|
|
|
|
|
type ExecOptions struct {
|
|
|
|
|
|
|
|
|
|
// NoCache causes the execution to avoid relying on the cached artifacts resulting from a previous
|
|
|
|
|
// execution.
|
|
|
|
|
NoCache bool
|
|
|
|
|
|
|
|
|
|
// ClearCache clears the cached artifacts resulting from a previous execution before the current execution.
|
|
|
|
|
ClearCache bool
|
|
|
|
|
|
|
|
|
|
// CacheDir specifies a custom cache dir for wand. Default: ~/.wand.
|
|
|
|
|
CacheDir string
|
|
|
|
|
|
|
|
|
|
// Import lists the packages required for the expression to be executed. It accepts versioned package
|
|
|
|
|
// specifications, or it uses the latest version. When a package spec is prefixed with alias= or .=, it will
|
|
|
|
|
// import the for the expression with the specified alias or inline.
|
|
|
|
|
Import []string
|
|
|
|
|
|
|
|
|
|
// ReplaceModule replaces imported go modules with local or other versions of the given modules. The format
|
|
|
|
|
// of the items is: <module-path>=<replacement-module-path>, like for the go mod edit -replace command.
|
|
|
|
|
ReplaceModule []string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Docreflect generates documentation from the go docs of a package to be included in the compiled binary and
|
|
|
|
|
// accessed by the automatic help and documentation generator.
|
|
|
|
|
//
|
|
|
|
|
// The packageName parameter specifies the package name for the generated Go code.
|
|
|
|
|
//
|
|
|
|
|
// The gopath arguments accept any number of package, package level symbol, or struct field paths. It is
|
|
|
|
|
// recommended to use package paths unless special circumstances.
|
2025-09-01 02:07:48 +02:00
|
|
|
func Docreflect(out io.Writer, packageName string, gopaths ...string) error {
|
|
|
|
|
return generate.GenerateRegistry(out, packageName, gopaths...)
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-07 17:54:56 +01:00
|
|
|
// Man generates a man page in roff format for a command defined with wand. It relies on the go doc entries
|
|
|
|
|
// extracted by Docreflect.
|
2025-12-10 20:31:10 +01:00
|
|
|
func Man(out io.Writer, o ManOptions, commandDir string) error {
|
|
|
|
|
return execCommandDir(
|
|
|
|
|
out,
|
|
|
|
|
commandDir,
|
|
|
|
|
"_wandgenerate=man",
|
|
|
|
|
fmt.Sprintf("_wandgeneratedate=%s", o.DateString),
|
|
|
|
|
fmt.Sprintf("_wandgenerateversion=%s", o.Version),
|
|
|
|
|
)
|
2025-09-01 02:07:48 +02:00
|
|
|
}
|
|
|
|
|
|
2026-01-07 17:54:56 +01:00
|
|
|
// Man generates a markdown page for a command defined with wand. It relies on the go doc entries extracted by
|
|
|
|
|
// Docreflect.
|
2025-09-01 02:07:48 +02:00
|
|
|
func Markdown(out io.Writer, o MarkdownOptions, commandDir string) error {
|
2025-12-10 20:31:10 +01:00
|
|
|
return execCommandDir(
|
|
|
|
|
out,
|
|
|
|
|
commandDir,
|
|
|
|
|
"_wandgenerate=markdown",
|
|
|
|
|
fmt.Sprintf("_wandmarkdownlevel=%d", o.Level),
|
|
|
|
|
)
|
2025-09-01 02:07:48 +02:00
|
|
|
}
|
2026-01-07 17:54:56 +01:00
|
|
|
|
|
|
|
|
// Exec executes a Go function expression parameterized by the subsequent command line arguments.
|
|
|
|
|
//
|
|
|
|
|
// It has two modes. When called with arguments, the first argument must be the function expression, and the
|
|
|
|
|
// rest of the arguments the command line flags and positional arguments. When no arguments are provided, it
|
|
|
|
|
// starts reading from the input, and considers every line as the expected function expression followed by the
|
|
|
|
|
// options and positional arguments. In the latter case, the escaping is mostly identical to that of the bash
|
|
|
|
|
// input. It keeps executing the input expressions until the input is closed (Ctrl+D).
|
|
|
|
|
func Exec(o ExecOptions, stdin io.Reader, stdout io.Writer, args ...string) error {
|
|
|
|
|
return execInput(o, stdin, stdout, os.Stderr, args...)
|
|
|
|
|
}
|