-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
164 lines (143 loc) · 3.71 KB
/
main.go
File metadata and controls
164 lines (143 loc) · 3.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package main
import (
"encoding/json"
"fmt"
"strconv"
"sync"
squadron "github.com/mlund01/squadron-sdk"
)
var tools = map[string]*squadron.ToolInfo{
"exec": {
Name: "exec",
Description: "Execute a shell command. The execution target (local, SSH, or Docker) is configured at the plugin level.",
Schema: squadron.Schema{
Type: squadron.TypeObject,
Properties: squadron.PropertyMap{
"command": {
Type: squadron.TypeString,
Description: "The shell command to execute",
},
"timeout": {
Type: squadron.TypeInteger,
Description: "Timeout in seconds. 0 means no timeout. Default: 120",
},
},
Required: []string{"command"},
},
},
}
type ShellPlugin struct {
mu sync.Mutex
executor Executor
mode string
}
// Configure sets up the executor based on plugin settings.
//
// Settings:
//
// mode - "local" (default), "ssh", or "docker"
// shell - Shell to use (default: "bash" on unix, "cmd" on windows, "sh" in docker)
//
// SSH mode settings:
//
// host - SSH host (required)
// user - SSH user (required)
// port - SSH port (default: 22)
// key_file - Path to SSH private key
// password - SSH password (prefer key_file)
//
// Docker mode settings:
//
// container - Name or ID of running container (use this OR image)
// image - Docker image to create a container from (use this OR container)
func (p *ShellPlugin) Configure(settings map[string]string) error {
p.mu.Lock()
defer p.mu.Unlock()
mode := settings["mode"]
if mode == "" {
mode = "local"
}
p.mode = mode
shell := settings["shell"]
switch mode {
case "local":
p.executor = NewLocalExecutor(shell)
return nil
case "ssh":
host := settings["host"]
user := settings["user"]
if host == "" || user == "" {
return fmt.Errorf("ssh mode requires 'host' and 'user' settings")
}
port := 22
if portStr := settings["port"]; portStr != "" {
var err error
port, err = strconv.Atoi(portStr)
if err != nil {
return fmt.Errorf("invalid port: %s", portStr)
}
}
keyFile := settings["key_file"]
password := settings["password"]
executor, err := NewSSHExecutor(host, user, keyFile, password, port)
if err != nil {
return err
}
p.executor = executor
return nil
case "docker":
container := settings["container"]
image := settings["image"]
executor, err := NewDockerExecutor(container, image, shell)
if err != nil {
return err
}
p.executor = executor
return nil
default:
return fmt.Errorf("unknown mode %q: use 'local', 'ssh', or 'docker'", mode)
}
}
type execParams struct {
Command string `json:"command"`
Timeout int `json:"timeout"`
}
func (p *ShellPlugin) Call(toolName string, payload string) (string, error) {
if toolName != "exec" {
return "", fmt.Errorf("unknown tool: %s", toolName)
}
var params execParams
if err := json.Unmarshal([]byte(payload), ¶ms); err != nil {
return "", fmt.Errorf("invalid parameters: %w", err)
}
if params.Command == "" {
return "", fmt.Errorf("command is required")
}
if params.Timeout == 0 {
params.Timeout = 120
}
p.mu.Lock()
executor := p.executor
p.mu.Unlock()
if executor == nil {
return "", fmt.Errorf("plugin not configured — call Configure first")
}
return executor.Exec(params.Command, params.Timeout)
}
func (p *ShellPlugin) GetToolInfo(toolName string) (*squadron.ToolInfo, error) {
info, ok := tools[toolName]
if !ok {
return nil, fmt.Errorf("unknown tool: %s", toolName)
}
return info, nil
}
func (p *ShellPlugin) ListTools() ([]*squadron.ToolInfo, error) {
result := make([]*squadron.ToolInfo, 0, len(tools))
for _, info := range tools {
result = append(result, info)
}
return result, nil
}
func main() {
squadron.Serve(&ShellPlugin{})
}