-
Notifications
You must be signed in to change notification settings - Fork 51
/
list.go
86 lines (77 loc) · 2.45 KB
/
list.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
75
76
77
78
79
80
81
82
83
84
85
86
package request
import (
"path/filepath"
"sort"
"strings"
"github.com/browserpass/browserpass-native/errors"
"github.com/browserpass/browserpass-native/response"
"github.com/mattn/go-zglob"
log "github.com/sirupsen/logrus"
)
func listFiles(request *request) {
responseData := response.MakeListResponse()
for _, store := range request.Settings.Stores {
normalizedStorePath, err := normalizePasswordStorePath(store.Path)
if err != nil {
log.Errorf(
"The password store '%+v' is not accessible at its location: %+v",
store, err,
)
response.SendErrorAndExit(
errors.CodeInaccessiblePasswordStore,
&map[errors.Field]string{
errors.FieldMessage: "The password store is not accessible",
errors.FieldAction: "list",
errors.FieldError: err.Error(),
errors.FieldStoreID: store.ID,
errors.FieldStoreName: store.Name,
errors.FieldStorePath: store.Path,
},
)
}
store.Path = normalizedStorePath
files, err := zglob.GlobFollowSymlinks(filepath.Join(store.Path, "/**/*.gpg"))
if err != nil {
log.Errorf(
"Unable to list the files in the password store '%+v' at its location: %+v",
store, err,
)
response.SendErrorAndExit(
errors.CodeUnableToListFilesInPasswordStore,
&map[errors.Field]string{
errors.FieldMessage: "Unable to list the files in the password store",
errors.FieldAction: "list",
errors.FieldError: err.Error(),
errors.FieldStoreID: store.ID,
errors.FieldStoreName: store.Name,
errors.FieldStorePath: store.Path,
},
)
}
for i, file := range files {
relativePath, err := filepath.Rel(store.Path, file)
if err != nil {
log.Errorf(
"Unable to determine the relative path for a file '%v' in the password store '%+v': %+v",
file, store, err,
)
response.SendErrorAndExit(
errors.CodeUnableToDetermineRelativeFilePathInPasswordStore,
&map[errors.Field]string{
errors.FieldMessage: "Unable to determine the relative path for a file in the password store",
errors.FieldAction: "list",
errors.FieldError: err.Error(),
errors.FieldFile: file,
errors.FieldStoreID: store.ID,
errors.FieldStoreName: store.Name,
errors.FieldStorePath: store.Path,
},
)
}
files[i] = strings.Replace(relativePath, "\\", "/", -1) // normalize Windows paths
}
sort.Strings(files)
responseData.Files[store.ID] = files
}
response.SendOk(responseData)
}