Skip to content

Commit

Permalink
feat: add file stat to workspace API
Browse files Browse the repository at this point in the history
Signed-off-by: Donnie Adams <[email protected]>
  • Loading branch information
thedadams committed Oct 28, 2024
1 parent 2d1632c commit 9d30b26
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
1 change: 1 addition & 0 deletions pkg/sdkserver/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
36 changes: 36 additions & 0 deletions pkg/sdkserver/workspaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -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})
}

0 comments on commit 9d30b26

Please sign in to comment.