-
Notifications
You must be signed in to change notification settings - Fork 1
/
directory.go
44 lines (35 loc) · 884 Bytes
/
directory.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
package base
import (
"fmt"
"os"
"github.com/evg4b/uncors/internal/helpers"
"github.com/gobuffalo/validate"
"github.com/spf13/afero"
)
type DirectoryValidator struct {
Field string
Value string
Fs afero.Fs
}
func (f *DirectoryValidator) IsValid(errors *validate.Errors) {
if f.Value == "" {
errors.Add(f.Field, fmt.Sprintf("%s must not be empty", f.Field))
return
}
helpers.PassedOrOsFs(&f.Fs)
stat, err := f.Fs.Stat(f.Value)
if err != nil {
switch {
case os.IsNotExist(err):
errors.Add(f.Field, fmt.Sprintf("%s directory does not exist", f.Field))
case os.IsPermission(err):
errors.Add(f.Field, fmt.Sprintf("%s directory is not accessible", f.Field))
default:
errors.Add(f.Field, fmt.Sprintf("%s is not a directory", f.Field))
}
return
}
if !stat.IsDir() {
errors.Add(f.Field, fmt.Sprintf("%s is not a directory", f.Field))
}
}