chore(repo): reinitialize repository

This commit is contained in:
2026-03-18 11:29:54 +08:00
commit 24871e213a
288 changed files with 44369 additions and 0 deletions
+91
View File
@@ -0,0 +1,91 @@
package systemfs
import (
"fmt"
"os"
"path/filepath"
"sort"
"strings"
)
type DirectoryEntry struct {
Name string `json:"name"`
Path string `json:"path"`
IsGit bool `json:"is_git"`
}
type DirectoryListing struct {
Current string `json:"current"`
CurrentIsGit bool `json:"current_is_git"`
Parent string `json:"parent"`
Directories []DirectoryEntry `json:"dirs"`
}
type Service struct{}
func NewService() *Service {
return &Service{}
}
func (s *Service) ListDirectories(current string) (DirectoryListing, error) {
current = strings.TrimSpace(current)
if current == "" {
wd, err := os.Getwd()
if err != nil {
return DirectoryListing{}, err
}
current = wd
}
current, err := filepath.Abs(current)
if err != nil {
return DirectoryListing{}, err
}
entries, err := os.ReadDir(current)
if err != nil {
return DirectoryListing{}, err
}
dirs := make([]DirectoryEntry, 0, len(entries))
for _, entry := range entries {
if !entry.IsDir() {
continue
}
fullPath := filepath.Join(current, entry.Name())
dirs = append(dirs, DirectoryEntry{
Name: entry.Name(),
Path: fullPath,
IsGit: pathHasGitDir(fullPath),
})
}
sort.Slice(dirs, func(i, j int) bool {
return dirs[i].Name < dirs[j].Name
})
return DirectoryListing{
Current: current,
CurrentIsGit: pathHasGitDir(current),
Parent: filepath.Dir(current),
Directories: dirs,
}, nil
}
func (s *Service) CreateDirectory(parent, name string) (string, error) {
parent = strings.TrimSpace(parent)
name = strings.TrimSpace(name)
if parent == "" || name == "" {
return "", fmt.Errorf("parent and name are required")
}
path := filepath.Join(parent, name)
if err := os.MkdirAll(path, 0o755); err != nil {
return "", err
}
return path, nil
}
func pathHasGitDir(path string) bool {
_, err := os.Stat(filepath.Join(path, ".git"))
return err == nil
}
@@ -0,0 +1,65 @@
package systemfs
import (
"os"
"path/filepath"
"testing"
)
func TestListDirectoriesReturnsSortedDirectoriesAndGitFlag(t *testing.T) {
root := t.TempDir()
if err := os.MkdirAll(filepath.Join(root, "b-dir"), 0o755); err != nil {
t.Fatalf("mkdir b-dir: %v", err)
}
if err := os.MkdirAll(filepath.Join(root, "a-dir", ".git"), 0o755); err != nil {
t.Fatalf("mkdir a-dir/.git: %v", err)
}
if err := os.WriteFile(filepath.Join(root, "notes.txt"), []byte("ignored"), 0o644); err != nil {
t.Fatalf("write notes.txt: %v", err)
}
service := NewService()
listing, err := service.ListDirectories(root)
if err != nil {
t.Fatalf("ListDirectories() error = %v", err)
}
if listing.Current != root {
t.Fatalf("current = %q, want %q", listing.Current, root)
}
if len(listing.Directories) != 2 {
t.Fatalf("expected 2 directories, got %#v", listing.Directories)
}
if listing.Directories[0].Name != "a-dir" || !listing.Directories[0].IsGit {
t.Fatalf("unexpected first directory: %#v", listing.Directories[0])
}
if listing.Directories[1].Name != "b-dir" || listing.Directories[1].IsGit {
t.Fatalf("unexpected second directory: %#v", listing.Directories[1])
}
}
func TestCreateDirectoryRequiresParentAndName(t *testing.T) {
service := NewService()
if _, err := service.CreateDirectory("", "child"); err == nil {
t.Fatal("expected validation error when parent is empty")
}
if _, err := service.CreateDirectory("/tmp", ""); err == nil {
t.Fatal("expected validation error when name is empty")
}
}
func TestCreateDirectoryCreatesPath(t *testing.T) {
root := t.TempDir()
service := NewService()
path, err := service.CreateDirectory(root, "nested/child")
if err != nil {
t.Fatalf("CreateDirectory() error = %v", err)
}
if path != filepath.Join(root, "nested/child") {
t.Fatalf("path = %q", path)
}
if info, err := os.Stat(path); err != nil || !info.IsDir() {
t.Fatalf("expected created directory, stat err=%v info=%#v", err, info)
}
}