commands: Documented File and implementations
Matt Bell committed
Nov 17, 2014 at 03:09 UTC
a9d784cb21c8788a1e65e242d83067229a509c82
1 file changed
+20
commands/file.go
+20
@@ -20,13 +20,28 @@ var (
20
ErrNotReader = errors.New("This file is a directory, can't use Reader functions")
21
)
22
23
+// File is an interface that provides functionality for handling files/directories
24
+// as values that can be supplied to commands. For directories, child files are
25
+// accessed serially by calling `NextFile()`.
26
type File interface {
27
+ // Files implement ReadCloser, but can only be read from or closed if they are not directories
28
io.ReadCloser
29
+
30
+ // FileName returns a full filename path associated with this file
31
FileName() string
32
+
33
+ // IsDirectory returns true if the File is a directory (and therefore supports calling `NextFile`)
34
+ // and false if the File is a normal file (and therefor supports calling `Read` and `Close`)
35
IsDirectory() bool
36
+
37
+ // NextFile returns the next child file available (if the File is a directory).
38
+ // It will return (nil, io.EOF) if no more files are available.
39
+ // If the file is a regular file (not a directory), NextFile will return a non-nil error.
40
NextFile() (File, error)
41
}
42
43
+// MultipartFile implements File, and is created from a `multipart.Part`.
44
+// It can be either a directory or file (checked by calling `IsDirectory()`).
45
type MultipartFile struct {
46
File
47
@@ -96,6 +111,9 @@ func (f *MultipartFile) Close() error {
111
return f.Part.Close()
112
}
113
114
+// SliceFile implements File, and provides simple directory handling.
115
+// It contains children files, and is created from a `[]File`.
116
+// SliceFiles are always directories, and can't be read from or closed.
117
type SliceFile struct {
118
Filename string
119
Files []File
@@ -126,6 +144,8 @@ func (f *SliceFile) Close() error {
144
return ErrNotReader
145
}
146
147
+// ReaderFile is a implementation of File created from an `io.Reader`.
148
+// ReaderFiles are never directories, and can be read from and closed.
149
type ReaderFile struct {
150
Filename string
151
Reader io.Reader