-
Notifications
You must be signed in to change notification settings - Fork 0
/
element.go
78 lines (61 loc) · 1.77 KB
/
element.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package poller
import (
"os"
"path/filepath"
"time"
)
// Element is the interface which describes a Remote Element.
// An Element could be a file, struct or anything which could satisfy this interface.
// Elements are compared between cycles, using cached (previous cycle) and current cycle.
// Elements will be compared if the same key exists in both cached and current cycle.
type Element interface {
// key for the element
Name() string
// modification time
LastModified() time.Time
// is a directory
IsDirectory() bool
}
// A FileElement is an implementation of Element.
// FileElement stores os.FileInfo and can be used as
// for local filesystem files
type FileElement struct {
os.FileInfo
name string
}
// Name of file returned by FileInfo
func (f *FileElement) Name() string {
return f.name
}
// LastModified time of file returned by FileInfo
func (f *FileElement) LastModified() time.Time {
return f.FileInfo.ModTime()
}
// IsDirectory returned by FileInfo
func (f *FileElement) IsDirectory() bool {
return f.FileInfo.IsDir()
}
func (f *FileElement) ListFiles() ([]Element, error) {
var fileElements []Element
err := filepath.Walk(f.Name(), func(path string, info os.FileInfo, err error) error {
fileElements = append(fileElements, &FileElement{FileInfo: info, name: filepath.ToSlash(path)})
return nil
})
if err != nil {
return nil, err
}
return fileElements, nil
}
//NewFileDirectory creates a PolledDirectory for the specified root dir.
// This implementation handles nested directories.
func NewFileDirectory(rootFilePath string) (PolledDirectory, error) {
file, err := os.Open(rootFilePath)
if err != nil {
return nil, err
}
info, err := file.Stat()
if err != nil {
return nil, err
}
return &FileElement{FileInfo: info, name: rootFilePath}, nil
}