Skip to content

Commit

Permalink
refactor: replace string slice search with slices package
Browse files Browse the repository at this point in the history
Signed-off-by: Mark Sagi-Kazar <[email protected]>
  • Loading branch information
sagikazarmark committed Jul 20, 2024
1 parent 11796f7 commit 7f20152
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 16 deletions.
5 changes: 3 additions & 2 deletions remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"io"
"reflect"
"slices"
)

// SupportedRemoteProviders are universally supported remote providers.
Expand Down Expand Up @@ -93,7 +94,7 @@ func 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)
}
if provider != "" && endpoint != "" {
Expand Down Expand Up @@ -126,7 +127,7 @@ func AddSecureRemoteProvider(provider, endpoint, path, secretkeyring string) err
}

func (v *Viper) AddSecureRemoteProvider(provider, endpoint, path, secretkeyring string) error {
if !stringInSlice(provider, SupportedRemoteProviders) {
if !slices.Contains(SupportedRemoteProviders, provider) {
return UnsupportedRemoteProviderError(provider)
}
if provider != "" && endpoint != "" {
Expand Down
9 changes: 0 additions & 9 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,15 +128,6 @@ func absPathify(logger *slog.Logger, inPath string) string {
return ""
}

func stringInSlice(a string, list []string) bool {
for _, b := range list {
if b == a {
return true
}
}
return false
}

func userHomeDir() string {
if runtime.GOOS == "windows" {
home := os.Getenv("HOMEDRIVE") + os.Getenv("HOMEPATH")
Expand Down
11 changes: 6 additions & 5 deletions viper.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"os"
"path/filepath"
"reflect"
"slices"
"strconv"
"strings"
"sync"
Expand Down Expand Up @@ -460,7 +461,7 @@ func (v *Viper) AddConfigPath(in string) {
absin := absPathify(v.logger, in)

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)
}
}
Expand Down Expand Up @@ -1478,7 +1479,7 @@ func (v *Viper) ReadInConfig() error {
return err
}

if !stringInSlice(v.getConfigType(), SupportedExts) {
if !slices.Contains(SupportedExts, v.getConfigType()) {
return UnsupportedConfigError(v.getConfigType())
}

Expand Down Expand Up @@ -1509,7 +1510,7 @@ func (v *Viper) MergeInConfig() error {
return err
}

if !stringInSlice(v.getConfigType(), SupportedExts) {
if !slices.Contains(SupportedExts, v.getConfigType()) {
return UnsupportedConfigError(v.getConfigType())
}

Expand Down Expand Up @@ -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)
}

if !stringInSlice(configType, SupportedExts) {
if !slices.Contains(SupportedExts, configType) {
return UnsupportedConfigError(configType)
}
if v.config == nil {
Expand Down Expand Up @@ -1645,7 +1646,7 @@ func (v *Viper) unmarshalReader(in io.Reader, c map[string]any) error {

format := strings.ToLower(v.getConfigType())

if !stringInSlice(format, SupportedExts) {
if !slices.Contains(SupportedExts, format) {
return UnsupportedConfigError(format)
}

Expand Down

0 comments on commit 7f20152

Please sign in to comment.