-
Notifications
You must be signed in to change notification settings - Fork 4
/
file.go
52 lines (43 loc) · 1.07 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
52
package flagvar
import (
"os"
"strings"
)
// File is a `flag.Value` for file path arguments.
// By default, any errors from os.Stat are returned.
// Alternatively, the value of the `Validate` field is used as a validator when specified.
type File struct {
Validate func(os.FileInfo, error) error
Value string
}
// Set is flag.Value.Set
func (fv *File) Set(v string) error {
info, err := os.Stat(v)
fv.Value = v
if fv.Validate != nil {
return fv.Validate(info, err)
}
return err
}
func (fv *File) String() string {
return fv.Value
}
// Files is a `flag.Value` for file path arguments.
// By default, any errors from os.Stat are returned.
// Alternatively, the value of the `Validate` field is used as a validator when specified.
type Files struct {
Validate func(os.FileInfo, error) error
Values []string
}
// Set is flag.Value.Set
func (fv *Files) Set(v string) error {
info, err := os.Stat(v)
fv.Values = append(fv.Values, v)
if fv.Validate != nil {
return fv.Validate(info, err)
}
return err
}
func (fv *Files) String() string {
return strings.Join(fv.Values, ",")
}