From 29d40969c2ab66955a9e0d175b64a306d3decd13 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miko=C5=82aj=20P=C4=99czkowski?= Date: Sun, 16 Jun 2024 15:28:12 +0200 Subject: [PATCH] Improve code readability #2 + Improve syntax --- internal/commands/status_cmd.go | 4 +-- internal/commands/sync_cmd.go | 38 ++++++++++++----------- internal/config/config_file_parse_test.go | 9 +++--- 3 files changed, 27 insertions(+), 24 deletions(-) diff --git a/internal/commands/status_cmd.go b/internal/commands/status_cmd.go index e559dfb..db55e69 100644 --- a/internal/commands/status_cmd.go +++ b/internal/commands/status_cmd.go @@ -54,8 +54,8 @@ func findNumberOfCommitDiffs(srcCommit *object.Commit, dstCommit *object.Commit) return diffRange, false } - baseCommitHashes := []string{} - destCommitHashes := []string{} + var baseCommitHashes []string + var destCommitHashes []string // Try to find all differences, limit only to five last changes to avoid reading whole repository at once for { diff --git a/internal/commands/sync_cmd.go b/internal/commands/sync_cmd.go index dc03bec..b82e6f6 100644 --- a/internal/commands/sync_cmd.go +++ b/internal/commands/sync_cmd.go @@ -1,6 +1,7 @@ package commands import ( + "errors" "fmt" "gitlab.com/revalus/grm/internal/config" @@ -26,15 +27,14 @@ const ( func fetchRepository(repo *git.Repository) (bool, error) { err := repo.Fetch(&git.FetchOptions{}) - if err == git.NoErrAlreadyUpToDate { + switch { + case errors.Is(err, git.NoErrAlreadyUpToDate): return false, nil + case errors.Is(err, git.NoErrAlreadyUpToDate): + return false, nil + default: + return true, nil } - - if err != nil && err != git.NoErrAlreadyUpToDate { - return false, err - } - - return true, nil } func cloneRepository(destPath string, repoCfg *config.RepositoryConfig) (bool, error) { @@ -62,22 +62,24 @@ func (s Synchronizer) Command(repoCfg config.RepositoryConfig) CommandStatus { destPath := fmt.Sprintf("%v/%v", s.workspace, repoCfg.Dest) repo, err := git.PlainOpen(destPath) - if err != nil && err == git.ErrRepositoryNotExists { - cmdStatus.Changed, err = cloneRepository(destPath, &repoCfg) - cmdStatus.Message = syncCloned - } else if err == nil { - cmdStatus.Changed, err = fetchRepository(repo) - if cmdStatus.Changed { - cmdStatus.Message = syncFetched + if err != nil { + if errors.Is(err, git.ErrRepositoryNotExists) { + cmdStatus.Changed, err = cloneRepository(destPath, &repoCfg) + cmdStatus.Message = syncCloned } else { - cmdStatus.Message = syncUpToDate + cmdStatus.Error = true + cmdStatus.Message = err.Error() } - } else { - cmdStatus.Error = true - cmdStatus.Message = err.Error() return cmdStatus } + cmdStatus.Changed, err = fetchRepository(repo) + if cmdStatus.Changed { + cmdStatus.Message = syncFetched + } else { + cmdStatus.Message = syncUpToDate + } + if err != nil { cmdStatus.Error = true cmdStatus.Message = err.Error() diff --git a/internal/config/config_file_parse_test.go b/internal/config/config_file_parse_test.go index e5fe458..3726ec2 100644 --- a/internal/config/config_file_parse_test.go +++ b/internal/config/config_file_parse_test.go @@ -1,6 +1,7 @@ package config import ( + "errors" "fmt" "os" "reflect" @@ -101,9 +102,9 @@ repositories: `) _, err := GetRepositoryConfig(exampleWrongYamlConfig, "yaml") - expectedError := fmt.Sprintf(errMissingSrcField, 0) + expectedError := errors.New(fmt.Sprintf(errMissingSrcField, 0)) - if err.Error() != expectedError { + if errors.Is(err, expectedError) { t.Errorf("Expected to get error with value %v, instead of this got: %v", expectedError, err.Error()) } } @@ -127,7 +128,7 @@ repositories: } expectedError := getDuplicateFieldError("name", "example2", []int{1, 2}) - if err.Error() != expectedError.Error() { + if errors.Is(err, expectedError) { t.Errorf("Expected to get error with value %v, instead of this got: %v", expectedError.Error(), err.Error()) } } @@ -151,7 +152,7 @@ repositories: expectedError := getDuplicateFieldError("dest", "example", []int{1, 2}) - if err.Error() != expectedError.Error() { + if errors.Is(err, expectedError) { t.Errorf("Expected to get error with value \"%v\", instead of this got: \"%v\"", expectedError, err) } }