fix: remove repeated use of the same fmt.Errorf() calls

Steve Moyer created

Change summary

repository/config.go       |  9 +++++++++
repository/config_mem.go   |  2 +-
repository/gogit_config.go | 12 ++++++------
3 files changed, 16 insertions(+), 7 deletions(-)

Detailed changes

repository/config.go 🔗

@@ -2,6 +2,7 @@ package repository
 
 import (
 	"errors"
+	"fmt"
 	"strconv"
 	"time"
 )
@@ -11,6 +12,14 @@ var (
 	ErrMultipleConfigEntry = errors.New("multiple config entry for the given key")
 )
 
+func newErrNoConfigEntry(key string) error {
+	return fmt.Errorf("%w: missing key %s", ErrNoConfigEntry, key)
+}
+
+func newErrMultipleConfigEntry(key string) error {
+	return fmt.Errorf("%w: duplicated key %s", ErrMultipleConfigEntry, key)
+}
+
 // Config represent the common function interacting with the repository config storage
 type Config interface {
 	ConfigRead

repository/config_mem.go 🔗

@@ -49,7 +49,7 @@ func (mc *MemConfig) ReadString(key string) (string, error) {
 	key = normalizeKey(key)
 	val, ok := mc.config[key]
 	if !ok {
-		return "", fmt.Errorf("%w: missing key %s", ErrNoConfigEntry, key)
+		return "", newErrNoConfigEntry(key)
 	}
 
 	return val, nil

repository/gogit_config.go 🔗

@@ -119,7 +119,7 @@ func (cr *goGitConfigReader) ReadString(key string) (string, error) {
 
 	sectionName := split[0]
 	if !cfg.Raw.HasSection(sectionName) {
-		return "", fmt.Errorf("%w: missing key %s", ErrNoConfigEntry, key)
+		return "", newErrNoConfigEntry(key)
 	}
 	section := cfg.Raw.Section(sectionName)
 
@@ -127,24 +127,24 @@ func (cr *goGitConfigReader) ReadString(key string) (string, error) {
 	case len(split) == 2:
 		optionName := split[1]
 		if !section.HasOption(optionName) {
-			return "", fmt.Errorf("%w: missing key %s", ErrNoConfigEntry, key)
+			return "", newErrNoConfigEntry(key)
 		}
 		if len(section.OptionAll(optionName)) > 1 {
-			return "", fmt.Errorf("%w: duplicated key %s", ErrMultipleConfigEntry, key)
+			return "", newErrMultipleConfigEntry(key)
 		}
 		return section.Option(optionName), nil
 	default:
 		subsectionName := strings.Join(split[1:len(split)-1], ".")
 		optionName := split[len(split)-1]
 		if !section.HasSubsection(subsectionName) {
-			return "", fmt.Errorf("%w: missing key %s", ErrNoConfigEntry, key)
+			return "", newErrNoConfigEntry(key)
 		}
 		subsection := section.Subsection(subsectionName)
 		if !subsection.HasOption(optionName) {
-			return "", fmt.Errorf("%w: missing key %s", ErrNoConfigEntry, key)
+			return "", newErrNoConfigEntry(key)
 		}
 		if len(subsection.OptionAll(optionName)) > 1 {
-			return "", fmt.Errorf("%w: duplicated key %s", ErrMultipleConfigEntry, key)
+			return "", newErrMultipleConfigEntry(key)
 		}
 		return subsection.Option(optionName), nil
 	}