From 6079b1d963f34ada5c4b25363f2319901e283936 Mon Sep 17 00:00:00 2001 From: "Gabriel A. Giovanini" Date: Sat, 8 Jun 2024 00:01:44 +0200 Subject: feat: Add error handling --- pkg/service/git.go | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) (limited to 'pkg/service') diff --git a/pkg/service/git.go b/pkg/service/git.go index 31a1cbb..94e2adc 100644 --- a/pkg/service/git.go +++ b/pkg/service/git.go @@ -1,6 +1,7 @@ package service import ( + "errors" "log/slog" "os" "path" @@ -31,6 +32,10 @@ type ( } ) +var ( + RepositoryNotFoundErr = errors.New("Repository not found") +) + // TODO: make it configurable const timeFormat = "2006.01.02 15:04:05" @@ -84,8 +89,10 @@ func (g *GitService) ListRepositories() ([]*Repository, error) { } func (g *GitService) ListCommits(name, ref string) ([]*object.Commit, error) { - // TODO: handle nil r := g.configRepo.GetByName(name) + if r == nil { + return nil, RepositoryNotFoundErr + } repo, err := git.OpenRepository(r.Path) if err != nil { @@ -100,8 +107,10 @@ func (g *GitService) ListCommits(name, ref string) ([]*object.Commit, error) { } func (g *GitService) GetTree(name, ref, path string) (*object.Tree, error) { - // TODO: handle nil r := g.configRepo.GetByName(name) + if r == nil { + return nil, RepositoryNotFoundErr + } repo, err := git.OpenRepository(r.Path) if err != nil { @@ -116,8 +125,10 @@ func (g *GitService) GetTree(name, ref, path string) (*object.Tree, error) { } func (g *GitService) GetFileContent(name, ref, path string) (string, error) { - // TODO: handle nil r := g.configRepo.GetByName(name) + if r == nil { + return "", RepositoryNotFoundErr + } repo, err := git.OpenRepository(r.Path) if err != nil { @@ -132,8 +143,10 @@ func (g *GitService) GetFileContent(name, ref, path string) (string, error) { } func (g *GitService) ListTags(name string) ([]*object.Tag, error) { - // TODO: handle nil r := g.configRepo.GetByName(name) + if r == nil { + return nil, RepositoryNotFoundErr + } repo, err := git.OpenRepository(r.Path) if err != nil { @@ -143,8 +156,10 @@ func (g *GitService) ListTags(name string) ([]*object.Tag, error) { } func (g *GitService) ListBranches(name string) ([]*plumbing.Reference, error) { - // TODO: handle nil r := g.configRepo.GetByName(name) + if r == nil { + return nil, RepositoryNotFoundErr + } repo, err := git.OpenRepository(r.Path) if err != nil { @@ -154,8 +169,10 @@ func (g *GitService) ListBranches(name string) ([]*plumbing.Reference, error) { } func (g *GitService) GetHead(name string) (*plumbing.Reference, error) { - // TODO: handle nil r := g.configRepo.GetByName(name) + if r == nil { + return nil, RepositoryNotFoundErr + } repo, err := git.OpenRepository(r.Path) if err != nil { -- cgit v1.2.3