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

@@ -6,28 +6,45 @@ import (
)
// SetupGitRepo sets up a Git repository for the given user and workspace IDs.
func (fs *FileSystem) SetupGitRepo(userID, workspaceID int, gitURL, gitUser, gitToken string) error {
workspacePath := fs.GetWorkspacePath(userID, workspaceID)
if _, ok := fs.GitRepos[userID]; !ok {
fs.GitRepos[userID] = make(map[int]*gitutils.GitRepo)
// Parameters:
// - userID: the ID of the user who owns the workspace
// - workspaceID: the ID of the workspace to set up the Git repository for
// - gitURL: the URL of the Git repository
// - gitUser: the username for the Git repository
// - gitToken: the access token for the Git repository
// Returns:
// - error: any error that occurred during setup
func (s *Storage) SetupGitRepo(userID, workspaceID int, gitURL, gitUser, gitToken string) error {
workspacePath := s.GetWorkspacePath(userID, workspaceID)
if _, ok := s.GitRepos[userID]; !ok {
s.GitRepos[userID] = make(map[int]*gitutils.GitRepo)
}
fs.GitRepos[userID][workspaceID] = gitutils.New(gitURL, gitUser, gitToken, workspacePath)
return fs.GitRepos[userID][workspaceID].EnsureRepo()
s.GitRepos[userID][workspaceID] = gitutils.New(gitURL, gitUser, gitToken, workspacePath)
return s.GitRepos[userID][workspaceID].EnsureRepo()
}
// DisableGitRepo disables the Git repository for the given user and workspace IDs.
func (fs *FileSystem) DisableGitRepo(userID, workspaceID int) {
if userRepos, ok := fs.GitRepos[userID]; ok {
// Parameters:
// - userID: the ID of the user who owns the workspace
// - workspaceID: the ID of the workspace to disable the Git repository for
func (s *Storage) DisableGitRepo(userID, workspaceID int) {
if userRepos, ok := s.GitRepos[userID]; ok {
delete(userRepos, workspaceID)
if len(userRepos) == 0 {
delete(fs.GitRepos, userID)
delete(s.GitRepos, userID)
}
}
}
// StageCommitAndPush stages, commits, and pushes the changes to the Git repository.
func (fs *FileSystem) StageCommitAndPush(userID, workspaceID int, message string) error {
repo, ok := fs.getGitRepo(userID, workspaceID)
// Parameters:
// - userID: the ID of the user who owns the workspace
// - workspaceID: the ID of the workspace to commit and push
// - message: the commit message
// Returns:
// - error: any error that occurred during the operation
func (s *Storage) StageCommitAndPush(userID, workspaceID int, message string) error {
repo, ok := s.getGitRepo(userID, workspaceID)
if !ok {
return fmt.Errorf("git settings not configured for this workspace")
}
@@ -40,8 +57,13 @@ func (fs *FileSystem) StageCommitAndPush(userID, workspaceID int, message string
}
// Pull pulls the changes from the remote Git repository.
func (fs *FileSystem) Pull(userID, workspaceID int) error {
repo, ok := fs.getGitRepo(userID, workspaceID)
// Parameters:
// - userID: the ID of the user who owns the workspace
// - workspaceID: the ID of the workspace to pull changes for
// Returns:
// - error: any error that occurred during the operation
func (s *Storage) Pull(userID, workspaceID int) error {
repo, ok := s.getGitRepo(userID, workspaceID)
if !ok {
return fmt.Errorf("git settings not configured for this workspace")
}
@@ -50,8 +72,8 @@ func (fs *FileSystem) Pull(userID, workspaceID int) error {
}
// getGitRepo returns the Git repository for the given user and workspace IDs.
func (fs *FileSystem) getGitRepo(userID, workspaceID int) (*gitutils.GitRepo, bool) {
userRepos, ok := fs.GitRepos[userID]
func (s *Storage) getGitRepo(userID, workspaceID int) (*gitutils.GitRepo, bool) {
userRepos, ok := s.GitRepos[userID]
if !ok {
return nil, false
}