feat: harden workspace file APIs

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
tao.chen
2026-07-02 17:22:26 +08:00
co-authored by Claude Fable 5
parent c1b8982e3b
commit 04b309d3fc
18 changed files with 1262 additions and 76 deletions
+28
View File
@@ -3,6 +3,7 @@ package workspace
import (
"os"
"path/filepath"
"sort"
"codespace/internal/util"
)
@@ -12,6 +13,7 @@ type Manager interface {
Create(id string) (*Workspace, error)
Get(id string) (*Workspace, error)
Delete(id string) error
List() ([]Workspace, error)
}
// LocalManager implements Manager using the local filesystem.
@@ -71,3 +73,29 @@ func (m *LocalManager) Delete(id string) error {
}
return nil
}
// List returns all workspaces under the manager root, sorted by ID.
// Only directories whose names pass ValidID are included.
func (m *LocalManager) List() ([]Workspace, error) {
if err := os.MkdirAll(m.root, 0o755); err != nil {
return nil, util.Wrap(util.CodeInternal, "failed to ensure workspace root", err)
}
entries, err := os.ReadDir(m.root)
if err != nil {
return nil, util.Wrap(util.CodeInternal, "failed to read workspace root", err)
}
var result []Workspace
for _, e := range entries {
if !e.IsDir() {
continue
}
if !ValidID(e.Name()) {
continue
}
result = append(result, Workspace{ID: e.Name(), Root: RootFor(m.root, e.Name())})
}
sort.Slice(result, func(i, j int) bool {
return result[i].ID < result[j].ID
})
return result, nil
}