mirror of
https://github.com/spf13/viper
synced 2024-12-22 03:27:03 +00:00
feat: use new finder library
Signed-off-by: Mark Sagi-Kazar <mark.sagikazar@gmail.com>
This commit is contained in:
parent
6fdfebc843
commit
cfa8fd9b4f
5 changed files with 25 additions and 176 deletions
65
fs.go
65
fs.go
|
@ -1,65 +0,0 @@
|
|||
//go:build go1.16 && finder
|
||||
// +build go1.16,finder
|
||||
|
||||
package viper
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"io/fs"
|
||||
"path"
|
||||
)
|
||||
|
||||
type finder struct {
|
||||
paths []string
|
||||
fileNames []string
|
||||
extensions []string
|
||||
|
||||
withoutExtension bool
|
||||
}
|
||||
|
||||
func (f finder) Find(fsys fs.FS) (string, error) {
|
||||
for _, searchPath := range f.paths {
|
||||
for _, fileName := range f.fileNames {
|
||||
for _, extension := range f.extensions {
|
||||
filePath := path.Join(searchPath, fileName+"."+extension)
|
||||
|
||||
ok, err := fileExists(fsys, filePath)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
if ok {
|
||||
return filePath, nil
|
||||
}
|
||||
}
|
||||
|
||||
if f.withoutExtension {
|
||||
filePath := path.Join(searchPath, fileName)
|
||||
|
||||
ok, err := fileExists(fsys, filePath)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
if ok {
|
||||
return filePath, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return "", nil
|
||||
}
|
||||
|
||||
func fileExists(fsys fs.FS, filePath string) (bool, error) {
|
||||
fileInfo, err := fs.Stat(fsys, filePath)
|
||||
if err == nil {
|
||||
return !fileInfo.IsDir(), nil
|
||||
}
|
||||
|
||||
if errors.Is(err, fs.ErrNotExist) {
|
||||
return false, nil
|
||||
}
|
||||
|
||||
return false, err
|
||||
}
|
100
fs_test.go
100
fs_test.go
|
@ -1,100 +0,0 @@
|
|||
//go:build go1.16 && finder
|
||||
// +build go1.16,finder
|
||||
|
||||
package viper
|
||||
|
||||
import (
|
||||
"io/fs"
|
||||
"testing"
|
||||
"testing/fstest"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestFinder(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
fsys := fstest.MapFS{
|
||||
"home/user/.config": &fstest.MapFile{},
|
||||
"home/user/config.json": &fstest.MapFile{},
|
||||
"home/user/config.yaml": &fstest.MapFile{},
|
||||
"home/user/data.json": &fstest.MapFile{},
|
||||
"etc/config/.config": &fstest.MapFile{},
|
||||
"etc/config/a_random_file.txt": &fstest.MapFile{},
|
||||
"etc/config/config.json": &fstest.MapFile{},
|
||||
"etc/config/config.yaml": &fstest.MapFile{},
|
||||
"etc/config/config.xml": &fstest.MapFile{},
|
||||
}
|
||||
|
||||
testCases := []struct {
|
||||
name string
|
||||
fsys func() fs.FS
|
||||
finder finder
|
||||
result string
|
||||
}{
|
||||
{
|
||||
name: "find file",
|
||||
fsys: func() fs.FS { return fsys },
|
||||
finder: finder{
|
||||
paths: []string{"etc/config"},
|
||||
fileNames: []string{"config"},
|
||||
extensions: []string{"json"},
|
||||
},
|
||||
result: "etc/config/config.json",
|
||||
},
|
||||
{
|
||||
name: "file not found",
|
||||
fsys: func() fs.FS { return fsys },
|
||||
finder: finder{
|
||||
paths: []string{"var/config"},
|
||||
fileNames: []string{"config"},
|
||||
extensions: []string{"json"},
|
||||
},
|
||||
result: "",
|
||||
},
|
||||
{
|
||||
name: "empty search params",
|
||||
fsys: func() fs.FS { return fsys },
|
||||
finder: finder{},
|
||||
result: "",
|
||||
},
|
||||
{
|
||||
name: "precedence",
|
||||
fsys: func() fs.FS { return fsys },
|
||||
finder: finder{
|
||||
paths: []string{"var/config", "home/user", "etc/config"},
|
||||
fileNames: []string{"aconfig", "config"},
|
||||
extensions: []string{"zml", "xml", "json"},
|
||||
},
|
||||
result: "home/user/config.json",
|
||||
},
|
||||
{
|
||||
name: "without extension",
|
||||
fsys: func() fs.FS { return fsys },
|
||||
finder: finder{
|
||||
paths: []string{"var/config", "home/user", "etc/config"},
|
||||
fileNames: []string{".config"},
|
||||
extensions: []string{"zml", "xml", "json"},
|
||||
|
||||
withoutExtension: true,
|
||||
},
|
||||
result: "home/user/.config",
|
||||
},
|
||||
}
|
||||
|
||||
for _, testCase := range testCases {
|
||||
testCase := testCase
|
||||
|
||||
t.Run(testCase.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
fsys := testCase.fsys()
|
||||
|
||||
result, err := testCase.finder.Find(fsys)
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.Equal(t, testCase.result, result)
|
||||
})
|
||||
}
|
||||
}
|
4
go.mod
4
go.mod
|
@ -9,6 +9,7 @@ require (
|
|||
github.com/mitchellh/mapstructure v1.5.0
|
||||
github.com/pelletier/go-toml/v2 v2.1.0
|
||||
github.com/sagikazarmark/crypt v0.14.0
|
||||
github.com/sagikazarmark/go-finder v0.1.0
|
||||
github.com/sagikazarmark/slog-shim v0.1.0
|
||||
github.com/spf13/afero v1.9.5
|
||||
github.com/spf13/cast v1.5.1
|
||||
|
@ -58,13 +59,14 @@ require (
|
|||
github.com/nats-io/nuid v1.0.1 // indirect
|
||||
github.com/pkg/errors v0.9.1 // indirect
|
||||
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
|
||||
github.com/sourcegraph/conc v0.3.0 // indirect
|
||||
go.etcd.io/etcd/api/v3 v3.5.9 // indirect
|
||||
go.etcd.io/etcd/client/pkg/v3 v3.5.9 // indirect
|
||||
go.etcd.io/etcd/client/v2 v2.305.9 // indirect
|
||||
go.etcd.io/etcd/client/v3 v3.5.9 // indirect
|
||||
go.opencensus.io v0.24.0 // indirect
|
||||
go.uber.org/atomic v1.9.0 // indirect
|
||||
go.uber.org/multierr v1.8.0 // indirect
|
||||
go.uber.org/multierr v1.9.0 // indirect
|
||||
go.uber.org/zap v1.21.0 // indirect
|
||||
golang.org/x/crypto v0.13.0 // indirect
|
||||
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
|
||||
|
|
8
go.sum
8
go.sum
|
@ -330,12 +330,16 @@ github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZV
|
|||
github.com/ryanuber/columnize v0.0.0-20160712163229-9b3edd62028f/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts=
|
||||
github.com/sagikazarmark/crypt v0.14.0 h1:+QD5vjd6aZd6moHuIRVL+uJO7fkhiRjMz3ldbZQY5go=
|
||||
github.com/sagikazarmark/crypt v0.14.0/go.mod h1:JuxBJUNXrVOAywrtQNTZpOeTgcL1Az5qM7jKVDOifig=
|
||||
github.com/sagikazarmark/go-finder v0.1.0 h1:5f1zyaYxU8FBMyPW7XnFOH6xZ6O9hm+m65CNjQ5snCM=
|
||||
github.com/sagikazarmark/go-finder v0.1.0/go.mod h1:kS8MkoGpA6O0X1ddmi8O5g9YXwbVd9grjG8XCVehFEY=
|
||||
github.com/sagikazarmark/slog-shim v0.1.0 h1:diDBnUNK9N/354PgrxMywXnAwEr1QZcOr6gto+ugjYE=
|
||||
github.com/sagikazarmark/slog-shim v0.1.0/go.mod h1:SrcSrq8aKtyuqEI1uvTDTK1arOWRIczQRv+GVI1AkeQ=
|
||||
github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529 h1:nn5Wsu0esKSJiIVhscUtVbo7ada43DJhG55ua/hjS5I=
|
||||
github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc=
|
||||
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
|
||||
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
|
||||
github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9ySo=
|
||||
github.com/sourcegraph/conc v0.3.0/go.mod h1:Sdozi7LEKbFPqYX2/J+iBAM6HpqSLTASQIKqDmF7Mt0=
|
||||
github.com/spf13/afero v1.9.5 h1:stMpOSZFs//0Lv29HduCmli3GUfpFoF3Y1Q/aXj/wVM=
|
||||
github.com/spf13/afero v1.9.5/go.mod h1:UBogFpq8E9Hx+xc5CNTTEpTnuHVmXDwZcZcE1eb/UhQ=
|
||||
github.com/spf13/cast v1.5.1 h1:R+kOtfhWQE6TVQzY+4D7wJLBgkdVasCEFxSUBYBYIlA=
|
||||
|
@ -390,8 +394,8 @@ go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
|
|||
go.uber.org/goleak v1.1.11 h1:wy28qYRKZgnJTxGxvye5/wgWr1EKjmUDGYox5mGlRlI=
|
||||
go.uber.org/goleak v1.1.11/go.mod h1:cwTWslyiVhfpKIDGSZEM2HlOvcqm+tG4zioyIeLoqMQ=
|
||||
go.uber.org/multierr v1.6.0/go.mod h1:cdWPpRnG4AhwMwsgIHip0KRBQjJy5kYEpYjJxpXp9iU=
|
||||
go.uber.org/multierr v1.8.0 h1:dg6GjLku4EH+249NNmoIciG9N/jURbDG+pFlTkhzIC8=
|
||||
go.uber.org/multierr v1.8.0/go.mod h1:7EAYxJLBy9rStEaz58O2t4Uvip6FSURkq8/ppBp95ak=
|
||||
go.uber.org/multierr v1.9.0 h1:7fIwc/ZtS0q++VgcfqFDxSBZVv/Xo49/SYnDFupUwlI=
|
||||
go.uber.org/multierr v1.9.0/go.mod h1:X2jQV1h+kxSjClGpnseKVIxpmcjrj7MNnI0bnlfKTVQ=
|
||||
go.uber.org/zap v1.21.0 h1:WefMeulhovoZ2sYXz7st6K0sLj7bBhpiFaud4r4zST8=
|
||||
go.uber.org/zap v1.21.0/go.mod h1:wjWOCqI0f2ZZrJF/UufIOkiC8ii6tm1iqIsLo76RfJw=
|
||||
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
|
||||
|
|
|
@ -6,27 +6,35 @@ package viper
|
|||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/sagikazarmark/go-finder"
|
||||
"github.com/spf13/afero"
|
||||
)
|
||||
|
||||
// Search all configPaths for any config file.
|
||||
// Returns the first path that exists (and is a config file).
|
||||
func (v *Viper) findConfigFile() (string, error) {
|
||||
finder := finder{
|
||||
paths: v.configPaths,
|
||||
fileNames: []string{v.configName},
|
||||
extensions: SupportedExts,
|
||||
withoutExtension: v.configType != "",
|
||||
var names []string
|
||||
|
||||
if v.configType != "" {
|
||||
names = finder.NameWithOptionalExtensions(v.configName, SupportedExts...)
|
||||
} else {
|
||||
names = finder.NameWithExtensions(v.configName, SupportedExts...)
|
||||
}
|
||||
|
||||
file, err := finder.Find(afero.NewIOFS(v.fs))
|
||||
finder := finder.Finder{
|
||||
Paths: v.configPaths,
|
||||
Names: names,
|
||||
Type: finder.FileTypeFile,
|
||||
}
|
||||
|
||||
results, err := finder.Find(afero.NewIOFS(v.fs))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
if file == "" {
|
||||
if len(results) == 0 {
|
||||
return "", ConfigFileNotFoundError{v.configName, fmt.Sprintf("%s", v.configPaths)}
|
||||
}
|
||||
|
||||
return file, nil
|
||||
return results[0], nil
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue