miller/pkg/terminals/terminals.go
John Kerl 2f3e54d1f6
Remove os.Exit callsites below the entrypoint: phase 4 (plans/exit.md) (#2205)
Phase 4 of plans/exit.md: the aux/terminal sub-entrypoints. pkg/auxents and
pkg/terminals are now os.Exit-free; even the dispatchers return exit codes
instead of exiting.

- auxents.Dispatch returns (handled bool, exitCode int); entrypoint.Main --
  the one sanctioned exit point -- exits with the code.
- terminals.Dispatch returns an exit code; the climain caller wraps it in a
  lib.ExitRequest, which also retires the 'terminal did not exit the process'
  panic.
- Usage functions (hex, unhex, lecat, termcvt, repl, script, regtest) no
  longer take an exitCode parameter and exit; call sites print usage and
  return the code explicitly.
- Inner I/O helpers (hexDumpFile, unhexFile, lecatFile, termcvtFile) return
  errors; each sub-main prints 'mlr {verb}: ...' as before and returns 1.
  lecatFile formerly looped forever on a non-EOF read error; it now returns
  the error.
- The regtest directory walk (Execute / executeSinglePath /
  executeSingleDirectory / hasCaseSubdirectories) plumbs errors up to
  RegTestMain; regression_test.go updated for the new Execute signature.

Stderr messages and exit codes are unchanged across mlr aux-list, hex, unhex,
lecat, termcvt, help, version, repl, script, and regtest surfaces (checked by
hand); all 4779 regression cases pass; make lint is clean; docs rebuild with
zero churn.

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
2026-07-15 15:27:26 -04:00

100 lines
2.9 KiB
Go

// Support for Miller regression testing. Originally bash scripts; ported to Go
// for ease of Windows-native testing.
package terminals
import (
"fmt"
"os"
"runtime"
"github.com/johnkerl/miller/v6/pkg/terminals/completion"
"github.com/johnkerl/miller/v6/pkg/terminals/help"
"github.com/johnkerl/miller/v6/pkg/terminals/mcp"
"github.com/johnkerl/miller/v6/pkg/terminals/registry"
"github.com/johnkerl/miller/v6/pkg/terminals/regtest"
"github.com/johnkerl/miller/v6/pkg/terminals/repl"
"github.com/johnkerl/miller/v6/pkg/terminals/script"
"github.com/johnkerl/miller/v6/pkg/terminals/skill"
"github.com/johnkerl/miller/v6/pkg/version"
)
// tTerminalMain is a function-pointer type for the entrypoint handler for a given terminal,
// such as 'help' or 'regtest'.
type tTerminalMain func(args []string) int
type tTerminalLookupEntry struct {
name string
main tTerminalMain
}
// _TERMINAL_LOOKUP_TABLE is the lookup table for terminals. We get a Golang
// "initialization loop" if this is defined statically. So, we use a "package
// init" function.
var _TERMINAL_LOOKUP_TABLE = []tTerminalLookupEntry{}
func init() {
_TERMINAL_LOOKUP_TABLE = []tTerminalLookupEntry{
{registry.TerminalList, terminalListMain},
{registry.Completion, completion.CompletionMain},
{registry.Help, help.HelpMain},
{registry.Mcp, mcp.McpMain},
{registry.Regtest, regtest.RegTestMain},
{registry.Repl, repl.ReplMain},
{registry.Script, script.ScriptMain},
{registry.Skill, skill.SkillMain},
{registry.Version, showVersion},
{registry.Which, help.WhichMain},
}
}
func Dispatchable(arg string) bool {
for _, entry := range _TERMINAL_LOOKUP_TABLE {
if arg == entry.name {
return true
}
}
return false
}
// Dispatch runs the terminal named by args[0] and returns the process exit
// code for the caller to propagate (via lib.ExitRequest) up to the entrypoint
// layer.
func Dispatch(args []string) int {
if len(args) < 1 {
// Can't happen: the climain caller passes a non-empty terminal sequence.
fmt.Fprintf(os.Stderr, "mlr: internal coding error: empty terminal sequence.\n")
return 1
}
terminal := args[0]
for _, entry := range _TERMINAL_LOOKUP_TABLE {
if terminal == entry.name {
return entry.main(args)
}
}
fmt.Fprintf(os.Stderr, "mlr: terminal \"%s\" not found.\n", terminal)
return 1
}
// terminalListMain is the handler for 'mlr terminal-list'.
func terminalListMain(args []string) int {
ShowTerminalEntries(os.Stdout)
return 0
}
// ShowTerminalEntries is a symbol is exported for 'mlr --help'.
func ShowTerminalEntries(o *os.File) {
fmt.Fprintf(o, "Available subcommands:\n")
for _, entry := range _TERMINAL_LOOKUP_TABLE {
fmt.Fprintf(o, " %s\n", entry.name)
}
fmt.Fprintf(o, "For more information, please invoke mlr {subcommand} --help.\n")
}
func showVersion(args []string) int {
fmt.Printf("mlr version %s for %s/%s/%s\n",
version.STRING, runtime.GOOS, runtime.GOARCH, runtime.Version())
return 0
}