From 9d30b268e3465118f16a3c0d7076e4a7a2dea405 Mon Sep 17 00:00:00 2001 From: Donnie Adams Date: Mon, 28 Oct 2024 07:12:24 -0400 Subject: [PATCH] feat: add file stat to workspace API Signed-off-by: Donnie Adams --- pkg/sdkserver/routes.go | 1 + pkg/sdkserver/workspaces.go | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/pkg/sdkserver/routes.go b/pkg/sdkserver/routes.go index 713f74fe..e9b1cca8 100644 --- a/pkg/sdkserver/routes.go +++ b/pkg/sdkserver/routes.go @@ -82,6 +82,7 @@ func (s *server) addRoutes(mux *http.ServeMux) { mux.HandleFunc("POST /workspaces/write-file", s.writeFileInWorkspace) mux.HandleFunc("POST /workspaces/delete-file", s.removeFileInWorkspace) mux.HandleFunc("POST /workspaces/read-file", s.readFileInWorkspace) + mux.HandleFunc("POST /workspaces/stat-file", s.statFileInWorkspace) } // health just provides an endpoint for checking whether the server is running and accessible. diff --git a/pkg/sdkserver/workspaces.go b/pkg/sdkserver/workspaces.go index 4356a583..ed6602ea 100644 --- a/pkg/sdkserver/workspaces.go +++ b/pkg/sdkserver/workspaces.go @@ -281,3 +281,39 @@ func (s *server) readFileInWorkspace(w http.ResponseWriter, r *http.Request) { writeResponse(logger, w, map[string]any{"stdout": out}) } + +type statFileInWorkspaceRequest struct { + workspaceCommonRequest `json:",inline"` + FilePath string `json:"filePath"` +} + +func (s *server) statFileInWorkspace(w http.ResponseWriter, r *http.Request) { + logger := gcontext.GetLogger(r.Context()) + var reqObject statFileInWorkspaceRequest + if err := json.NewDecoder(r.Body).Decode(&reqObject); err != nil { + writeError(logger, w, http.StatusBadRequest, fmt.Errorf("invalid request body: %w", err)) + return + } + + prg, err := loader.Program(r.Context(), s.getWorkspaceTool(reqObject.workspaceCommonRequest), "Stat File In Workspace", loader.Options{Cache: s.client.Cache}) + if err != nil { + writeError(logger, w, http.StatusInternalServerError, fmt.Errorf("failed to load program: %w", err)) + return + } + + out, err := s.client.Run( + r.Context(), + prg, + reqObject.Env, + fmt.Sprintf( + `{"workspace_id": "%s", "file_path": "%s"}`, + reqObject.ID, reqObject.FilePath, + ), + ) + if err != nil { + writeError(logger, w, http.StatusInternalServerError, fmt.Errorf("failed to run program: %w", err)) + return + } + + writeResponse(logger, w, map[string]any{"stdout": out}) +}