local

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Sep 10, 2026 License: MIT Imports: 13 Imported by: 0

README

Local Executor

Local Executor 实现了在本地机器上直接执行 Pipeline 节点的功能,无需额外的容器或远程环境。

功能特性

  • 在本地机器上直接执行流水线步骤
  • 支持自定义工作目录
  • 支持环境变量配置
  • 支持多步骤顺序执行
  • 支持自定义 Shell(bash/sh/powershell/cmd)
  • 支持执行超时设置
  • 支持伪终端模式(PTY)
  • 实时流式输出执行结果
  • 支持执行终止功能
  • 跨平台支持(Windows/Linux/macOS)

依赖

Local Executor 仅依赖 Go 标准库,无需额外安装。

配置示例

全局执行器配置
Executors:
  local:
    type: local
    config:
      workdir: /home/user/project      # 可选:工作目录
      shell: bash                       # 可选:指定 shell (bash/sh/powershell/cmd)
      timeout: 10m                      # 可选:默认超时时间
      pty: true                         # 可选:启用伪终端模式
      ptyWidth: 120                     # 可选:终端宽度(默认 80)
      ptyHeight: 40                     # 可选:终端高度(默认 24)
      env:                              # 可选:环境变量
        GO_VERSION: "1.21"
        NODE_ENV: production
节点配置
Nodes:
  Build:
    executor: local
    steps:
      - name: install-deps
        run: npm install
      - name: build
        run: npm run build

  Test:
    executor: local
    workdir: /home/user/project
    steps:
      - name: unit-test
        run: go test ./...
      - name: integration-test
        run: make test-integration

API 使用

基本使用
import (
    "context"
    "log"

    "github.com/LerkoX/flowx"
    "github.com/LerkoX/flowx/executor/local"
)

func main() {
    ctx := context.Background()

    // 创建执行器
    executor := local.NewLocalExecutor()

    // 配置执行器
    executor.SetWorkdir("/home/user/project")
    executor.SetEnv("CGO_ENABLED", "0")
    executor.SetShell("bash")

    // 准备环境(验证工作目录和 shell)
    if err := executor.Prepare(ctx); err != nil {
        log.Fatal(err)
    }
    defer executor.Destruction(ctx) // 确保终止正在执行的进程

    // 创建通信通道
    resultChan := make(chan any)   // 用于接收执行结果
    commandChan := make(chan any)  // 用于发送命令

    // 启动 Transfer 处理 goroutine
    go executor.Transfer(ctx, resultChan, commandChan, nil)

    // 发送步骤执行
    steps := []flowx.Step{
        {Name: "build", Run: "go build -o app ."},
        {Name: "test", Run: "go test ./..."},
    }
    commandChan <- steps

    // 接收执行结果
    for i := 0; i < len(steps); i++ {
        result := <-resultChan
        if r, ok := result.(*local.StepResult); ok {
            if r.Error != nil {
                log.Printf("Step %s failed: %v\n", r.StepName, r.Error)
            } else {
                log.Printf("Step %s succeeded\n", r.StepName)
            }
        }
    }
}
使用 Bridge 和 Adapter 模式
// 创建桥接器
bridge := local.NewLocalBridge()

// 创建适配器并配置
adapter := local.NewLocalAdapter()
config := map[string]any{
    "workdir": "/home/user/project",
    "shell": "bash",
    "timeout": "5m",
    "env": map[string]string{
        "GO_VERSION": "1.21",
    },
}

if err := adapter.Config(ctx, config); err != nil {
    log.Fatal(err)
}

// 通过桥接器创建执行器
executor, err := bridge.Conn(ctx, adapter)
if err != nil {
    log.Fatal(err)
}

接口实现

Local Executor 实现了以下接口:

接口 实现类型 说明
flowx.Executor LocalExecutor 执行器主接口
flowx.Adapter LocalAdapter 配置适配器接口
flowx.Bridge LocalBridge 连接桥接器接口

LocalExecutor 方法

构造函数
// 创建新的本地执行器
func NewLocalExecutor() *LocalExecutor
配置方法
func (l *LocalExecutor) SetWorkdir(workdir string)       // 设置工作目录
func (l *LocalExecutor) SetEnv(key, value string)        // 设置环境变量
func (l *LocalExecutor) SetShell(shell string)           // 设置 shell
func (l *LocalExecutor) SetTimeout(timeout time.Duration) // 设置默认超时
func (l *LocalExecutor) SetPTY(enabled bool)             // 设置是否启用伪终端
func (l *LocalExecutor) SetPTYSize(width, height int)    // 设置终端尺寸
生命周期方法
func (l *LocalExecutor) Prepare(ctx context.Context) error      // 准备执行环境
func (l *LocalExecutor) Transfer(ctx context.Context, resultChan chan<- any, commandChan <-chan any)  // 执行命令
func (l *LocalExecutor) Destruction(ctx context.Context) error  // 终止当前进程
获取配置方法
func (l *LocalExecutor) GetWorkdir() string   // 获取工作目录
func (l *LocalExecutor) GetShell() string     // 获取当前 shell

StepResult 结构

type StepResult struct {
    StepName   string    // 步骤名称
    Command    string    // 执行的命令
    Output     string    // 命令输出(预留字段)
    Error      error     // 执行错误(如果 exit code != 0)
    StartTime  time.Time // 开始时间
    FinishTime time.Time // 结束时间
}

跨平台支持

Local Executor 自动检测操作系统并使用相应的 shell:

操作系统 优先 Shell 回退 Shell
Windows PowerShell (pwsh/powershell) cmd
Linux/macOS bash sh

注意事项

  1. 工作目录

    • 如果指定了工作目录,Prepare() 会验证目录是否存在
    • 所有命令都会在指定的工作目录下执行
  2. Shell 选择

    • Windows 优先使用 PowerShell,回退到 cmd
    • Unix-like 系统优先使用 bash,回退到 sh
    • 可以手动指定 shell(如 SetShell("zsh")
  3. 执行终止

    • Destruction() 会终止当前正在执行的进程
    • 当 context 被取消时,也会自动终止进程
    • 先尝试发送中断信号(Unix)或 Ctrl+Break(Windows),如果失败则强制终止
  4. 超时设置

    • 可以为每个执行器设置默认超时时间
    • 超时后会取消执行并返回错误
    • 设置为 0 表示无超时限制
  5. 伪终端模式(PTY)

    • 启用 PTY 后,命令会在伪终端中执行
    • 适用于需要终端交互的程序
    • 可以设置终端尺寸(宽度/高度)
  6. 环境变量

    • 继承当前进程的所有环境变量
    • 通过 SetEnv 设置的变量会覆盖现有变量

架构说明

┌─────────────────┐     ┌─────────────────┐     ┌─────────────────┐
│   LocalBridge   │────▶│   LocalAdapter  │────▶│  LocalExecutor  │
│   (连接管理)     │     │   (配置管理)     │     │   (执行管理)     │
└─────────────────┘     └─────────────────┘     └─────────────────┘
                                                        │
                                                        ▼
                                               ┌─────────────────┐
                                               │  os/exec 包      │
                                               │  (Go 标准库)      │
                                               └─────────────────┘
                                                        │
                                                        ▼
                                               ┌─────────────────┐
                                               │   本地 Shell     │
                                               │  (bash/sh/cmd)  │
                                               └─────────────────┘

测试

# 运行 Local Executor 测试
go test ./executor/local/ -v

# 运行所有测试
go test ./... -v

注意:测试会在本地机器上实际执行命令,请确保测试环境中的命令安全。

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type LocalAdapter

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

LocalAdapter 本地适配器实现

func NewLocalAdapter

func NewLocalAdapter() *LocalAdapter

NewLocalAdapter 创建新的本地适配器

func (*LocalAdapter) Config

func (a *LocalAdapter) Config(ctx context.Context, config map[string]any) error

Config 配置适配器 支持的配置项:

  • workdir: 工作目录 string
  • env: 环境变量 map[string]string
  • shell: 指定shell string (例如: "bash", "sh", "powershell", "cmd")
  • timeout: 默认超时时间 string (例如: "30s", "5m")
  • pty: 是否启用伪终端 bool
  • ptyWidth: 终端宽度 int(默认 80)
  • ptyHeight: 终端高度 int(默认 24)

type LocalBridge

type LocalBridge struct{}

LocalBridge 本地桥接器实现

func NewLocalBridge

func NewLocalBridge() *LocalBridge

NewLocalBridge 创建新的本地桥接器

func (*LocalBridge) Conn

func (b *LocalBridge) Conn(ctx context.Context, adapter executor.Adapter) (executor.Executor, error)

Conn 连接到本地环境并创建执行器 adapter: 适配器,包含本地执行配置 返回: 本地执行器实例

type LocalExecutor

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

LocalExecutor 本地执行器实现

func NewLocalExecutor

func NewLocalExecutor() *LocalExecutor

NewLocalExecutor 创建新的本地执行器

func (*LocalExecutor) Destruction

func (l *LocalExecutor) Destruction(ctx context.Context) error

Destruction 销毁本地执行环境 本地执行器不需要特殊的清理,但会终止正在运行的命令

func (*LocalExecutor) GetInstanceId

func (l *LocalExecutor) GetInstanceId() string

GetInstanceId 获取实例ID(本地执行器没有持久化的实例,返回空)

func (*LocalExecutor) GetRuntimeInfo

func (l *LocalExecutor) GetRuntimeInfo() map[string]any

GetRuntimeInfo 获取运行时信息

func (*LocalExecutor) GetShell

func (l *LocalExecutor) GetShell() string

GetShell 获取当前shell

func (*LocalExecutor) GetType

func (l *LocalExecutor) GetType() string

GetType 获取executor类型

func (*LocalExecutor) GetWorkdir

func (l *LocalExecutor) GetWorkdir() string

GetWorkdir 获取工作目录

func (*LocalExecutor) Prepare

func (l *LocalExecutor) Prepare(ctx context.Context) error

Prepare 准备本地执行环境 本地执行器不需要特殊的准备,只需要验证工作目录

func (*LocalExecutor) Transfer

func (l *LocalExecutor) Transfer(ctx context.Context, resultChan chan<- any, commandChan <-chan any, inputChan <-chan []byte)

Transfer 接收命令并执行 只支持 string 类型的命令 inputChan 用于接收交互式输入数据,可为 nil(不需要输入时)

当 ctx 被取消时,会立即停止执行新命令,并终止当前正在执行的进程

Jump to

Keyboard shortcuts

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