forked from streamingfast/firehose-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
82 lines (65 loc) · 1.89 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
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
package firecore
import (
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
"github.com/streamingfast/cli"
)
func mkdirStorePathIfLocal(storeURL string) (err error) {
if dirs := getDirsToMake(storeURL); len(dirs) > 0 {
err = MakeDirs(dirs)
}
return
}
func getDirsToMake(storeURL string) []string {
parts := strings.Split(storeURL, "://")
if len(parts) > 1 {
if parts[0] != "file" {
// Not a local store, nothing to do
return nil
}
storeURL = parts[1]
}
// Some of the store URL are actually a file directly, let's try our best to cope for that case
filename := filepath.Base(storeURL)
if strings.Contains(filename, ".") {
storeURL = filepath.Dir(storeURL)
}
// If we reach here, it's a local store path
return []string{storeURL}
}
func MakeDirs(directories []string) error {
for _, directory := range directories {
err := os.MkdirAll(directory, 0755)
if err != nil {
return fmt.Errorf("failed to create directory %q: %w", directory, err)
}
}
return nil
}
// MustReplaceDataDir replaces `{data-dir}` from within the `in` received argument by the
// `dataDir` argument
func MustReplaceDataDir(dataDir, in string) string {
d, err := filepath.Abs(dataDir)
if err != nil {
panic(fmt.Errorf("file path abs: %w", err))
}
in = strings.Replace(in, "{data-dir}", d, -1)
// Some legacy code still uses '{sf-data-dir}' (firehose-ethereum/firehose-near for example), so let's replace it
// also to keep it compatible even though it's not advertised anymore
in = strings.Replace(in, "{sf-data-dir}", d, -1)
return in
}
var Example = func(in string) string {
return string(cli.Example(in))
}
func ExamplePrefixed[B Block](chain *Chain[B], prefix, in string) string {
return string(cli.ExamplePrefixed(chain.BinaryName()+" "+prefix, in))
}
func MustParseUint64(s string) uint64 {
i, err := strconv.Atoi(s)
cli.NoError(err, "Unable to parse %q as uint64", s)
return uint64(i)
}