Refactor filesystem to make it testable

This commit is contained in:
2024-11-13 22:31:04 +01:00
parent 52ffb17e2d
commit 93963b1867
7 changed files with 210 additions and 77 deletions

View File

@@ -2,19 +2,28 @@ package filesystem
import (
"fmt"
"os"
"path/filepath"
)
// GetWorkspacePath returns the path to the workspace directory for the given user and workspace IDs.
func (fs *FileSystem) GetWorkspacePath(userID, workspaceID int) string {
return filepath.Join(fs.RootDir, fmt.Sprintf("%d", userID), fmt.Sprintf("%d", workspaceID))
// Parameters:
// - userID: the ID of the user who owns the workspace
// - workspaceID: the ID of the workspace
// Returns:
// - result: the path to the workspace directory
func (s *Storage) GetWorkspacePath(userID, workspaceID int) string {
return filepath.Join(s.RootDir, fmt.Sprintf("%d", userID), fmt.Sprintf("%d", workspaceID))
}
// InitializeUserWorkspace creates the workspace directory for the given user and workspace IDs.
func (fs *FileSystem) InitializeUserWorkspace(userID, workspaceID int) error {
workspacePath := fs.GetWorkspacePath(userID, workspaceID)
err := os.MkdirAll(workspacePath, 0755)
// Parameters:
// - userID: the ID of the user who owns the workspace
// - workspaceID: the ID of the workspace to initialize
// Returns:
// - error: any error that occurred during the operation
func (s *Storage) InitializeUserWorkspace(userID, workspaceID int) error {
workspacePath := s.GetWorkspacePath(userID, workspaceID)
err := s.fs.MkdirAll(workspacePath, 0755)
if err != nil {
return fmt.Errorf("failed to create workspace directory: %w", err)
}
@@ -23,18 +32,17 @@ func (fs *FileSystem) InitializeUserWorkspace(userID, workspaceID int) error {
}
// DeleteUserWorkspace deletes the workspace directory for the given user and workspace IDs.
func (fs *FileSystem) DeleteUserWorkspace(userID, workspaceID int) error {
workspacePath := fs.GetWorkspacePath(userID, workspaceID)
err := os.RemoveAll(workspacePath)
// Parameters:
// - userID: the ID of the user who owns the workspace
// - workspaceID: the ID of the workspace to delete
// Returns:
// - error: any error that occurred during the operation
func (s *Storage) DeleteUserWorkspace(userID, workspaceID int) error {
workspacePath := s.GetWorkspacePath(userID, workspaceID)
err := s.fs.RemoveAll(workspacePath)
if err != nil {
return fmt.Errorf("failed to delete workspace directory: %w", err)
}
return nil
}
// CreateWorkspaceDirectory creates the workspace directory for the given user and workspace IDs.
func (fs *FileSystem) CreateWorkspaceDirectory(userID, workspaceID int) error {
dir := fs.GetWorkspacePath(userID, workspaceID)
return os.MkdirAll(dir, 0755)
}