feat: scaffold codespace backend
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,79 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"codespace/internal/fs"
|
||||
"codespace/internal/workspace"
|
||||
)
|
||||
|
||||
// FileService handles file operations within workspaces.
|
||||
type FileService struct {
|
||||
workspaces workspace.Manager
|
||||
}
|
||||
|
||||
// NewFileService creates a FileService.
|
||||
func NewFileService(workspaces workspace.Manager) *FileService {
|
||||
return &FileService{workspaces: workspaces}
|
||||
}
|
||||
|
||||
// List lists files at the given workspace-relative path.
|
||||
func (s *FileService) List(workspaceID, path string) ([]fs.FileInfo, error) {
|
||||
ws, err := s.workspaces.Get(workspaceID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return fs.NewLocal(ws.Root).List(path)
|
||||
}
|
||||
|
||||
// Read reads a file at the given workspace-relative path.
|
||||
func (s *FileService) Read(workspaceID, path string) ([]byte, error) {
|
||||
ws, err := s.workspaces.Get(workspaceID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return fs.NewLocal(ws.Root).Read(path)
|
||||
}
|
||||
|
||||
// Write writes data to a file at the given workspace-relative path.
|
||||
func (s *FileService) Write(workspaceID, path string, data []byte) error {
|
||||
ws, err := s.workspaces.Get(workspaceID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return fs.NewLocal(ws.Root).Write(path, data)
|
||||
}
|
||||
|
||||
// Mkdir creates a directory at the given workspace-relative path.
|
||||
func (s *FileService) Mkdir(workspaceID, path string) error {
|
||||
ws, err := s.workspaces.Get(workspaceID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return fs.NewLocal(ws.Root).Mkdir(path)
|
||||
}
|
||||
|
||||
// Remove removes a file or directory at the given workspace-relative path.
|
||||
func (s *FileService) Remove(workspaceID, path string) error {
|
||||
ws, err := s.workspaces.Get(workspaceID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return fs.NewLocal(ws.Root).Remove(path)
|
||||
}
|
||||
|
||||
// Rename renames a file or directory from oldPath to newPath.
|
||||
func (s *FileService) Rename(workspaceID, oldPath, newPath string) error {
|
||||
ws, err := s.workspaces.Get(workspaceID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return fs.NewLocal(ws.Root).Rename(oldPath, newPath)
|
||||
}
|
||||
|
||||
// Stat returns file info for the given workspace-relative path.
|
||||
func (s *FileService) Stat(workspaceID, path string) (*fs.FileInfo, error) {
|
||||
ws, err := s.workspaces.Get(workspaceID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return fs.NewLocal(ws.Root).Stat(path)
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"codespace/internal/process"
|
||||
"codespace/internal/workspace"
|
||||
)
|
||||
|
||||
// ProcessService manages OpenCode processes for workspaces.
|
||||
type ProcessService struct {
|
||||
workspaces workspace.Manager
|
||||
processes process.Manager
|
||||
}
|
||||
|
||||
// NewProcessService creates a ProcessService.
|
||||
func NewProcessService(workspaces workspace.Manager, processes process.Manager) *ProcessService {
|
||||
return &ProcessService{workspaces: workspaces, processes: processes}
|
||||
}
|
||||
|
||||
// Start starts an OpenCode process for the given workspace.
|
||||
func (s *ProcessService) Start(workspaceID string) error {
|
||||
ws, err := s.workspaces.Get(workspaceID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return s.processes.Start(workspaceID, ws.Root)
|
||||
}
|
||||
|
||||
// Stop stops the OpenCode process for the given workspace.
|
||||
func (s *ProcessService) Stop(workspaceID string) error {
|
||||
return s.processes.Stop(workspaceID)
|
||||
}
|
||||
|
||||
// Restart restarts the OpenCode process for the given workspace.
|
||||
func (s *ProcessService) Restart(workspaceID string) error {
|
||||
ws, err := s.workspaces.Get(workspaceID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return s.processes.Restart(workspaceID, ws.Root)
|
||||
}
|
||||
|
||||
// Status returns the process status for the given workspace.
|
||||
func (s *ProcessService) Status(workspaceID string) (process.Status, error) {
|
||||
if _, err := s.workspaces.Get(workspaceID); err != nil {
|
||||
return process.Status{}, err
|
||||
}
|
||||
return s.processes.Status(workspaceID), nil
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"codespace/internal/process"
|
||||
"codespace/internal/workspace"
|
||||
)
|
||||
|
||||
// WorkspaceService orchestrates workspace lifecycle operations.
|
||||
type WorkspaceService struct {
|
||||
workspaces workspace.Manager
|
||||
processes process.Manager
|
||||
}
|
||||
|
||||
// NewWorkspaceService creates a WorkspaceService.
|
||||
func NewWorkspaceService(workspaces workspace.Manager, processes process.Manager) *WorkspaceService {
|
||||
return &WorkspaceService{workspaces: workspaces, processes: processes}
|
||||
}
|
||||
|
||||
// Create creates a new workspace.
|
||||
func (s *WorkspaceService) Create(id string) (*workspace.Workspace, error) {
|
||||
return s.workspaces.Create(id)
|
||||
}
|
||||
|
||||
// Get retrieves a workspace by id.
|
||||
func (s *WorkspaceService) Get(id string) (*workspace.Workspace, error) {
|
||||
return s.workspaces.Get(id)
|
||||
}
|
||||
|
||||
// Delete stops any running process for the workspace, then removes it.
|
||||
func (s *WorkspaceService) Delete(id string) error {
|
||||
status := s.processes.Status(id)
|
||||
if status.Running {
|
||||
_ = s.processes.Stop(id)
|
||||
}
|
||||
return s.workspaces.Delete(id)
|
||||
}
|
||||
Reference in New Issue
Block a user