-
Notifications
You must be signed in to change notification settings - Fork 0
/
filelister.go
78 lines (67 loc) · 1.66 KB
/
filelister.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 main
import (
"os"
"path"
"path/filepath"
"time"
)
// ListEntry is an entry that appears in the UI file input. All FileSpecs ultimately result in one or more ListEntry instances, which the server ships off to the client.
type ListEntry struct {
Path string `json:"path"`
Alias string `json:"alias"`
Size int64 `json:"size"`
ModTime time.Time `json:"mtime"`
Exists bool `json:"exists"`
}
func fileInfo(path string) *ListEntry {
entry := ListEntry{}
entry.Path = path
info, err := os.Stat(path)
if !os.IsNotExist(err) {
entry.Exists = true
entry.Size = info.Size()
entry.ModTime = info.ModTime()
}
return &entry
}
var allFiles map[string]bool
func createListing(filespecs []FileSpec) map[string][]*ListEntry {
allFiles = make(map[string]bool)
res := make(map[string][]*ListEntry)
for _, spec := range filespecs {
group := "__default__"
if spec.Group != "" {
group = spec.Group
}
switch spec.Type {
case "file":
entry := fileInfo(spec.Path)
if spec.Alias != "" {
entry.Alias = spec.Alias
} else {
entry.Alias = entry.Path
}
res[group] = append(res[group], entry)
allFiles[entry.Path] = true
case "glob":
matches, _ := filepath.Glob(spec.Path)
for _, match := range matches {
entry := fileInfo(match)
if spec.Alias != "" {
entry.Alias = path.Join(spec.Alias, path.Base(entry.Path))
} else {
cwd, _ := os.Getwd()
rel, _ := filepath.Rel(cwd, entry.Path)
entry.Alias = rel
}
res[group] = append(res[group], entry)
allFiles[entry.Path] = true
}
}
}
return res
}
func fileAllowed(path string) bool {
_, ok := allFiles[path]
return ok
}