-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeys.go
48 lines (45 loc) · 1007 Bytes
/
keys.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
package searchparty
import (
"fmt"
"os"
"path"
"strings"
"github.com/denysvitali/searchparty-go/model"
)
func LoadKeys(dir string, key []byte) ([]model.MainKey, error) {
keys := make([]model.MainKey, 0)
files, err := os.ReadDir(dir)
if err != nil {
return nil, err
}
for _, v := range files {
if v.IsDir() {
continue
}
var toAddKey model.MainKey
switch {
case strings.HasSuffix(v.Name(), ".keys"):
f, err := os.Open(path.Join(dir, v.Name()))
if err != nil {
return nil, fmt.Errorf("failed to open file %s: %w", v.Name(), err)
}
toAddKey, err = LoadStaticKey(f)
if err != nil {
return nil, err
}
case strings.HasSuffix(v.Name(), ".record"):
f, err := os.Open(path.Join(dir, v.Name()))
if err != nil {
return nil, fmt.Errorf("failed to open file %s: %w", v.Name(), err)
}
toAddKey, err = LoadDynamicKey(f, key)
if err != nil {
return nil, err
}
default:
continue
}
keys = append(keys, toAddKey)
}
return keys, nil
}