Skip to content

Commit d35e9b0

Browse files
committed
feat: add file stat to workspace API
Signed-off-by: Donnie Adams <[email protected]>
1 parent dd84ea2 commit d35e9b0

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

pkg/sdkserver/routes.go

+1
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ func (s *server) addRoutes(mux *http.ServeMux) {
8282
mux.HandleFunc("POST /workspaces/write-file", s.writeFileInWorkspace)
8383
mux.HandleFunc("POST /workspaces/delete-file", s.removeFileInWorkspace)
8484
mux.HandleFunc("POST /workspaces/read-file", s.readFileInWorkspace)
85+
mux.HandleFunc("POST /workspaces/stat-file", s.statFileInWorkspace)
8586
}
8687

8788
// health just provides an endpoint for checking whether the server is running and accessible.

pkg/sdkserver/workspaces.go

+36
Original file line numberDiff line numberDiff line change
@@ -281,3 +281,39 @@ func (s *server) readFileInWorkspace(w http.ResponseWriter, r *http.Request) {
281281

282282
writeResponse(logger, w, map[string]any{"stdout": out})
283283
}
284+
285+
type statFileInWorkspaceRequest struct {
286+
workspaceCommonRequest `json:",inline"`
287+
FilePath string `json:"filePath"`
288+
}
289+
290+
func (s *server) statFileInWorkspace(w http.ResponseWriter, r *http.Request) {
291+
logger := gcontext.GetLogger(r.Context())
292+
var reqObject statFileInWorkspaceRequest
293+
if err := json.NewDecoder(r.Body).Decode(&reqObject); err != nil {
294+
writeError(logger, w, http.StatusBadRequest, fmt.Errorf("invalid request body: %w", err))
295+
return
296+
}
297+
298+
prg, err := loader.Program(r.Context(), s.getWorkspaceTool(reqObject.workspaceCommonRequest), "Stat File In Workspace", loader.Options{Cache: s.client.Cache})
299+
if err != nil {
300+
writeError(logger, w, http.StatusInternalServerError, fmt.Errorf("failed to load program: %w", err))
301+
return
302+
}
303+
304+
out, err := s.client.Run(
305+
r.Context(),
306+
prg,
307+
reqObject.Env,
308+
fmt.Sprintf(
309+
`{"workspace_id": "%s", "file_path": "%s"}`,
310+
reqObject.ID, reqObject.FilePath,
311+
),
312+
)
313+
if err != nil {
314+
writeError(logger, w, http.StatusInternalServerError, fmt.Errorf("failed to run program: %w", err))
315+
return
316+
}
317+
318+
writeResponse(logger, w, map[string]any{"stdout": out})
319+
}

0 commit comments

Comments
 (0)