-
-
Notifications
You must be signed in to change notification settings - Fork 193
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #334 from hairyhenderson/add-file-directory-support
Adding directory support for file datasources
- Loading branch information
Showing
9 changed files
with
185 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package data | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"io/ioutil" | ||
"net/url" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/pkg/errors" | ||
|
||
"github.com/blang/vfs" | ||
) | ||
|
||
func readFile(source *Source, args ...string) ([]byte, error) { | ||
if source.FS == nil { | ||
source.FS = vfs.OS() | ||
} | ||
|
||
p := filepath.FromSlash(source.URL.Path) | ||
|
||
if len(args) == 1 { | ||
parsed, err := url.Parse(args[0]) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if parsed.Path != "" { | ||
p = p + "/" + parsed.Path | ||
} | ||
} | ||
|
||
// make sure we can access the file | ||
i, err := source.FS.Stat(p) | ||
if err != nil { | ||
return nil, errors.Wrapf(err, "Can't stat %s", p) | ||
} | ||
|
||
if strings.HasSuffix(p, "/") { | ||
source.Type = jsonArrayMimetype | ||
if i.IsDir() { | ||
return readFileDir(source, p) | ||
} | ||
return nil, errors.Errorf("%s is not a directory", p) | ||
} | ||
|
||
f, err := source.FS.OpenFile(p, os.O_RDONLY, 0) | ||
if err != nil { | ||
return nil, errors.Wrapf(err, "Can't open %s", p) | ||
} | ||
|
||
b, err := ioutil.ReadAll(f) | ||
if err != nil { | ||
return nil, errors.Wrapf(err, "Can't read %s", p) | ||
} | ||
return b, nil | ||
} | ||
|
||
func readFileDir(source *Source, p string) ([]byte, error) { | ||
names, err := source.FS.ReadDir(p) | ||
if err != nil { | ||
return nil, err | ||
} | ||
files := make([]string, len(names)) | ||
for i, v := range names { | ||
files[i] = v.Name() | ||
} | ||
|
||
var buf bytes.Buffer | ||
enc := json.NewEncoder(&buf) | ||
if err := enc.Encode(files); err != nil { | ||
return nil, err | ||
} | ||
b := buf.Bytes() | ||
// chop off the newline added by the json encoder | ||
return b[:len(b)-1], nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// +build !windows | ||
|
||
package data | ||
|
||
import ( | ||
"net/url" | ||
"testing" | ||
|
||
"github.com/blang/vfs" | ||
"github.com/blang/vfs/memfs" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func mustParseURL(in string) *url.URL { | ||
u, _ := url.Parse(in) | ||
return u | ||
} | ||
|
||
func TestReadFile(t *testing.T) { | ||
content := []byte(`hello world`) | ||
fs := memfs.Create() | ||
|
||
_ = fs.Mkdir("/tmp", 0777) | ||
f, _ := vfs.Create(fs, "/tmp/foo") | ||
_, _ = f.Write(content) | ||
|
||
_ = fs.Mkdir("/tmp/partial", 0777) | ||
f, _ = vfs.Create(fs, "/tmp/partial/foo.txt") | ||
_, _ = f.Write(content) | ||
_, _ = vfs.Create(fs, "/tmp/partial/bar.txt") | ||
_, _ = vfs.Create(fs, "/tmp/partial/baz.txt") | ||
|
||
source, _ := NewSource("foo", mustParseURL("file:///tmp/foo")) | ||
source.FS = fs | ||
|
||
actual, err := readFile(source) | ||
assert.NoError(t, err) | ||
assert.Equal(t, content, actual) | ||
|
||
source, _ = NewSource("bogus", mustParseURL("file:///bogus")) | ||
source.FS = fs | ||
_, err = readFile(source) | ||
assert.Error(t, err) | ||
|
||
source, _ = NewSource("partial", mustParseURL("file:///tmp/partial")) | ||
source.FS = fs | ||
actual, err = readFile(source, "foo.txt") | ||
assert.NoError(t, err) | ||
assert.Equal(t, content, actual) | ||
|
||
source, _ = NewSource("dir", mustParseURL("file:///tmp/partial/")) | ||
source.FS = fs | ||
actual, err = readFile(source) | ||
assert.NoError(t, err) | ||
assert.Equal(t, []byte(`["bar.txt","baz.txt","foo.txt"]`), actual) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package data | ||
|
||
const ( | ||
textMimetype = "text/plain" | ||
csvMimetype = "text/csv" | ||
jsonMimetype = "application/json" | ||
jsonArrayMimetype = "application/array+json" | ||
tomlMimetype = "application/toml" | ||
yamlMimetype = "application/yaml" | ||
) |
Oops, something went wrong.