-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
47 lines (41 loc) · 1.05 KB
/
utils.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
package bitcaspy
import (
"fmt"
"os"
"path/filepath"
"sort"
"strconv"
"strings"
)
// Exists returns true if the given path exists on the filesystem.
func exists(path string) bool {
if _, err := os.Stat(path); err != nil {
return false
} else {
return true
}
}
// returns the list of files in the database directory
func getDataFiles(outDir string) ([]string, error) {
if !exists(outDir) {
return nil, fmt.Errorf("Error finding the file %s", outDir)
}
files, err := filepath.Glob(fmt.Sprintf("%s/*.db", outDir))
if err != nil {
return nil, fmt.Errorf("Error getting files from the directory %v", err)
}
return files, nil
}
// Returns the list of sorted ids of the file
func getIds(files []string) ([]int, error) {
ids := make([]int, 0)
for _, file := range files {
id, err := strconv.ParseInt((strings.TrimPrefix(strings.TrimSuffix(filepath.Base(file), ".db"), "bitcaspy_")), 10, 32)
if err != nil {
return nil, fmt.Errorf("Error parsing the files path: %v", err)
}
ids = append(ids, int(id))
}
sort.Ints(ids)
return ids, nil
}