-
Notifications
You must be signed in to change notification settings - Fork 390
/
wizard.go
205 lines (170 loc) · 4.78 KB
/
wizard.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
195
196
197
198
199
200
201
202
203
204
205
// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information
package wizard
import (
"bytes"
"fmt"
"net"
"os"
"strconv"
"strings"
"github.com/spf13/cobra"
"github.com/zeebo/errs"
"golang.org/x/term"
"storj.io/common/storj"
)
// PromptForAccessName handles user input for access name to be used with wizards.
func PromptForAccessName() (string, error) {
_, err := fmt.Printf("Choose an access name (use lowercase letters) [\"default\"]: ")
if err != nil {
return "", err
}
var accessName string
n, err := fmt.Scanln(&accessName)
if err != nil && n != 0 {
return "", err
}
if accessName == "" {
return "default", nil
}
if accessName != strings.ToLower(accessName) {
return "", errs.New("Please only use lowercase letters for access name.")
}
return accessName, nil
}
// PromptForSatellite handles user input for a satellite address to be used with wizards.
func PromptForSatellite(cmd *cobra.Command) (string, error) {
satellites := []string{
"12EayRS2V1kEsWESU9QMRseFhdxYxKicsiFmxrsLZHeLUtdps3S@us1.storj.io:7777",
"12L9ZFwhzVpuEKMUNUqkaTLGzwY9G24tbiigLiXpmZWKwmcNDDs@eu1.storj.io:7777",
"121RTSDpyNZVcEU84Ticf2L1ntiuUimbWgfATz21tuvgk3vzoA6@ap1.storj.io:7777",
}
_, err := fmt.Print("Select your satellite:\n")
if err != nil {
return "", err
}
for iterator, value := range satellites {
nodeURL, err := storj.ParseNodeURL(value)
if err != nil {
return "", err
}
host, _, err := net.SplitHostPort(nodeURL.Address)
if err != nil {
return "", err
}
_, err = fmt.Printf("\t[%d] %s\n", iterator+1, host)
if err != nil {
return "", nil //nolint: nilerr // we'll skip the prompt, when there's an error
}
}
_, err = fmt.Print(`Enter number or satellite address as "<nodeid>@<address>:<port>" [1]: `)
if err != nil {
return "", err
}
var satelliteAddress string
n, err := fmt.Scanln(&satelliteAddress)
if err != nil {
if n != 0 {
return "", err
}
// fmt.Scanln cannot handle empty input
satelliteAddress = satellites[0]
}
if len(satelliteAddress) == 0 {
return "", errs.New("satellite address cannot be empty")
}
if len(satelliteAddress) == 1 {
satIdx, err := strconv.Atoi(satelliteAddress)
if err != nil {
return "", errs.New("invalid satellite address option")
}
if satIdx < 1 || satIdx > len(satellites) {
return "", errs.New("invalid satellite address option")
}
satelliteAddress = satellites[satIdx-1]
}
nodeURL, err := storj.ParseNodeURL(satelliteAddress)
if err != nil {
return "", err
}
if nodeURL.ID.IsZero() {
return "", errs.New(`missing node id, satellite address must be in the format "<nodeid>@<address>:<port>"`)
}
return satelliteAddress, nil
}
// PromptForAPIKey handles user input for an API key to be used with wizards.
func PromptForAPIKey() (string, error) {
_, err := fmt.Print("Enter your API key: ")
if err != nil {
return "", err
}
var apiKey string
n, err := fmt.Scanln(&apiKey)
if err != nil && n != 0 {
return "", err
}
if apiKey == "" {
return "", errs.New("API key cannot be empty")
}
return apiKey, nil
}
// PromptForEncryptionPassphrase handles user input for an encryption passphrase to be used with wizards.
func PromptForEncryptionPassphrase() (string, error) {
_, err := fmt.Print(`Data is encrypted on the network, with an encryption passphrase
stored on your local machine. Enter a passphrase you'd like to use.
Enter your encryption passphrase: `)
if err != nil {
return "", err
}
encKey, err := term.ReadPassword(int(os.Stdin.Fd()))
if err != nil {
return "", err
}
_, err = fmt.Println()
if err != nil {
return "", err
}
if len(encKey) == 0 {
return "", errs.New("Encryption passphrase cannot be empty")
}
_, err = fmt.Print("Enter your encryption passphrase again: ")
if err != nil {
return "", err
}
repeatedEncKey, err := term.ReadPassword(int(os.Stdin.Fd()))
if err != nil {
return "", err
}
_, err = fmt.Println()
if err != nil {
return "", err
}
if !bytes.Equal(encKey, repeatedEncKey) {
return "", errs.New("encryption passphrase does not match")
}
return string(encKey), nil
}
// PromptForTracing handles user input for consent to turn on tracing to be used with wizards.
func PromptForTracing() (bool, error) {
_, err := fmt.Printf(`
With your permission, Storj can automatically collect analytics information from your uplink CLI to help improve the quality and performance of our products. This information is sent only with your consent and is submitted anonymously to Storj Labs: (y/n)
`)
if err != nil {
return false, err
}
var userConsent string
n, err := fmt.Scanln(&userConsent)
if err != nil {
if n != 0 {
return false, err
}
// fmt.Scanln cannot handle empty input
userConsent = "n"
}
switch userConsent {
case "y", "yes", "Y", "Yes":
return true, nil
default:
return false, nil
}
}