-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhurl.go
More file actions
259 lines (213 loc) · 7.11 KB
/
Copy pathhurl.go
File metadata and controls
259 lines (213 loc) · 7.11 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
package main
import (
"embed"
"fmt"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
)
//go:embed binaries/*
var binaries embed.FS
// GetHurlPath returns the path to the hurl binary
// It extracts the embedded binary to a temp location if needed
func GetHurlPath() (string, error) {
// Determine the binary name based on OS
binaryName := "hurl"
if runtime.GOOS == "windows" {
binaryName = "hurl.exe"
}
// Check if hurl is already in PATH
if path, err := exec.LookPath("hurl"); err == nil {
return path, nil
}
// Determine the embedded binary path based on OS and architecture
embeddedPath := fmt.Sprintf("binaries/%s-%s/%s", runtime.GOOS, runtime.GOARCH, binaryName)
// Read the embedded binary
data, err := binaries.ReadFile(embeddedPath)
if err != nil {
return "", fmt.Errorf("hurl binary not found for %s-%s: %w", runtime.GOOS, runtime.GOARCH, err)
}
// Create a temp directory for the binary
tempDir := filepath.Join(os.TempDir(), "hurlstudio")
if err := os.MkdirAll(tempDir, 0755); err != nil {
return "", fmt.Errorf("failed to create temp directory: %w", err)
}
// Write the binary to temp location
binaryPath := filepath.Join(tempDir, binaryName)
// Only write if file doesn't exist or is different
if _, err := os.Stat(binaryPath); os.IsNotExist(err) {
if err := os.WriteFile(binaryPath, data, 0755); err != nil {
return "", fmt.Errorf("failed to write binary: %w", err)
}
}
return binaryPath, nil
}
// RunHurl executes a hurl file and returns the JSON report
// setupReportDir creates and cleans the report directory for a file
func setupReportDir(filePath string) (string, error) {
reportDir := filepath.Join(os.TempDir(), "hurlstudio", filePath)
// Remove and recreate this specific file's report directory to ensure clean state
os.RemoveAll(reportDir)
if err := os.MkdirAll(reportDir, 0755); err != nil {
return "", fmt.Errorf("failed to create report directory: %w", err)
}
return reportDir, nil
}
// readReportFromDir reads the most recent JSON report from the directory
func readReportFromDir(reportDir string, fallbackOutput []byte) (string, error) {
reportFiles, err := filepath.Glob(filepath.Join(reportDir, "*.json"))
if err != nil || len(reportFiles) == 0 {
// If no JSON report found, return the stdout output
return string(fallbackOutput), nil
}
// Read the most recent report file (last one in sorted list)
reportFile := reportFiles[len(reportFiles)-1]
jsonContent, err := os.ReadFile(reportFile)
if err != nil {
return string(fallbackOutput), nil
}
return string(jsonContent), nil
}
// createVariablesFile creates a temporary variables file from environment variables
func (a *App) createVariablesFile() (string, error) {
// Get active environment
activeEnv, err := a.GetActiveEnvironment()
if err != nil {
// If error, just return empty string (no variables file)
return "", nil
}
// Get flattened variables for active environment
vars, err := a.GetFlattenedVariables(activeEnv)
if err != nil {
return "", nil
}
// If no variables, don't create a file
if len(vars) == 0 {
return "", nil
}
// Create temp variables file
tempFile, err := os.CreateTemp("", "hurl-vars-*.txt")
if err != nil {
return "", fmt.Errorf("failed to create temp variables file: %w", err)
}
defer tempFile.Close()
// Write variables in key=value format
var lines []string
for key, value := range vars {
lines = append(lines, fmt.Sprintf("%s=%s", key, value))
}
content := strings.Join(lines, "\n")
if _, err := tempFile.WriteString(content); err != nil {
os.Remove(tempFile.Name())
return "", fmt.Errorf("failed to write variables file: %w", err)
}
return tempFile.Name(), nil
}
// Generates a JSON report in /tmp/hurlstudio/<full-file-path>/
func (a *App) RunHurl(filePath string) (string, error) {
hurlPath, err := GetHurlPath()
if err != nil {
return "", err
}
reportDir, err := setupReportDir(filePath)
if err != nil {
return "", err
}
// Create variables file if needed
varsFile, err := a.createVariablesFile()
if err != nil {
return "", err
}
if varsFile != "" {
defer os.Remove(varsFile)
}
// Build command with variables if present
args := []string{"--report-json", reportDir}
if varsFile != "" {
args = append(args, "--variables-file", varsFile)
}
args = append(args, filePath)
// Run hurl with JSON report generation
cmd := exec.Command(hurlPath, args...)
output, _ := cmd.CombinedOutput()
return readReportFromDir(reportDir, output)
}
// RunHurlWithOptions executes a hurl file with custom options
func (a *App) RunHurlWithOptions(filePath string, options []string) (string, error) {
hurlPath, err := GetHurlPath()
if err != nil {
return "", err
}
args := append(options, filePath)
cmd := exec.Command(hurlPath, args...)
output, err := cmd.CombinedOutput()
return string(output), err
}
// RunHurlEntry runs a specific entry from a Hurl file
// entryIndex is 1-based (first entry is 1)
// Uses the same report directory as RunHurl
func (a *App) RunHurlEntry(filePath string, entryIndex int) (string, error) {
hurlPath, err := GetHurlPath()
if err != nil {
return "", err
}
reportDir, err := setupReportDir(filePath)
if err != nil {
return "", err
}
// Create variables file if needed
varsFile, err := a.createVariablesFile()
if err != nil {
return "", err
}
if varsFile != "" {
defer os.Remove(varsFile)
}
// Build command with variables if present
args := []string{"--report-json", reportDir}
if varsFile != "" {
args = append(args, "--variables-file", varsFile)
}
args = append(args, "--from-entry", fmt.Sprintf("%d", entryIndex))
args = append(args, "--to-entry", fmt.Sprintf("%d", entryIndex))
args = append(args, filePath)
// Run only the specific entry
cmd := exec.Command(hurlPath, args...)
output, _ := cmd.CombinedOutput()
return readReportFromDir(reportDir, output)
}
// GetExistingReport checks if a report already exists for the given file path
// Returns the report JSON content if found, empty string if not found
func (a *App) GetExistingReport(filePath string) (string, error) {
reportDir := filepath.Join(os.TempDir(), "hurlstudio", filePath)
// Check if report directory exists
if _, err := os.Stat(reportDir); os.IsNotExist(err) {
return "", nil // No report exists yet
}
// Look for JSON report files
reportFiles, err := filepath.Glob(filepath.Join(reportDir, "*.json"))
if err != nil || len(reportFiles) == 0 {
return "", nil // No report found
}
// Read the most recent report file
reportFile := reportFiles[len(reportFiles)-1]
jsonContent, err := os.ReadFile(reportFile)
if err != nil {
return "", nil // Could not read report
}
return string(jsonContent), nil
}
// GetResponseBody reads the response body from the report directory
// bodyPath is the relative path from the report (e.g., "store/response_1.txt")
func (a *App) GetResponseBody(hurlFilePath string, bodyPath string) (string, error) {
reportDir := filepath.Join(os.TempDir(), "hurlstudio", hurlFilePath)
bodyFilePath := filepath.Join(reportDir, bodyPath)
// Read the body file
content, err := os.ReadFile(bodyFilePath)
if err != nil {
return "", fmt.Errorf("failed to read body file: %w", err)
}
return string(content), nil
}