/
utils.go
81 lines (70 loc) · 1.56 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
package blsgen
import (
"fmt"
"os"
"strings"
bls_core "github.com/harmony-one/bls/ffi/go/bls"
)
func loadBasicKeyWithProvider(blsKeyFile string, pp passProvider) (*bls_core.SecretKey, error) {
pass, err := pp.getPassphrase(blsKeyFile)
if err != nil {
return nil, err
}
secretKey, err := LoadBLSKeyWithPassPhrase(blsKeyFile, pass)
if err != nil {
return nil, err
}
return secretKey, nil
}
func checkIsFile(path string) error {
info, err := os.Stat(path)
if err != nil {
return err
}
if info.IsDir() {
return fmt.Errorf("%v is directory", path)
}
return nil
}
func checkIsDir(path string) error {
info, err := os.Stat(path)
if err != nil {
return err
}
if !info.IsDir() {
return fmt.Errorf("%v is a file", path)
}
return nil
}
func keyFileToPassFileFull(keyFile string) string {
return strings.TrimSuffix(keyFile, basicKeyExt) + passExt
}
func promptGetPassword(prompt string) (string, error) {
if !strings.HasSuffix(prompt, ":") {
prompt += ":"
}
console.print(prompt)
return console.readPassword()
}
const yesNoPrompt = "[y/n]: "
func promptYesNo(prompt string) (bool, error) {
if !strings.HasSuffix(prompt, yesNoPrompt) {
prompt = prompt + yesNoPrompt
}
for {
console.print(prompt)
response, err := console.readln()
if err != nil {
return false, err
}
response = strings.TrimSpace(strings.ToLower(response))
if response == "y" || response == "yes" {
return true, nil
} else if response == "n" || response == "no" {
return false, nil
}
}
}
func stringIsSet(val *string) bool {
return val != nil && *val != ""
}