forked from cloudfoundry/bosh-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.go
51 lines (40 loc) · 1.05 KB
/
file.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
package resource
import (
gopath "path"
"path/filepath"
"strings"
)
type File struct {
Path string
DirPath string
RelativePath string
// Fingerprinting options
UseBasename bool
ExcludeMode bool
}
func NewFile(path, dirPath string) File {
sep := string(filepath.Separator)
// returns "inner" for args ("/tmp/inner", "/tmp")
relativePath := strings.TrimPrefix(strings.TrimPrefix(path, dirPath), sep)
return File{
Path: path,
DirPath: strings.TrimSuffix(dirPath, sep),
RelativePath: relativePath,
}
}
func NewFileUsesBasename(path, dirPath string) File {
file := NewFile(path, dirPath)
file.UseBasename = true
return file
}
func (f File) WithNewDir(dirPath string) File {
f.Path = gopath.Join(dirPath, f.RelativePath)
f.DirPath = dirPath
return f
}
type FileRelativePathSorting []File
func (s FileRelativePathSorting) Len() int { return len(s) }
func (s FileRelativePathSorting) Less(i, j int) bool {
return s[i].RelativePath < s[j].RelativePath
}
func (s FileRelativePathSorting) Swap(i, j int) { s[i], s[j] = s[j], s[i] }