-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathverify.go
74 lines (61 loc) · 1.46 KB
/
verify.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
package main
import (
"fmt"
"os"
"github.com/kelseykm/kelcryptor/colour"
"github.com/kelseykm/kelcryptor/errors"
)
func verifyFiles(files []string) ([]string, error) {
var cleanFiles, nonExistentFiles, nonRegularFiles []string
var badFilesErr *errors.BadFilesError
for _, file := range files {
fileInfo, err := os.Stat(file)
if err != nil {
nonExistentFiles = append(nonExistentFiles, file)
} else if !fileInfo.Mode().IsRegular() {
nonRegularFiles = append(nonRegularFiles, file)
} else {
cleanFiles = append(cleanFiles, file)
}
}
if len(nonExistentFiles) != 0 {
badFilesErr = &errors.BadFilesError{}
badFilesErr.AddToMesg(
fmt.Sprintf(
"%s %s",
colour.Error,
colour.Message("Some files do not exist:\n"),
))
for index, file := range nonExistentFiles {
badFilesErr.AddToMesg(
fmt.Sprintf(
" %v: %s\n",
index+1,
colour.FileName(file),
))
}
}
if len(nonRegularFiles) != 0 {
if badFilesErr == nil {
badFilesErr = &errors.BadFilesError{}
}
badFilesErr.AddToMesg(
fmt.Sprintf(
"%s %s",
colour.Error,
colour.Message("Some files are not regular files:\n"),
))
for index, file := range nonRegularFiles {
badFilesErr.AddToMesg(
fmt.Sprintf(
" %v: %s\n",
index+1,
colour.FileName(file),
))
}
}
if badFilesErr == nil {
return cleanFiles, nil
}
return cleanFiles, badFilesErr
}