-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathhelpers.go
49 lines (41 loc) · 928 Bytes
/
helpers.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
package main
import (
"encoding/binary"
"fmt"
"os"
"os/user"
"path/filepath"
"strings"
"github.com/ethereum/go-ethereum/common"
)
// File helpers.
func expandPath(p string) (string, error) {
if strings.HasPrefix(p, "~/") || strings.HasPrefix(p, "~\\") {
if home := homeDir(); home != "" {
p = home + p[1:]
}
}
return filepath.Abs(filepath.Clean(os.ExpandEnv(p)))
}
func homeDir() string {
if home := os.Getenv("HOME"); home != "" {
return home
}
if usr, err := user.Current(); err == nil {
return usr.HomeDir
}
return ""
}
// Bytes helpers.
func stringArrayFrom2DBytes(bytes2d [][]byte) []string {
stringArray := make([]string, len(bytes2d))
for i, bytes := range bytes2d {
stringArray[i] = fmt.Sprintf("%#x", bytes)
}
return stringArray
}
func uint64To256BytesBigEndian(i uint64) []byte {
buf := make([]byte, 8)
binary.BigEndian.PutUint64(buf, i)
return common.LeftPadBytes(buf, 32)
}