core

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Sep 22, 2026 License: BSD-3-Clause Imports: 15 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DefaultPromptTailChars     = `$#%>\]:`
	DefaultPromptSuffixPattern = `.*[` + DefaultPromptTailChars + `]\s*$`
)

Variables

View Source
var (
	DefaultPromptRegex        = regexp.MustCompile(`\S+` + DefaultPromptSuffixPattern)
	FlexibleOptionPromptRegex = regexp.MustCompile(interceptor.FlexibleOptionPromptPattern)
	UsernameRegex             = regexp.MustCompile(`(?i).*(login|user(name)?):\s*$`)
	PasswordRegex             = regexp.MustCompile(`(?i).*pass(word)?:\s*$`)
)

Functions

func IsAuth

func IsAuth(err error) bool

func IsCanceled

func IsCanceled(err error) bool

func IsDial

func IsDial(err error) bool

func IsTimeout

func IsTimeout(err error) bool

Types

type Config

type Config struct {
	// 输出 io.Reader 中读取的原始数据,用于上层调试
	RawOut io.Writer

	// 从 io.Reader 中读取到数据后,用来过滤特殊字符的自定义函数,在 Decoder 前执行
	Filter filter.IFilter

	// 从 io.Reader 中读取到数据后,用来解码的自定义函数
	Decoder func(b []byte) ([]byte, error)

	// 命令行提示符的匹配规则
	PromptRegex []*regexp.Regexp

	// 是否自动纠正命令行提示符,仅当未指定 PromptRegex 时有效
	//	该参数为true时,会在默认规则第一次匹配到结束符时尝试修正匹配规则,某些情况下可能修正后的规则不如默认规则灵活,慎用
	AutoPrompt bool

	// 是否输出命令行提示符
	ShowPrompt bool

	// 调用 ReadToEndLine 时的确认次数
	ReadConfirm int
	// 调用 ReadToEndLine 时的确认间隔
	ReadConfirmWait time.Duration

	// 调用 ReadXXX 函数前的自定义回调函数
	BeforeRead func() error

	// 延迟触发 OnOut 的时间间隔
	//   如果需要在超过指定间隔或输出内容超过指定长度后再触发 OnOut、而不是实时触发 OnOut,可以指定 LazyOutInterval 和 LazyOutSize
	LazyOutInterval time.Duration
	// 延迟触发 OnOut 的缓冲区大小
	//   如果需要在超过指定间隔或输出内容超过指定长度后再触发 OnOut、而不是实时触发 OnOut,可以指定 LazyOutInterval 和 LazyOutSize
	LazyOutSize int
}

type Error

type Error struct {
	// Op is the operation which caused the error, such as "dial" or "auth".
	Op string
	// For operations involving a remote network connection.
	// like Dial, Read, or Write, Addr is the remote address of that connection.
	Addr string
	// Err is the error that occurred during the operation.
	Err error
}

func (*Error) Auth

func (e *Error) Auth() bool

是否是身份认证错误

func (*Error) Canceled

func (e *Error) Canceled() bool

是否是取消错误

func (*Error) Dial

func (e *Error) Dial() bool

是否是连接错误

func (*Error) Error

func (e *Error) Error() string

func (*Error) Name

func (e *Error) Name() string

func (*Error) Timeout

func (e *Error) Timeout() bool

是否是超时错误

func (*Error) Unwrap

func (e *Error) Unwrap() error

type ReadWriter

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

func New

func New(in io.Writer, out, err io.Reader, cfg Config) *ReadWriter

func (*ReadWriter) IsEndLine

func (r *ReadWriter) IsEndLine(s string) bool

func (*ReadWriter) Prompt

func (r *ReadWriter) Prompt() string

Prompt 命令交互过程中提示符可能发生变化,该方法获取最新的提示符

func (*ReadWriter) Read

func (r *ReadWriter) Read(ctx context.Context, stopOnEndLine bool, onOut func(lines []string), interceptors ...interceptor.Interceptor) (err error)

func (*ReadWriter) ReadAll

func (r *ReadWriter) ReadAll(timeout time.Duration, onOut func(lines []string), interceptors ...interceptor.Interceptor) (err error)

func (*ReadWriter) ReadAllContext

func (r *ReadWriter) ReadAllContext(ctx context.Context, onOut func(lines []string), interceptors ...interceptor.Interceptor) error

ReadAllContext 读取全部输出直到流结束,支持通过 context 取消

func (*ReadWriter) ReadToEndLine

func (r *ReadWriter) ReadToEndLine(timeout time.Duration, onOut func(lines []string), interceptors ...interceptor.Interceptor) (err error)

func (*ReadWriter) ReadToEndLineContext

func (r *ReadWriter) ReadToEndLineContext(ctx context.Context, onOut func(lines []string), interceptors ...interceptor.Interceptor) error

ReadToEndLineContext 读取输出直到提示符,支持通过 context 取消

func (*ReadWriter) Run

func (r *ReadWriter) Run(cmd string, timeout time.Duration, onOut func(lines []string), interceptors ...interceptor.Interceptor) error

Run 写入命令并读取输出直到提示符,等价于 Write + ReadToEndLine

func (*ReadWriter) RunAll

func (r *ReadWriter) RunAll(cmd string, timeout time.Duration, onOut func(lines []string), interceptors ...interceptor.Interceptor) error

RunAll 写入命令并读取全部输出直到流结束,等价于 Write + ReadAll

func (*ReadWriter) RunAllContext

func (r *ReadWriter) RunAllContext(ctx context.Context, cmd string, onOut func(lines []string), interceptors ...interceptor.Interceptor) error

RunAllContext 写入命令并读取全部输出直到流结束,支持通过 context 取消

func (*ReadWriter) RunContext

func (r *ReadWriter) RunContext(ctx context.Context, cmd string, onOut func(lines []string), interceptors ...interceptor.Interceptor) error

RunContext 写入命令并读取输出直到提示符,支持通过 context 取消

func (*ReadWriter) Stop

func (r *ReadWriter) Stop()

func (*ReadWriter) Write

func (r *ReadWriter) Write(cmd string) (err error)

Write 写入一个命令(自动在末尾补充 \n 换行符)。

func (*ReadWriter) WriteRaw

func (r *ReadWriter) WriteRaw(b []byte) (err error)

WriteRaw 向输入流写入指定内容(不自动补充换行符)。

Jump to

Keyboard shortcuts

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