Skip to content

Commit 5bfea85

Browse files
committed
refactor: improve docker executor and constants
1. Fix DockerExecutor configuration: Update NewDockerExecutor to use DockerConfig struct, fix configuration in server and exec commands 2. Refactor Docker constants: Move magic strings to constants, apply minimum exposure principle, only expose necessary public constants 3. Code improvements: Better error handling, improved code readability, enhanced maintainability
1 parent ebecff9 commit 5bfea85

File tree

22 files changed

+1459
-191
lines changed

22 files changed

+1459
-191
lines changed

cmd/runshell/cmd/exec.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ var execCmd = &cobra.Command{
2828
// 创建执行器
2929
var exec types.Executor
3030
if execDockerImage != "" {
31-
exec = executor.NewDockerExecutor(execDockerImage)
31+
exec = executor.NewDockerExecutor(executor.DockerConfig{
32+
Image: execDockerImage,
33+
})
3234
} else {
3335
exec = executor.NewLocalExecutor()
3436
}

cmd/runshell/cmd/server.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ var serverCmd = &cobra.Command{
2828
// 创建执行器
2929
var exec types.Executor
3030
if dockerImage != "" {
31-
exec = executor.NewDockerExecutor(dockerImage)
31+
exec = executor.NewDockerExecutor(executor.DockerConfig{
32+
Image: dockerImage,
33+
})
3234
} else {
3335
exec = executor.NewLocalExecutor()
3436
}

examples/README.md

Lines changed: 46 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,63 @@
1-
# RunShell Pipeline Examples
1+
# RunShell Examples
22

3-
这个目录包含了 RunShell 的使用示例。
3+
本目录包含了 RunShell 的使用示例。
44

5-
## Pipeline 示例
5+
## 目录结构
66

7-
`pipeline.go` 展示了如何使用 RunShell 的管道功能。这个示例包含了以下几种管道使用场景:
7+
```
8+
examples/
9+
├── pipeline/ # 管道执行示例
10+
│ └── pipeline.go # 演示如何使用管道执行命令
11+
12+
├── session_dev/ # 会话开发示例
13+
│ ├── session_dev.go # 使用会话进行开发的示例代码
14+
│ └── session_dev.md # 示例说明文档
15+
16+
└── README.md # 本文件
17+
```
18+
19+
## 示例说明
820

9-
1. 简单管道:`echo "hello world" | grep "world"`
10-
2. 多重管道:`ls -l | grep go | wc -l`
11-
3. 带环境变量的管道:`env | grep PATH`
12-
4. 复杂管道:`ps aux | grep go | sort -k2 | head -n 3`
13-
5. 文件处理管道:`cat go.mod | grep require | sort | uniq -c`
21+
### 1. Pipeline Example
1422

15-
### 运行示例
23+
`pipeline/pipeline.go` 展示了如何使用 RunShell 的管道功能:
24+
- 简单管道命令
25+
- 多重管道命令
26+
- 带环境变量的管道
27+
- 复杂的管道处理
28+
- 文件处理管道
1629

30+
运行方式:
1731
```bash
18-
# 编译并运行示例
32+
cd pipeline
1933
go run pipeline.go
20-
21-
# 或者先编译后运行
22-
go build pipeline.go
23-
./pipeline
2434
```
2535

26-
### 示例输出说明
36+
### 2. Session Development Example
2737

28-
1. 简单管道示例
29-
- 输出 "hello world" 并通过 grep 过滤出包含 "world" 的行
30-
- 展示了基本的管道功能
38+
`session_dev/session_dev.go` 展示了如何使用 RunShell 的会话功能进行开发:
39+
- Docker 容器环境
40+
- 会话生命周期管理
41+
- 完整开发工作流程
42+
- 自动化构建和测试
3143

32-
2. 多重管道示例
33-
- 列出当前目录文件,过滤出包含 "go" 的行,并计算行数
34-
- 展示了多个命令的串联
44+
运行方式:
45+
```bash
46+
# 终端 1:启动服务器
47+
go run ../cmd/runshell/main.go server --addr :8080
3548

36-
3. 环境变量管道示例
37-
- 显示环境变量并过滤出包含 PATH 的行
38-
- 展示了如何在管道中使用环境变量
49+
# 终端 2:运行示例
50+
cd session_dev
51+
go run session_dev.go
52+
```
3953

40-
4. 复杂管道示例
41-
- 显示进程信息,过滤包含 "go" ��行,排序后显示前三行
42-
- 展示了复杂的管道命令组合
54+
## 注意事项
4355

44-
5. 文件处理管道示例
45-
- 读取 go.mod 文件,过滤出 require 行,排序并统计重复行
46-
- 展示了文件处理相关的管道操作
56+
1. 运行示例前请确保:
57+
- Docker daemon 正在运行
58+
- 相关端口未被占用
59+
- 已安装所需依赖
4760

48-
### 注意事项
61+
2. 每个示例都有自己的 README 或文档,请参考具体说明。
4962

50-
1. 确保系统中安装了相关命令(grep, sort, wc 等)
51-
2. 示例中的某些命令可能需要根据你的系统环境进行调整
52-
3. 管道命令的执行结果可能因系统环境不同而有所不同
63+
3. 示例代码主要用于演示目的,生产环境使用时请注意安全性和错误处理。

examples/pipeline/README.md

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
# Pipeline Example
2+
3+
本示例展示了如何使用 RunShell 的管道功能执行命令链。
4+
5+
## 功能特点
6+
7+
1. 命令链支持
8+
- 支持标准 Unix 管道操作
9+
- 多级命令链执行
10+
- 环境变量传递
11+
- 错误处理和状态报告
12+
13+
2. 执行器特性
14+
- 标准 Unix 命令支持(ls, grep, wc 等)
15+
- 输入输出流管理
16+
- 执行状态跟踪
17+
- 优雅的错误处理
18+
19+
## 示例说明
20+
21+
### 1. 简单管道命令
22+
```bash
23+
ls -la | grep go
24+
```
25+
列出当前目录内容并过滤包含 "go" 的行。这个示例展示了基本的管道功能。
26+
27+
### 2. 多重管道命令
28+
```bash
29+
ls -la | grep go | wc -l
30+
```
31+
统计当前目录中包含 "go" 的文件数量。展示了多级管道的使用。
32+
33+
### 3. 带环境变量的管道命令
34+
```bash
35+
env | grep PATH
36+
```
37+
显示系统路径信息,并支持添加自定义环境变量。展示了环境变量的处理。
38+
39+
### 4. 复杂的管道命令
40+
```bash
41+
ps aux | grep go | sort -k2 | head -n 3
42+
```
43+
查找并排序 Go 相关进程,显示前三个。展示了复杂命令链的处理能力。
44+
45+
### 5. 文件处理管道命令
46+
```bash
47+
cat ../go.mod | grep require | sort | uniq -c
48+
```
49+
分析 go.mod 文件中的依赖项。展示了文件内容处理能力。
50+
51+
## 使用方法
52+
53+
1. 直接运行示例:
54+
```bash
55+
go run pipeline.go
56+
```
57+
58+
2. 在代码中使用:
59+
```go
60+
// 创建执行器
61+
localExec := executor.NewLocalExecutor()
62+
pipeExec := executor.NewPipelineExecutor(localExec)
63+
64+
// 执行管道命令
65+
err := runPipeline(pipeExec, "ls -la | grep go", nil)
66+
if err != nil {
67+
log.Printf("Pipeline failed: %v\n", err)
68+
}
69+
```
70+
71+
3. 带环境变量的执行:
72+
```go
73+
env := map[string]string{
74+
"CUSTOM_PATH": "/custom/path",
75+
}
76+
err := runPipeline(pipeExec, "env | grep PATH", env)
77+
```
78+
79+
## 注意事项
80+
81+
1. 命令执行
82+
- 确保所需命令在系统中可用
83+
- 注意命令的执行权限
84+
- 考虑命令的执行时间
85+
86+
2. 错误处理
87+
- 检查每个命令的返回值
88+
- 处理管道中断的情况
89+
- 注意错误信息的传递
90+
91+
3. 资源管理
92+
- 及时关闭不需要的管道
93+
- 注意内存使用(特别是大文件处理)
94+
- 避免管道死锁
95+
96+
## 扩展建议
97+
98+
1. 功能增强
99+
- 添加超时控制
100+
- 实现并行管道
101+
- 支持命令重试
102+
- 添加管道缓冲控制
103+
104+
2. 使用场景
105+
- 日志处理和分析
106+
- 文件批处理
107+
- 系统监控
108+
- 数据转换
109+
110+
3. 最佳实践
111+
- 使用适当的缓冲区大小
112+
- 实现优雅的错误处理
113+
- 添加详细的日志记录
114+
- 考虑资源限制
115+
116+
## 相关文档
117+
118+
- [RunShell 文档](../../README.md)
119+
- [执行器文档](../../pkg/executor/doc.go)
120+
- [类型定义](../../pkg/types/doc.go)
Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,23 @@
1+
// Package main demonstrates the pipeline functionality of RunShell.
2+
//
3+
// This example shows how to use the pipeline executor to run command chains,
4+
// similar to Unix-style pipes. It demonstrates various pipeline patterns:
5+
// - Simple command piping
6+
// - Multi-stage pipelines
7+
// - Environment variable handling
8+
// - Process filtering and sorting
9+
// - File content processing
10+
//
11+
// The pipeline executor supports:
12+
// - Standard Unix commands (ls, grep, wc, etc.)
13+
// - Environment variable propagation
14+
// - Error handling and status reporting
15+
// - Input/Output stream management
16+
//
17+
// Example usage:
18+
//
19+
// pipeExec := executor.NewPipelineExecutor(localExec)
20+
// err := runPipeline(pipeExec, "ls -la | grep go | wc -l", nil)
121
package main
222

323
import (
@@ -10,6 +30,23 @@ import (
1030
"github.com/iamlongalong/runshell/pkg/types"
1131
)
1232

33+
// runPipeline executes a pipeline of commands and handles the results.
34+
//
35+
// Parameters:
36+
// - pipeExec: The pipeline executor instance
37+
// - cmdStr: The command string containing one or more piped commands
38+
// - env: Optional environment variables for the pipeline
39+
//
40+
// The function:
41+
// 1. Parses the command string into a pipeline
42+
// 2. Sets up the execution context and options
43+
// 3. Executes the pipeline and captures the result
44+
// 4. Reports any errors that occur
45+
//
46+
// Example:
47+
//
48+
// runPipeline(pipeExec, "ls -la | grep go", nil)
49+
// runPipeline(pipeExec, "cat file.txt | grep pattern | wc -l", env)
1350
func runPipeline(pipeExec *executor.PipelineExecutor, cmdStr string, env map[string]string) error {
1451
fmt.Printf("\nExecuting: %s\n", cmdStr)
1552

@@ -42,20 +79,23 @@ func main() {
4279
pipeExec := executor.NewPipelineExecutor(localExec)
4380

4481
// 示例 1: 简单的管道命令
82+
// 列出当前目录内容并过滤包含 "go" 的行
4583
fmt.Println("\n=== Example 1: Simple Pipeline ===")
46-
err := runPipeline(pipeExec, "echo hello world | grep world", nil)
84+
err := runPipeline(pipeExec, "ls -la | grep go", nil)
4785
if err != nil {
4886
log.Printf("Example 1 failed: %v\n", err)
4987
}
5088

5189
// 示例 2: 多重管道命令
90+
// 统计当前目录中包含 "go" 的文件数量
5291
fmt.Println("\n=== Example 2: Multiple Pipes ===")
5392
err = runPipeline(pipeExec, "ls -la | grep go | wc -l", nil)
5493
if err != nil {
5594
log.Printf("Example 2 failed: %v\n", err)
5695
}
5796

5897
// 示例 3: 带环境变量的管道命令
98+
// 显示系统路径信息,并添加自定义路径
5999
fmt.Println("\n=== Example 3: Pipeline with Environment Variables ===")
60100
err = runPipeline(pipeExec, "env | grep PATH", map[string]string{
61101
"CUSTOM_PATH": "/custom/path",
@@ -65,13 +105,15 @@ func main() {
65105
}
66106

67107
// 示例 4: 复杂的管道命令
108+
// 查找并排序 Go 相关进程,显示前三个
68109
fmt.Println("\n=== Example 4: Complex Pipeline ===")
69110
err = runPipeline(pipeExec, "ps aux | grep go | sort -k2 | head -n 3", nil)
70111
if err != nil {
71112
log.Printf("Example 4 failed: %v\n", err)
72113
}
73114

74115
// 示例 5: 文件处理管道命令
116+
// 分析 go.mod 文件中的依赖项
75117
fmt.Println("\n=== Example 5: File Processing Pipeline ===")
76118
err = runPipeline(pipeExec, "cat ../go.mod | grep require | sort | uniq -c", nil)
77119
if err != nil {

0 commit comments

Comments
 (0)