shell

package
v3.3.1 Latest Latest
Warning

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

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Exec

func Exec(shell string) (string, error)

Exec 执行 shell 命令

func ExecWithLog

func ExecWithLog(ctx context.Context, shell string, logFile string) error

ExecWithLog 执行 shell 命令并将输出写入指定的日志文件 ctx 取消时会杀死整个进程组

func ExecWithStdinProgress added in v3.3.1

func ExecWithStdinProgress(
	ctx context.Context,
	name string, args []string,
	stdin io.Reader, total int64,
	interval time.Duration,
	progress func(written, total int64, rate float64),
) (string, error)

ExecWithStdinProgress 以外部命令消费 stdin,同时按周期通过 progress 回调输出进度 name/args 为命令与参数; stdin 是被喂给命令的字节流; total 为已知总字节数(<=0 表示未知) interval 是进度回调周期; progress(written, total, rate) 中 rate 单位是 bytes/s pipe 存在背压,written 增速 ≈ 命令处理速率,可作为大概进度使用

func Execf

func Execf(shell string, args ...any) (string, error)

Execf 安全执行 shell 命令

func ExecfAsync

func ExecfAsync(shell string, args ...any) error

ExecfAsync 异步执行 shell 命令

func ExecfWithContext added in v3.3.1

func ExecfWithContext(ctx context.Context, shell string, args ...any) (string, error)

ExecfWithContext 安全执行 shell 命令,ctx 取消时终止进程

func ExecfWithDir

func ExecfWithDir(dir, shell string, args ...any) (string, error)

ExecfWithDir 在指定目录下执行 shell 命令

func ExecfWithOutput

func ExecfWithOutput(shell string, args ...any) error

ExecfWithOutput 执行 shell 命令并输出到终端

func ExecfWithPipe

func ExecfWithPipe(ctx context.Context, shell string, args ...any) (io.ReadCloser, error)

ExecfWithPipe 执行 shell 命令并返回管道

func ExecfWithTTY

func ExecfWithTTY(shell string, args ...any) (string, error)

ExecfWithTTY 在伪终端下执行 shell 命令

func ExecfWithTimeout

func ExecfWithTimeout(timeout time.Duration, shell string, args ...any) (string, error)

ExecfWithTimeout 执行 shell 命令并设置超时时间

func IsPTYError

func IsPTYError(err error) error

IsPTYError Linux kernel return EIO when attempting to read from a master pseudo terminal which no longer has an open slave. So ignore error here. See https://github.com/creack/pty/issues/21

Types

type MessagePing

type MessagePing struct {
	Ping bool `json:"ping"`
}

MessagePing ping 消息

type MessageResize

type MessageResize struct {
	Resize  bool `json:"resize"`
	Columns uint `json:"columns"`
	Rows    uint `json:"rows"`
}

MessageResize 终端大小调整消息

type Turn

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

Turn PTY 终端

func NewPTYTurn

func NewPTYTurn(ctx context.Context, ws *websocket.Conn, shell string, args ...any) (*Turn, error)

NewPTYTurn 使用 PTY 执行命令,返回 Turn 用于流式读取输出 调用方需要负责调用 Close() 和 Wait()

func (*Turn) Close

func (t *Turn) Close()

Close 关闭 PTY 并终止子进程

func (*Turn) Handle

func (t *Turn) Handle(ctx context.Context) error

Handle 从 WebSocket 读取输入写入 PTY

func (*Turn) Pipe

func (t *Turn) Pipe(ctx context.Context) error

Pipe 从 PTY 读取输出写入 WebSocket

func (*Turn) Resize

func (t *Turn) Resize(rows, cols uint16) error

Resize 调整 PTY 窗口大小

func (*Turn) Wait

func (t *Turn) Wait()

Wait 等待命令完成

func (*Turn) Write

func (t *Turn) Write(data []byte) (int, error)

Write 写入 PTY 输入

Jump to

Keyboard shortcuts

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