file.go 1.45 KB
Newer Older
1 2 3 4 5
package files

import (
	"errors"
	"io"
6
	"os"
7 8 9
)

var (
10
	ErrNotDirectory = errors.New("Couldn't call NextFile(), this isn't a directory")
11 12 13
	ErrNotReader    = errors.New("This file is a directory, can't use Reader functions")
)

14 15 16
// File is an interface that provides functionality for handling
// files/directories as values that can be supplied to commands. For
// directories, child files are accessed serially by calling `NextFile()`.
17
type File interface {
18 19
	// Files implement ReadCloser, but can only be read from or closed if
	// they are not directories
20 21
	io.ReadCloser

22
	// FileName returns a filename associated with this file
23 24
	FileName() string

25
	// FullPath returns the full path used when adding with this file
26 27
	FullPath() string

28 29 30
	// IsDirectory returns true if the File is a directory (and therefore
	// supports calling `NextFile`) and false if the File is a normal file
	// (and therefor supports calling `Read` and `Close`)
31 32
	IsDirectory() bool

33 34 35 36
	// NextFile returns the next child file available (if the File is a
	// directory). It will return (nil, io.EOF) if no more files are
	// available. If the file is a regular file (not a directory), NextFile
	// will return a non-nil error.
37 38
	NextFile() (File, error)
}
39 40 41 42 43 44 45 46

type StatFile interface {
	File

	Stat() os.FileInfo
}

type PeekFile interface {
47
	SizeFile
48 49 50 51

	Peek(n int) File
	Length() int
}
52 53 54 55 56 57

type SizeFile interface {
	File

	Size() (int64, error)
}
58 59

type FileInfo interface {
60
	AbsPath() string
61 62
	Stat() os.FileInfo
}