Documentation ¶
Overview ¶
Package opshell - Operator's interactive shell
Index ¶
- Constants
- Variables
- func CutCommand(s string) ([]string, error)
- func DefaultErrorHandler[T any](s *Shell[T], line string, err error) error
- type Config
- type PTYConfig
- type Shell
- func (s *Shell[T]) Cdr() *subcom.Cdr[*Shell[T]]
- func (s *Shell[T]) ErrorLogf(format string, args ...any)
- func (s *Shell[T]) ErrorWrite(b []byte) (int, error)
- func (s *Shell[T]) Errorf(format string, args ...any) (int, error)
- func (s *Shell[T]) HandleCommand(cmd string) error
- func (s *Shell[T]) HandleCommands() error
- func (s *Shell[T]) InPTYMode() bool
- func (s *Shell[T]) Logf(format string, args ...any)
- func (s *Shell[T]) Printf(format string, args ...any) (int, error)
- func (s *Shell[T]) ReadLine() (string, error)
- func (s *Shell[T]) ResetTerm() error
- func (s *Shell[T]) SetCommandErrorHandler(h func(s *Shell[T], line string, err error) error)
- func (s *Shell[T]) SetPrompt(prompt string)
- func (s *Shell[T]) SetSize(width, height int) error
- func (s *Shell[T]) SetSplitter(f func(string) ([]string, error))
- func (s *Shell[T]) SetV(v T)
- func (s *Shell[T]) V() T
- func (s *Shell[T]) Write(b []byte) (int, error)
- type SplitError
Examples ¶
Constants ¶
const DefaultPrompt = "> "
DefaultPrompt is the default prompt used if none is specified in a Config.
Variables ¶
var DefaultSplitter = simpleshsplit.SplitGoUnquote
DefaultSplitter is the default function used for splitting commands into arguments.
var ErrQuit = errors.New("quit requested")
ErrQuit may returned by a command handler to cause Shell.HandleCommands to stop handling commands. It is intended to be returned from handler for a quit command.
Functions ¶
func CutCommand ¶
CutCommand is similar to strings.Cut, but returns the first non-whitespace portion and the remainder of the string with leading whitespace removed along with a nil error. The returned slice will have a length of 0 if s was empty or whitespace, 1 if s didn't have two non-whitespace parts separated by whitespace, or 2 in any other case.
Example ¶
parts, err := CutCommand("foo bar tridge") if nil != err { panic(err) } for _, v := range parts { fmt.Println(v) }
Output: foo bar tridge
Types ¶
type Config ¶
type Config[T any] struct { // Reader is from where the shell draws its input. If unset, os.Stdin // will be used. Reader io.Reader // Writer is to where the shell sends its output. If unset, os.Stdout // will be used. Writer io.Writer // ErrWriter is where the shell sends error output via Shell.Errorf and // friends when not in PTY mode. ErrorWriter io.Writer // PTYMode determines whether or not shells created with this config // use PTY mode, i.e. golang.org/x/term.Terminals under the hood. If // unset or set to PTYDefault, PTY mode is enabled if Reader is // a terminal, as determined by golang.org/x/term.IsTerminal. When in // PTY mode, ErrorWriter is ignored and all output goes to Writer. PTYMode PTYConfig // Prompt sets the initial prompt. If unset, DefaultPrompt is used. Prompt string }
Config is the configuration needed to create a Shell. See the documentation for Shell for more information. The shell referred to in Config's fields' documentation is a shell created using Config.New.
type PTYConfig ¶
type PTYConfig int
PTYConfig is used in Config to indicate whether or not to enable PTY mode. See the documentation for Config.PTY for more details.
type Shell ¶
type Shell[T any] struct { // contains filtered or unexported fields }
Shell represents the interface between an operator and the rest of the program. See the documentation for Shell.V for more information about T.
func (*Shell[T]) Cdr ¶
Cdr returns s's Cdr. This should be used to set command handlers before processing commands via s.HandleCommand or s.HandleCommands.
func (*Shell[T]) ErrorLogf ¶
ErrorLogf is identical to Logf, but writes to s's ErrorWriter if not in PTY mode.
func (*Shell[T]) ErrorWrite ¶
ErrorWrite is identical to PrintWrite, but writes to s's ErrorWriter if not in PTY mode.
func (*Shell[T]) Errorf ¶
Errorf is identical to Printf, but writes to s's ErrorWriter if not in PTY mode.
func (*Shell[T]) HandleCommand ¶
HandleCommand handles a single command sent to the shell. If the command fails to split, a SplitError is returned. This is useful for filtering lines returned by s.ReadLine before command processing.
func (*Shell[T]) HandleCommands ¶
HandleCommands reads lines from s and uses the configured subcom.Cdr to handle commands. If a command handler returns ErrQuit, command handling will stop and HandleCommands will return ErrQuit.
func (*Shell[T]) ResetTerm ¶
ResetTerm resets s's underlying Terminal to its original state if it was put into raw mode due to s being in PTY mode. ResetTerm should be called to prevent stdio being in raw mode on program exit and is safe to call even if s is not in PTY mode.
func (*Shell[T]) SetCommandErrorHandler ¶
SetCommandErrorHandler sets a handler which s.HandleCommands will call when an error handling a command is encountered. If f is nil, DefaultErrorHandler will be used. If the handler returns an error, it will be returned by s.HandleCommands.
In particular, two types of errors should be expected: SplitError, which indicates that a command was unable to be split into arguments, and subcom.ErrNotFound, which indicates that the underlying subcom.Cdr didn't have a handler for the requested command.
func (*Shell[T]) SetPrompt ¶
SetPrompt sets the prompt s presents to the user if in PTY mode and is a no-op if not in PTY mode.
func (*Shell[T]) SetSize ¶
SetSize sets s's size in PTY mode and is a no-op otherwise. In PTY mode, s's size will be automatically changed upon receipt of SIGWINCH.
func (*Shell[T]) SetSplitter ¶
SetSplitter sets the function s.HandleCommand and s.HandleCommands use to split command lines into arguments. If f is nil, DefaultSplitter will be used.
func (*Shell[T]) SetV ¶
func (s *Shell[T]) SetV(v T)
SetV changes the value returned by s.V. Under the hood, it stores a pointer to v, not v itself.
type SplitError ¶
type SplitError struct {
Err error
}
SplitError is a decorator indicating the string passed to HandleCommand failed to split into arguments; no command was executed.
func (SplitError) Error ¶
func (err SplitError) Error() string
Error implements the error interface.