shell

package
v3.3.6 Latest Latest
Warning

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

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ApplyEnv added in v3.3.2

func ApplyEnv(cmd *exec.Cmd, env ...string)

func Exec

func Exec(ctx context.Context, shell string) (string, error)

Exec 执行已拼好的 shell 命令

func ExecWithLog

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

ExecWithLog 执行 shell 命令并将输出覆盖写入指定的日志文件

func ExecWithLogAppend added in v3.3.6

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

ExecWithLogAppend 执行 shell 命令并将输出追加到指定的日志文件

func ExecWithOutput added in v3.3.6

func ExecWithOutput(ctx context.Context, shell string) error

ExecWithOutput 执行已拼好的 shell 命令并输出到终端

func ExecWithPipe added in v3.3.6

func ExecWithPipe(ctx context.Context, shell string) (io.ReadCloser, error)

ExecWithPipe 执行已拼好的 shell 命令并返回管道

func ExecWithStdinProgress added in v3.3.1

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

ExecWithStdinProgress 以外部命令消费 stdin,同时按周期通过 progress 回调输出进度

func Execf

func Execf(ctx context.Context, format string, args ...any) (string, error)

Execf 按格式串拼出 shell 命令后执行

func ExecfAsync

func ExecfAsync(ctx context.Context, format string, args ...any) error

ExecfAsync 异步执行 shell 命令 异步即命令要活过调用方,入口断开取消链,否则 sleep 1 && systemctl restart acepanel 这类自杀式操作 会在 HTTP 响应写完的瞬间被杀且无人察觉

func ExecfWithDir

func ExecfWithDir(ctx context.Context, dir, format string, args ...any) (string, error)

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

func ExecfWithEnv added in v3.3.2

func ExecfWithEnv(ctx context.Context, env []string, format string, args ...any) (string, error)

ExecfWithEnv 安全执行 shell 命令,环境变量仅注入子进程

func ExecfWithOutput

func ExecfWithOutput(ctx context.Context, format string, args ...any) error

ExecfWithOutput 按格式串拼出 shell 命令后执行并输出到终端

func ExecfWithPipe

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

ExecfWithPipe 按格式串拼出 shell 命令后执行并返回管道

func ExecfWithTimeout

func ExecfWithTimeout(ctx context.Context, timeout time.Duration, format string, args ...any) (string, error)

ExecfWithTimeout 执行 shell 命令并设置超时时间,ctx 取消或超时到期均终止进程

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