forked from gookit/goutil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathelem.go
49 lines (41 loc) · 956 Bytes
/
elem.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package finder
import (
"io/fs"
"github.com/gookit/goutil/strutil"
)
// Elem of find file/dir path result
type Elem interface {
fs.DirEntry
// Path get file/dir full path. eg: "/path/to/file.go"
Path() string
// Info get file info. like fs.DirEntry.Info(), but will cache result.
Info() (fs.FileInfo, error)
}
type elem struct {
fs.DirEntry
path string
stat fs.FileInfo
sErr error
}
// NewElem create a new Elem instance
func NewElem(fPath string, ent fs.DirEntry) Elem {
return &elem{
path: fPath,
DirEntry: ent,
}
}
// Path get full file/dir path. eg: "/path/to/file.go"
func (e *elem) Path() string {
return e.path
}
// Info get file info, will cache result
func (e *elem) Info() (fs.FileInfo, error) {
if e.stat == nil {
e.stat, e.sErr = e.DirEntry.Info()
}
return e.stat, e.sErr
}
// String get string representation
func (e *elem) String() string {
return strutil.OrCond(e.IsDir(), "dir: ", "file: ") + e.Path()
}