executor

package
v0.0.16 Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2025 License: MIT Imports: 12 Imported by: 0

README

Executor 包结构说明

概述

executor 包提供了统一的命令执行、文件操作和 SSH tunnel 管理接口,支持本地和远程两种执行模式。

文件结构

internal/executor/
├── interface.go          # 接口定义(Session、Executor、TunnelCloser)
├── factory.go            # 工厂方法(根据配置创建对应的 Executor)
├── local.go              # 本地执行器实现
├── local_session.go      # 本地 Session 实现
├── remote.go             # 远程执行器实现
├── remote_session.go     # 远程 Session 实现
└── util.go               # 工具类型(noopCloser)

各文件说明

interface.go (49 行)

定义核心接口:

  • Session: 流式命令执行接口

    • StdoutPipe() - 获取标准输出
    • Start() - 启动命令
    • Wait() - 等待完成
    • Close() - 关闭 session
  • Executor: 执行器接口

    • Execute() - 执行 shell 命令
    • CreateSession() - 创建流式 session
    • UploadFile() - 上传/复制文件
    • CreateScript() - 创建脚本
    • CreateTunnel() - 创建 SSH tunnel
    • Close() - 关闭连接
    • IsRemote() - 判断模式
  • TunnelCloser: Tunnel 关闭接口

设计模式

1. 接口隔离原则 (ISP)
  • 清晰的接口定义分离在 interface.go
  • 实现细节分散在各自的文件中
2. 依赖倒置原则 (DIP)
  • 上层依赖 Executor 接口,不依赖具体实现
  • factory.go 负责创建具体实现
3. 适配器模式
  • LocalSession 和 RemoteSession 适配不同的底层 API
  • 统一的 Session 接口隐藏差异
4. 工厂模式
  • NewExecutor() 根据配置创建对应实例
  • NewLocalExecutor() 和 NewRemoteExecutor() 分别构造

使用示例

import "github.com/laysdragon/go-docker-dev-swap/internal/executor"

// 1. 创建 executor
exec, err := executor.NewExecutor(cfg)
if err != nil {
    return err
}
defer exec.Close()

// 2. 执行命令
output, err := exec.Execute("docker ps")

// 3. 流式命令执行
session, err := exec.CreateSession()
defer session.Close()

session.Start("docker logs -f container")
stdout, _ := session.StdoutPipe()

scanner := bufio.NewScanner(stdout)
for scanner.Scan() {
    fmt.Println(scanner.Text())
}

session.Wait()

// 4. 文件操作
err = exec.UploadFile("/local/path", "/remote/path")

// 5. 创建 tunnel(仅远程模式)
if exec.IsRemote() {
    tunnel, err := exec.CreateTunnel(2345, 2345)
    defer tunnel.Close()
}

代码统计

文件 行数 功能
interface.go 49 接口定义
factory.go 13 工厂方法
local.go 105 本地执行器
local_session.go 50 本地 Session
remote.go 64 远程执行器
remote_session.go 51 远程 Session
util.go 8 工具类型
总计 340 -

优势

✅ 模块化清晰
  • 每个文件职责单一
  • 易于定位和修改
✅ 可维护性高
  • 本地和远程实现完全分离
  • Session 实现独立管理
✅ 易于测试
  • 可以针对每个文件单独测试
  • Mock 接口简单
✅ 易于扩展
  • 添加新功能只需修改对应文件
  • 接口变更影响范围明确

未来扩展

如果需要添加新的执行模式(如 Docker exec、Kubernetes exec):

  1. 在 interface.go 中保持接口不变
  2. 创建新的实现文件:
    • docker.go - Docker executor 实现
    • docker_session.go - Docker session 实现
  3. 在 factory.go 中添加创建逻辑
  4. 上层代码无需任何修改

相关文档

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Executor

type Executor interface {
	// Execute 執行 shell 指令
	Execute(command string) (string, error)

	// CreateSession 建立一個流式執行 session
	CreateSession() (Session, error)

	// UploadFile 上傳/複製檔案
	UploadFile(localPath, remotePath string) error

	// CreateScript 建立腳本檔案
	CreateScript(script, path string) error

	// CreateTunnel 建立 SSH tunnel (僅遠端模式)
	CreateTunnel(localPort, remotePort int) (TunnelCloser, error)

	// Close 關閉連接
	Close() error

	// IsRemote 判斷是否為遠端模式
	IsRemote() bool
}

Executor 定義了執行操作的抽象接口

func NewExecutor

func NewExecutor(rc *config.RuntimeConfig) (Executor, error)

NewExecutor 根據配置建立對應的 Executor

type LocalExecutor

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

LocalExecutor 本地執行器

func NewLocalExecutor

func NewLocalExecutor(rc *config.RuntimeConfig) (*LocalExecutor, error)

NewLocalExecutor 創建本地執行器

func (*LocalExecutor) Close

func (e *LocalExecutor) Close() error

func (*LocalExecutor) CreateScript

func (e *LocalExecutor) CreateScript(script, path string) error

func (*LocalExecutor) CreateSession

func (e *LocalExecutor) CreateSession() (Session, error)

func (*LocalExecutor) CreateTunnel

func (e *LocalExecutor) CreateTunnel(localPort, remotePort int) (TunnelCloser, error)

func (*LocalExecutor) Execute

func (e *LocalExecutor) Execute(command string) (string, error)

func (*LocalExecutor) IsRemote

func (e *LocalExecutor) IsRemote() bool

func (*LocalExecutor) UploadFile

func (e *LocalExecutor) UploadFile(localPath, destPath string) error

type LocalSession

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

LocalSession 本地命令執行 session

func (*LocalSession) Close

func (s *LocalSession) Close() error

func (*LocalSession) Start

func (s *LocalSession) Start(command string) error

func (*LocalSession) StdoutPipe

func (s *LocalSession) StdoutPipe() (io.Reader, error)

func (*LocalSession) Wait

func (s *LocalSession) Wait() error

type RemoteExecutor

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

RemoteExecutor 遠端執行器

func NewRemoteExecutor

func NewRemoteExecutor(rc *config.RuntimeConfig) (*RemoteExecutor, error)

NewRemoteExecutor 創建遠端執行器

func (*RemoteExecutor) Close

func (e *RemoteExecutor) Close() error

func (*RemoteExecutor) CreateScript

func (e *RemoteExecutor) CreateScript(script, path string) error

func (*RemoteExecutor) CreateSession

func (e *RemoteExecutor) CreateSession() (Session, error)

func (*RemoteExecutor) CreateTunnel

func (e *RemoteExecutor) CreateTunnel(localPort, remotePort int) (TunnelCloser, error)

func (*RemoteExecutor) Execute

func (e *RemoteExecutor) Execute(command string) (string, error)

func (*RemoteExecutor) GetSSHClient

func (e *RemoteExecutor) GetSSHClient() *SSHClient

GetSSHClient 返回底層的 SSH client (僅用於需要直接訪問的場景)

func (*RemoteExecutor) IsRemote

func (e *RemoteExecutor) IsRemote() bool

func (*RemoteExecutor) UploadFile

func (e *RemoteExecutor) UploadFile(localPath, remotePath string) error

type RemoteSession

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

RemoteSession SSH 遠端執行 session

func (*RemoteSession) Close

func (s *RemoteSession) Close() error

func (*RemoteSession) Start

func (s *RemoteSession) Start(command string) error

func (*RemoteSession) StdoutPipe

func (s *RemoteSession) StdoutPipe() (io.Reader, error)

func (*RemoteSession) Wait

func (s *RemoteSession) Wait() error

type SSHClient added in v0.0.14

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

func NewSSHClient added in v0.0.14

func NewSSHClient(cfg config.RemoteHost) (*SSHClient, error)

func (*SSHClient) Close added in v0.0.14

func (c *SSHClient) Close() error

func (*SSHClient) CreateScript added in v0.0.14

func (c *SSHClient) CreateScript(script, path string) error

func (*SSHClient) CreateSession added in v0.0.14

func (c *SSHClient) CreateSession() (*ssh.Session, error)

func (*SSHClient) CreateTunnel added in v0.0.14

func (c *SSHClient) CreateTunnel(localPort, remotePort int) (*Tunnel, error)

func (*SSHClient) Execute added in v0.0.14

func (c *SSHClient) Execute(command string) (string, error)

func (*SSHClient) UploadFile added in v0.0.14

func (c *SSHClient) UploadFile(localPath, remotePath string) error

type Session

type Session interface {
	// StdoutPipe 返回標準輸出管道
	StdoutPipe() (io.Reader, error)

	// Start 啟動命令
	Start(command string) error

	// Wait 等待命令完成
	Wait() error

	// Close 關閉 session
	Close() error
}

Session 定義了流式命令執行的統一接口

type SudoWrapper added in v0.0.14

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

SudoWrapper 提供通用的 sudo 命令包装功能 支持临时提权 bash 执行命令,提高命令兼容性

func NewSudoWrapper added in v0.0.14

func NewSudoWrapper(enabled bool, password string) *SudoWrapper

NewSudoWrapper 创建 sudo 包装器

func (*SudoWrapper) Enabled added in v0.0.14

func (w *SudoWrapper) Enabled() bool

Enabled 返回是否启用 sudo

func (*SudoWrapper) HasPassword added in v0.0.14

func (w *SudoWrapper) HasPassword() bool

HasPassword 返回是否配置了密码

func (*SudoWrapper) Wrap added in v0.0.14

func (w *SudoWrapper) Wrap(command string) string

Wrap 将命令包装为带 sudo 的形式 使用临时提权 bash 来运行命令,提高兼容性

func (*SudoWrapper) WrapMultiple added in v0.0.14

func (w *SudoWrapper) WrapMultiple(parts ...string) string

WrapMultiple 包装多个命令参数组成的命令 例如: WrapMultiple("docker", "ps", "-a") -> sudo bash -c 'docker ps -a'

type Tunnel added in v0.0.14

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

func (*Tunnel) Close added in v0.0.14

func (t *Tunnel) Close() error

type TunnelCloser

type TunnelCloser interface {
	Close() error
}

TunnelCloser 定義了 tunnel 的關閉接口

Jump to

Keyboard shortcuts

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