From d16298ea4fc8bcbf16a023a6a9b9f3b48bb3e9e2 Mon Sep 17 00:00:00 2001 From: Dave Henderson Date: Thu, 7 Mar 2019 09:13:32 -0500 Subject: Replacing uses of blang/vfs with spf13/afero Signed-off-by: Dave Henderson --- data/datasource.go | 15 ++++++++------- data/datasource_file.go | 8 ++++---- data/datasource_file_test.go | 14 +++++++------- data/datasource_merge_test.go | 14 +++++++------- data/datasource_test.go | 31 ++++++++++++++----------------- 5 files changed, 40 insertions(+), 42 deletions(-) (limited to 'data') diff --git a/data/datasource.go b/data/datasource.go index b723aea8..9236a88a 100644 --- a/data/datasource.go +++ b/data/datasource.go @@ -13,9 +13,10 @@ import ( "regexp" "strings" + "github.com/spf13/afero" + "github.com/pkg/errors" - "github.com/blang/vfs" "github.com/hairyhenderson/gomplate/libkv" "github.com/hairyhenderson/gomplate/vault" ) @@ -122,12 +123,12 @@ type Source struct { Alias string URL *url.URL mediaType string - fs vfs.Filesystem // used for file: URLs, nil otherwise - hc *http.Client // used for http[s]: URLs, nil otherwise - vc *vault.Vault // used for vault: URLs, nil otherwise - kv *libkv.LibKV // used for consul:, etcd:, zookeeper: & boltdb: URLs, nil otherwise - asmpg awssmpGetter // used for aws+smp:, nil otherwise - header http.Header // used for http[s]: URLs, nil otherwise + fs afero.Fs // used for file: URLs, nil otherwise + hc *http.Client // used for http[s]: URLs, nil otherwise + vc *vault.Vault // used for vault: URLs, nil otherwise + kv *libkv.LibKV // used for consul:, etcd:, zookeeper: & boltdb: URLs, nil otherwise + asmpg awssmpGetter // used for aws+smp:, nil otherwise + header http.Header // used for http[s]: URLs, nil otherwise } func (s *Source) inherit(parent *Source) { diff --git a/data/datasource_file.go b/data/datasource_file.go index 93367cc5..a94944cd 100644 --- a/data/datasource_file.go +++ b/data/datasource_file.go @@ -9,14 +9,14 @@ import ( "path/filepath" "strings" - "github.com/pkg/errors" + "github.com/spf13/afero" - "github.com/blang/vfs" + "github.com/pkg/errors" ) func readFile(source *Source, args ...string) ([]byte, error) { if source.fs == nil { - source.fs = vfs.OS() + source.fs = afero.NewOsFs() } p := filepath.FromSlash(source.URL.Path) @@ -59,7 +59,7 @@ func readFile(source *Source, args ...string) ([]byte, error) { } func readFileDir(source *Source, p string) ([]byte, error) { - names, err := source.fs.ReadDir(p) + names, err := afero.ReadDir(source.fs, p) if err != nil { return nil, err } diff --git a/data/datasource_file_test.go b/data/datasource_file_test.go index 3a402b3b..665173a0 100644 --- a/data/datasource_file_test.go +++ b/data/datasource_file_test.go @@ -5,24 +5,24 @@ package data import ( "testing" - "github.com/blang/vfs" - "github.com/blang/vfs/memfs" + "github.com/spf13/afero" + "github.com/stretchr/testify/assert" ) func TestReadFile(t *testing.T) { content := []byte(`hello world`) - fs := memfs.Create() + fs := afero.NewMemMapFs() _ = fs.Mkdir("/tmp", 0777) - f, _ := vfs.Create(fs, "/tmp/foo") + f, _ := fs.Create("/tmp/foo") _, _ = f.Write(content) _ = fs.Mkdir("/tmp/partial", 0777) - f, _ = vfs.Create(fs, "/tmp/partial/foo.txt") + f, _ = fs.Create("/tmp/partial/foo.txt") _, _ = f.Write(content) - _, _ = vfs.Create(fs, "/tmp/partial/bar.txt") - _, _ = vfs.Create(fs, "/tmp/partial/baz.txt") + _, _ = fs.Create("/tmp/partial/bar.txt") + _, _ = fs.Create("/tmp/partial/baz.txt") source := &Source{Alias: "foo", URL: mustParseURL("file:///tmp/foo")} source.fs = fs diff --git a/data/datasource_merge_test.go b/data/datasource_merge_test.go index b4ad7768..586d4dff 100644 --- a/data/datasource_merge_test.go +++ b/data/datasource_merge_test.go @@ -6,8 +6,8 @@ import ( "net/url" "testing" - "github.com/blang/vfs" - "github.com/blang/vfs/memfs" + "github.com/spf13/afero" + "github.com/stretchr/testify/assert" ) @@ -18,16 +18,16 @@ func TestReadMerge(t *testing.T) { mergedContent := []byte("goodnight: moon\nhello: world\n") - fs := memfs.Create() + fs := afero.NewMemMapFs() _ = fs.Mkdir("/tmp", 0777) - f, _ := vfs.Create(fs, "/tmp/jsonfile.json") + f, _ := fs.Create("/tmp/jsonfile.json") _, _ = f.Write(jsonContent) - f, _ = vfs.Create(fs, "/tmp/array.json") + f, _ = fs.Create("/tmp/array.json") _, _ = f.Write(arrayContent) - f, _ = vfs.Create(fs, "/tmp/yamlfile.yaml") + f, _ = fs.Create("/tmp/yamlfile.yaml") _, _ = f.Write(yamlContent) - f, _ = vfs.Create(fs, "/tmp/textfile.txt") + f, _ = fs.Create("/tmp/textfile.txt") _, _ = f.Write([]byte(`plain text...`)) source := &Source{Alias: "foo", URL: mustParseURL("merge:file:///tmp/jsonfile.json|file:///tmp/yamlfile.yaml")} diff --git a/data/datasource_test.go b/data/datasource_test.go index ea0eb719..ee6c6acf 100644 --- a/data/datasource_test.go +++ b/data/datasource_test.go @@ -7,8 +7,8 @@ import ( "strings" "testing" - "github.com/blang/vfs" - "github.com/blang/vfs/memfs" + "github.com/spf13/afero" + "github.com/stretchr/testify/assert" ) @@ -97,17 +97,17 @@ func TestParseSourceWithAlias(t *testing.T) { func TestDatasource(t *testing.T) { setup := func(ext, mime string, contents []byte) *Data { fname := "foo." + ext - fs := memfs.Create() + fs := afero.NewMemMapFs() var uPath string - var f vfs.File + var f afero.File if runtime.GOOS == "windows" { _ = fs.Mkdir("C:\\tmp", 0777) - f, _ = vfs.Create(fs, "C:\\tmp\\"+fname) + f, _ = fs.Create("C:\\tmp\\" + fname) _, _ = f.Write(contents) uPath = "C:/tmp/" + fname } else { _ = fs.Mkdir("/tmp", 0777) - f, _ = vfs.Create(fs, "/tmp/"+fname) + f, _ = fs.Create("/tmp/" + fname) uPath = "/tmp/" + fname } _, _ = f.Write(contents) @@ -144,16 +144,16 @@ func TestDatasource(t *testing.T) { func TestDatasourceReachable(t *testing.T) { fname := "foo.json" - fs := memfs.Create() + fs := afero.NewMemMapFs() var uPath string - var f vfs.File + var f afero.File if runtime.GOOS == "windows" { _ = fs.Mkdir("C:\\tmp", 0777) - f, _ = vfs.Create(fs, "C:\\tmp\\"+fname) + f, _ = fs.Create("C:\\tmp\\" + fname) uPath = "C:/tmp/" + fname } else { _ = fs.Mkdir("/tmp", 0777) - f, _ = vfs.Create(fs, "/tmp/"+fname) + f, _ = fs.Create("/tmp/" + fname) uPath = "/tmp/" + fname } _, _ = f.Write([]byte("{}")) @@ -190,20 +190,17 @@ func TestInclude(t *testing.T) { ext := "txt" contents := "hello world" fname := "foo." + ext - fs := memfs.Create() - // _ = fs.Mkdir("/tmp", 0777) - // f, _ := vfs.Create(fs, "/tmp/"+fname) - // _, _ = f.Write([]byte(contents)) + fs := afero.NewMemMapFs() var uPath string - var f vfs.File + var f afero.File if runtime.GOOS == "windows" { _ = fs.Mkdir("C:\\tmp", 0777) - f, _ = vfs.Create(fs, "C:\\tmp\\"+fname) + f, _ = fs.Create("C:\\tmp\\" + fname) uPath = "C:/tmp/" + fname } else { _ = fs.Mkdir("/tmp", 0777) - f, _ = vfs.Create(fs, "/tmp/"+fname) + f, _ = fs.Create("/tmp/" + fname) uPath = "/tmp/" + fname } _, _ = f.Write([]byte(contents)) -- cgit v1.2.3