executor

package
v0.14.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 20, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

Package executor provides a CommandExecutor implementation for CMD.EXE built-in commands and external-process dispatch.

Typical usage — full CMD.EXE compatibility:

proc.Executor = executor.New()

To extend with custom commands while keeping all built-ins:

reg := executor.New()
reg.HandleFunc("greet", func(p *processor.Processor, cmd *parser.SimpleCommand) error {
    fmt.Fprintln(p.Stdout, "hello,", strings.Join(cmd.Args, " "))
    return nil
})
proc.Executor = reg

To override a specific built-in:

reg := executor.New()
reg.HandleFunc("echo", myEcho)
proc.Executor = reg

To build a completely custom executor with no built-ins:

reg := executor.NewEmpty()
reg.HandleFunc("print", myPrint)
proc.Executor = reg

Index

Constants

View Source
const (
	ErrSyntaxIncorrect     = "The syntax of the command is incorrect.\n"
	ErrFileNotFound        = "The system cannot find the file specified.\n"
	ErrPathNotFound        = "The system cannot find the path specified.\n"
	ErrAccessDenied        = "Access is denied.\n"
	ErrFileNotFoundPattern = "Could Not Find %s\n"
	ErrDirNotEmpty         = "The directory is not empty.\n"
	ErrFileExists          = "A duplicate file name exists, or the file cannot be found\n"
	ErrCannotCopyOntoSelf  = "The file cannot be copied onto itself.\n"
	ErrFileAlreadyExists   = "A subdirectory or file %s already exists.\n"
)

Variables

View Source
var ErrCommandNotFound = fmt.Errorf("command not found")

Functions

func CommandHelp added in v0.3.0

func CommandHelp(name string) string

CommandHelp returns the help text for a CMD/batch command, or an empty string. Builtin command help is defined here; tool help is sourced from the tools package.

func CommandNames added in v0.3.0

func CommandNames() []string

CommandNames returns the names of all commands that have help text.

func VersionString

func VersionString() string

VersionString returns the string printed by the VER command and the interactive session banner.

It can be overridden by setting MSBATCH_VERSION in the host environment:

MSBATCH_VERSION="MyApp Runner [Version 2.1.0]"

When unset, the default is derived from the host OS at runtime:

  • Non-Windows: "uname -s" + "uname -r" → e.g. "Linux [Version 6.19.5-3-cachyos]"
  • Windows: output of "cmd /c ver" → e.g. "Microsoft Windows [Version 10.0.19045]"
  • Fallback (if exec fails): runtime.GOOS

Types

type Registry

type Registry struct {
	// contains filtered or unexported fields
}

Registry dispatches commands to registered handlers by name (case-insensitive). Unrecognised commands are forwarded to the fallback executor if one is set.

The zero value is valid but has no handlers and no fallback; commands are silently ignored. Use New to obtain a Registry pre-loaded with CMD.EXE built-ins, or NewEmpty for a blank slate.

func New

func New() *Registry

New returns a Registry pre-loaded with the full set of CMD.EXE built-in commands and an external-process fallback for unrecognised names.

func NewEmpty

func NewEmpty() *Registry

NewEmpty returns a Registry with no registered handlers and no fallback.

func (*Registry) ExecCommand

func (r *Registry) ExecCommand(p *processor.Processor, cmd *parser.SimpleCommand) error

ExecCommand looks up cmd.Name and calls the registered handler. Falls through to the fallback executor when the name is not registered.

func (*Registry) Handle

func (r *Registry) Handle(name string, h processor.CommandExecutor)

Handle registers h as the handler for the given command name. Names are matched case-insensitively. Registering the same name twice replaces the previous handler.

func (*Registry) HandleFunc

func (r *Registry) HandleFunc(name string, fn func(*processor.Processor, *parser.SimpleCommand) error)

HandleFunc registers fn as the handler for the given command name. It is shorthand for r.Handle(name, processor.CommandExecutorFunc(fn)).

func (*Registry) Names added in v0.1.3

func (r *Registry) Names() []string

Names returns a sorted list of all registered command names.

func (*Registry) NamesMap added in v0.9.0

func (r *Registry) NamesMap() map[string]bool

NamesMap returns a map of all registered command names for fast lookup.

func (*Registry) SetFallback

func (r *Registry) SetFallback(h processor.CommandExecutor)

SetFallback sets the executor used when no registered handler matches the command name. Pass nil to disable fallback (unrecognised commands are silently ignored).

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL