diff options
author | Gabriel A. Giovanini <mail@gabrielgio.me> | 2024-06-08 00:01:44 +0200 |
---|---|---|
committer | Gabriel A. Giovanini <mail@gabrielgio.me> | 2024-06-08 00:01:44 +0200 |
commit | 6079b1d963f34ada5c4b25363f2319901e283936 (patch) | |
tree | f7f93616eb3dacfcebee486fe7542ec3adfb3950 /pkg/service | |
parent | e9098e00fb6339b759df5b0df2e086cef8a7ce83 (diff) | |
download | cerrado-6079b1d963f34ada5c4b25363f2319901e283936.tar.gz cerrado-6079b1d963f34ada5c4b25363f2319901e283936.tar.bz2 cerrado-6079b1d963f34ada5c4b25363f2319901e283936.zip |
feat: Add error handling
Diffstat (limited to 'pkg/service')
-rw-r--r-- | pkg/service/git.go | 29 |
1 files changed, 23 insertions, 6 deletions
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 { |