-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexecutor.go
More file actions
181 lines (145 loc) · 4.2 KB
/
executor.go
File metadata and controls
181 lines (145 loc) · 4.2 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package main
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
)
// ExecutorService handles execution of Base CLI commands
type ExecutorService struct {
basePath string
cmdPath string
}
// NewExecutorService creates a new executor service
func NewExecutorService() *ExecutorService {
// Try to find the base command in various locations
basePath := findBasePath()
cmdPath := findCmdPath()
return &ExecutorService{
basePath: basePath,
cmdPath: cmdPath,
}
}
// findBasePath attempts to locate the base executable
func findBasePath() string {
// Check if base is in PATH
if path, err := exec.LookPath("base"); err == nil {
return path
}
// Check common installation locations
candidates := []string{
"/usr/local/bin/base",
"/usr/bin/base",
"./cmd/base",
"../cmd/base",
"./base",
}
for _, candidate := range candidates {
if _, err := os.Stat(candidate); err == nil {
return candidate
}
}
// Default to assuming base is in PATH
return "base"
}
// findCmdPath attempts to locate the cmd directory for the Base CLI
func findCmdPath() string {
candidates := []string{
"../cmd",
"./cmd",
"../../cmd",
}
for _, candidate := range candidates {
if stat, err := os.Stat(candidate); err == nil && stat.IsDir() {
return candidate
}
}
return ""
}
// ExecuteGenerate executes the base generate command
func (e *ExecutorService) ExecuteGenerate(name string, fields []string) (string, error) {
args := []string{"generate", name}
args = append(args, fields...)
return e.executeBaseCommand(args...)
}
// ExecuteStart executes the base start command
func (e *ExecutorService) ExecuteStart(reload, docs bool) (string, error) {
args := []string{"start"}
if reload {
args = append(args, "-r")
}
if docs {
args = append(args, "-d")
}
return e.executeBaseCommand(args...)
}
// ExecuteNew executes the base new command
func (e *ExecutorService) ExecuteNew(name, path string) (string, error) {
args := []string{"new", name}
if path != "" {
args = append(args, "--path", path)
}
return e.executeBaseCommand(args...)
}
// ExecuteDestroy executes the base destroy command
func (e *ExecutorService) ExecuteDestroy(name string) (string, error) {
return e.executeBaseCommand("destroy", name)
}
// ExecuteDocs executes the base docs command
func (e *ExecutorService) ExecuteDocs() (string, error) {
return e.executeBaseCommand("docs")
}
// executeBaseCommand executes a base command with the given arguments
func (e *ExecutorService) executeBaseCommand(args ...string) (string, error) {
// Try using the base CLI if available
if e.basePath != "" {
cmd := exec.Command(e.basePath, args...)
output, err := cmd.CombinedOutput()
if err != nil {
return "", fmt.Errorf("base command failed: %v\nOutput: %s", err, string(output))
}
return string(output), nil
}
// Fallback to direct Go execution if cmd path is available
if e.cmdPath != "" {
return e.executeGoDirect(args...)
}
return "", fmt.Errorf("base CLI not found - please install Base CLI or run from Base project directory")
}
// executeGoDirect executes Base CLI commands directly using go run
func (e *ExecutorService) executeGoDirect(args ...string) (string, error) {
mainGo := filepath.Join(e.cmdPath, "main.go")
// Check if main.go exists
if _, err := os.Stat(mainGo); os.IsNotExist(err) {
return "", fmt.Errorf("base CLI main.go not found at %s", mainGo)
}
// Prepare go run command
goArgs := []string{"run", mainGo}
goArgs = append(goArgs, args...)
cmd := exec.Command("go", goArgs...)
cmd.Dir = e.cmdPath
output, err := cmd.CombinedOutput()
if err != nil {
return "", fmt.Errorf("go run failed: %v\nOutput: %s", err, string(output))
}
return string(output), nil
}
// IsBaseAvailable checks if Base CLI is available
func (e *ExecutorService) IsBaseAvailable() bool {
return e.basePath != "" || e.cmdPath != ""
}
// GetStatus returns the status of the executor service
func (e *ExecutorService) GetStatus() string {
var status []string
if e.basePath != "" {
status = append(status, fmt.Sprintf("Base CLI: %s", e.basePath))
}
if e.cmdPath != "" {
status = append(status, fmt.Sprintf("Cmd Path: %s", e.cmdPath))
}
if len(status) == 0 {
return "Base CLI not found"
}
return strings.Join(status, "\n")
}