-
Notifications
You must be signed in to change notification settings - Fork 0
/
prompt.go
91 lines (73 loc) · 2.09 KB
/
prompt.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
package setup
import (
"errors"
"log"
"os"
"strconv"
"github.com/manifoldco/promptui"
crypto "github.com/proximax-storage/go-xpx-crypto"
)
func PromptDir() string {
return promptBase("Enter base installation directory", DefaultInstallationDirectory, "Invalid base installation directory", func(path string) error {
_, err := os.Stat(path)
return err
})
}
func PromptRestUrl() string {
return promptBase("Enter REST server URL", DefaultRestUrl, "Invalid REST server URL", func(url string) error {
return nil
})
}
func PromptKey(label string) string {
return promptBase(label, "", "Invalid key", func(input string) error {
_, err := crypto.NewPrivateKeyfromHexString(input)
return err
})
}
func PromptDbrbPort(label string, configUpdater *ConfigUpdater) string {
return promptBase(label, "7903", "Invalid port", func(input string) error {
if input == configUpdater.SubscriberPort {
return errors.New("port value is already set as subscriber port in config-messaging.properties")
}
if input == configUpdater.Port {
return errors.New("port value is already set as P2P port in config-node.properties")
}
if input == configUpdater.ApiPort {
return errors.New("port value is already set as API port in config-node.properties")
}
port, err := strconv.Atoi(input)
if err != nil {
return err
}
if port < 1024 || port > 65535 {
return errors.New("port is not in recommended range 1024-65535")
}
return nil
})
}
func PromptConfirmation(label string) bool {
result := promptBase(label+" (y/n)", "", "", func(input string) error {
if input != "y" && input != "n" {
return errors.New("y/n expected")
}
return nil
})
return result == "y"
}
func PromptInfo(label string) {
promptBase(label, "", "", func(input string) error {
return nil
})
}
func promptBase(label string, defaultValue string, errorMessage string, validate promptui.ValidateFunc) string {
prompt := promptui.Prompt{
Label: label,
Validate: validate,
Default: defaultValue,
}
result, err := prompt.Run()
if err != nil {
log.Fatal(errorMessage, ": ", err)
}
return result
}