refactor: replace string slice search with slices package

Signed-off-by: Mark Sagi-Kazar <mark.sagikazar@gmail.com>
This commit is contained in:
Mark Sagi-Kazar 2024-07-20 15:42:51 +02:00 committed by Márk Sági-Kazár
parent ebe913cc53
commit 3268cbdcf4
3 changed files with 9 additions and 16 deletions

View file

@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"io" "io"
"reflect" "reflect"
"slices"
) )
// SupportedRemoteProviders are universally supported remote providers. // SupportedRemoteProviders are universally supported remote providers.
@ -93,7 +94,7 @@ func AddRemoteProvider(provider, endpoint, path string) error {
} }
func (v *Viper) AddRemoteProvider(provider, endpoint, path string) error { func (v *Viper) AddRemoteProvider(provider, endpoint, path string) error {
if !stringInSlice(provider, SupportedRemoteProviders) { if !slices.Contains(SupportedRemoteProviders, provider) {
return UnsupportedRemoteProviderError(provider) return UnsupportedRemoteProviderError(provider)
} }
if provider != "" && endpoint != "" { if provider != "" && endpoint != "" {
@ -126,7 +127,7 @@ func AddSecureRemoteProvider(provider, endpoint, path, secretkeyring string) err
} }
func (v *Viper) AddSecureRemoteProvider(provider, endpoint, path, secretkeyring string) error { func (v *Viper) AddSecureRemoteProvider(provider, endpoint, path, secretkeyring string) error {
if !stringInSlice(provider, SupportedRemoteProviders) { if !slices.Contains(SupportedRemoteProviders, provider) {
return UnsupportedRemoteProviderError(provider) return UnsupportedRemoteProviderError(provider)
} }
if provider != "" && endpoint != "" { if provider != "" && endpoint != "" {

View file

@ -128,15 +128,6 @@ func absPathify(logger *slog.Logger, inPath string) string {
return "" return ""
} }
func stringInSlice(a string, list []string) bool {
for _, b := range list {
if b == a {
return true
}
}
return false
}
func userHomeDir() string { func userHomeDir() string {
if runtime.GOOS == "windows" { if runtime.GOOS == "windows" {
home := os.Getenv("HOMEDRIVE") + os.Getenv("HOMEPATH") home := os.Getenv("HOMEDRIVE") + os.Getenv("HOMEPATH")

View file

@ -29,6 +29,7 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"reflect" "reflect"
"slices"
"strconv" "strconv"
"strings" "strings"
"sync" "sync"
@ -460,7 +461,7 @@ func (v *Viper) AddConfigPath(in string) {
absin := absPathify(v.logger, in) absin := absPathify(v.logger, in)
v.logger.Info("adding path to search paths", "path", absin) v.logger.Info("adding path to search paths", "path", absin)
if !stringInSlice(absin, v.configPaths) { if !slices.Contains(v.configPaths, absin) {
v.configPaths = append(v.configPaths, absin) v.configPaths = append(v.configPaths, absin)
} }
} }
@ -1478,7 +1479,7 @@ func (v *Viper) ReadInConfig() error {
return err return err
} }
if !stringInSlice(v.getConfigType(), SupportedExts) { if !slices.Contains(SupportedExts, v.getConfigType()) {
return UnsupportedConfigError(v.getConfigType()) return UnsupportedConfigError(v.getConfigType())
} }
@ -1509,7 +1510,7 @@ func (v *Viper) MergeInConfig() error {
return err return err
} }
if !stringInSlice(v.getConfigType(), SupportedExts) { if !slices.Contains(SupportedExts, v.getConfigType()) {
return UnsupportedConfigError(v.getConfigType()) return UnsupportedConfigError(v.getConfigType())
} }
@ -1616,7 +1617,7 @@ func (v *Viper) writeConfig(filename string, force bool) error {
return fmt.Errorf("config type could not be determined for %s", filename) return fmt.Errorf("config type could not be determined for %s", filename)
} }
if !stringInSlice(configType, SupportedExts) { if !slices.Contains(SupportedExts, configType) {
return UnsupportedConfigError(configType) return UnsupportedConfigError(configType)
} }
if v.config == nil { if v.config == nil {
@ -1645,7 +1646,7 @@ func (v *Viper) unmarshalReader(in io.Reader, c map[string]any) error {
format := strings.ToLower(v.getConfigType()) format := strings.ToLower(v.getConfigType())
if !stringInSlice(format, SupportedExts) { if !slices.Contains(SupportedExts, format) {
return UnsupportedConfigError(format) return UnsupportedConfigError(format)
} }