-
Notifications
You must be signed in to change notification settings - Fork 110
/
utils.go
194 lines (164 loc) · 5.12 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// Copyright 2019 ChainSafe Systems (ON) Corp.
// This file is part of gossamer.
//
// The gossamer library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The gossamer library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the gossamer library. If not, see <http://www.gnu.org/licenses/>.
package utils
import (
"fmt"
"io/ioutil"
"os"
"os/user"
"path"
"path/filepath"
"runtime"
"strings"
)
// PathExists returns true if the named file or directory exists, otherwise false
func PathExists(p string) bool {
if _, err := os.Stat(p); err != nil {
if os.IsNotExist(err) {
return false
}
}
return true
}
// HomeDir returns the user's current HOME directory
func HomeDir() string {
if home := os.Getenv("HOME"); home != "" {
return home
}
if usr, err := user.Current(); err == nil {
return usr.HomeDir
}
return ""
}
// ExpandDir expands a tilde prefix path to a full home path
func ExpandDir(targetPath string) string {
if strings.HasPrefix(targetPath, "~\\") || strings.HasPrefix(targetPath, "~/") {
if homeDir := HomeDir(); homeDir != "" {
targetPath = homeDir + targetPath[1:]
}
} else if strings.HasPrefix(targetPath, ".\\") || strings.HasPrefix(targetPath, "./") {
targetPath, _ = filepath.Abs(targetPath)
}
return path.Clean(os.ExpandEnv(targetPath))
}
// BasePath attempts to create a data directory using the given name within the
// gossamer directory within the user's HOME directory, returns absolute path
// or, if unable to locate HOME directory, returns within current directory
func BasePath(name string) string {
home := HomeDir()
if home != "" {
if runtime.GOOS == "darwin" {
return filepath.Join(home, "Library", "Gossamer", name)
} else if runtime.GOOS == "windows" {
return filepath.Join(home, "AppData", "Roaming", "Gossamer", name)
} else {
return filepath.Join(home, ".gossamer", name)
}
}
return name
}
// KeystoreDir returns the absolute filepath of the keystore directory
func KeystoreDir(basepath string) (keystorepath string, err error) {
// basepath specified, set keystore filepath to absolute path of [basepath]/keystore
if basepath != "" {
basepath = ExpandDir(basepath)
keystorepath, err = filepath.Abs(basepath + "/keystore")
if err != nil {
return "", fmt.Errorf("failed to create absolute filepath: %s", err)
}
}
// if basepath does not exist, create it
if _, err = os.Stat(keystorepath); os.IsNotExist(err) {
err = os.Mkdir(keystorepath, os.ModePerm)
if err != nil {
return "", fmt.Errorf("failed to create data directory: %s", err)
}
}
// if basepath/keystore does not exist, create it
if _, err = os.Stat(keystorepath); os.IsNotExist(err) {
err = os.Mkdir(keystorepath, os.ModePerm)
if err != nil {
return "", fmt.Errorf("failed to create keystore directory: %s", err)
}
}
return keystorepath, nil
}
// KeystoreFiles returns the filenames of all the keys in the basepath's keystore
func KeystoreFiles(basepath string) ([]string, error) {
keystorepath, err := KeystoreDir(basepath)
if err != nil {
return nil, fmt.Errorf("failed to get keystore directory: %s", err)
}
files, err := ioutil.ReadDir(keystorepath)
if err != nil {
return nil, fmt.Errorf("failed to read keystore directory: %s", err)
}
keys := []string{}
for _, f := range files {
ext := filepath.Ext(f.Name())
if ext == ".key" {
keys = append(keys, f.Name())
}
}
return keys, nil
}
// KeystoreFilepaths lists all the keys in the basepath/keystore/ directory and returns them as a list of filepaths
func KeystoreFilepaths(basepath string) ([]string, error) {
keys, err := KeystoreFiles(basepath)
if err != nil {
return nil, err
}
for i, key := range keys {
fmt.Printf("[%d] %s\n", i, key)
}
return keys, nil
}
// GetGssmrGenesisRawPath gets the gssmr raw genesis path
func GetGssmrGenesisRawPath() string {
path1 := "../chain/gssmr/genesis.json"
path2 := "../../chain/gssmr/genesis.json"
var fp string
if PathExists(path1) {
fp, _ = filepath.Abs(path1)
} else if PathExists(path2) {
fp, _ = filepath.Abs(path2)
}
return fp
}
// GetGssmrGenesisPath gets the gssmr human-readable genesis path
func GetGssmrGenesisPath() string {
path1 := "../chain/gssmr/genesis-spec.json"
path2 := "../../chain/gssmr/genesis-spec.json"
var fp string
if PathExists(path1) {
fp, _ = filepath.Abs(path1)
} else if PathExists(path2) {
fp, _ = filepath.Abs(path2)
}
return fp
}
// GetKusamaGenesisPath gets the kusama genesis path
func GetKusamaGenesisPath() string {
path1 := "../chain/kusama/genesis.json"
path2 := "../../chain/kusama/genesis.json"
var fp string
if PathExists(path1) {
fp, _ = filepath.Abs(path1)
} else if PathExists(path2) {
fp, _ = filepath.Abs(path2)
}
return fp
}