-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsingle.go
More file actions
87 lines (73 loc) · 1.61 KB
/
Copy pathsingle.go
File metadata and controls
87 lines (73 loc) · 1.61 KB
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
79
80
81
82
83
84
85
86
87
package aferox
import (
"errors"
"fmt"
"io/fs"
"github.com/spf13/afero"
"github.com/unmango/go/fopt"
)
type errSingle struct {
acc string
cur string
}
func (e errSingle) Error() string {
return fmt.Sprintf("fs contains more than one entry\n\thad:\t%s\n\tfound:\t%s", e.acc, e.cur)
}
func StatSingle(fsys afero.Fs, root string, options ...IterOption) (fs.FileInfo, error) {
opts := &iterOptions{}
fopt.ApplyAll(opts, options)
info, err := Fold(fsys, root,
func(path string, info fs.FileInfo, acc fs.FileInfo, err error) (fs.FileInfo, error) {
if err != nil {
return nil, err
}
if path == "." || path == "" {
return nil, nil
}
if info.IsDir() && opts.skipDirs {
return nil, nil
}
if acc != nil {
return nil, errSingle{acc.Name(), info.Name()}
}
return info, nil
},
nil,
)
if err != nil {
return nil, err
}
if info == nil {
return nil, errors.New("Fs contains no entries")
}
return info, nil
}
func OpenSingle(fsys afero.Fs, root string, options ...IterOption) (afero.File, error) {
opts := &iterOptions{}
fopt.ApplyAll(opts, options)
file, err := Fold(fsys, root,
func(path string, info fs.FileInfo, acc afero.File, err error) (afero.File, error) {
if err != nil {
return nil, err
}
if path == "." || path == "" {
return nil, nil
}
if info.IsDir() && opts.skipDirs {
return nil, nil
}
if acc != nil {
return nil, errSingle{acc.Name(), info.Name()}
}
return fsys.Open(path)
},
nil,
)
if err != nil {
return nil, err
}
if file == nil {
return nil, errors.New("Fs contains no entries")
}
return file, nil
}