-
Notifications
You must be signed in to change notification settings - Fork 390
/
wizard.go
162 lines (137 loc) · 3.95 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
// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information
package wizard
import (
"bytes"
"fmt"
"net"
"os"
"strings"
"github.com/spf13/cobra"
"github.com/zeebo/errs"
"golang.org/x/crypto/ssh/terminal"
)
// applyDefaultHostAndPortToAddr applies the default host and/or port if either is missing in the specified address.
func applyDefaultHostAndPortToAddr(address, defaultAddress string) (string, error) {
defaultHost, defaultPort, err := net.SplitHostPort(defaultAddress)
if err != nil {
return "", err
}
addressParts := strings.Split(address, ":")
numberOfParts := len(addressParts)
if numberOfParts > 1 && len(addressParts[0]) > 0 && len(addressParts[1]) > 0 {
// address is host:port so skip applying any defaults.
return address, nil
}
// We are missing a host:port part. Figure out which part we are missing.
indexOfPortSeparator := strings.Index(address, ":")
lengthOfFirstPart := len(addressParts[0])
if indexOfPortSeparator < 0 {
if lengthOfFirstPart == 0 {
// address is blank.
return defaultAddress, nil
}
// address is host
return net.JoinHostPort(addressParts[0], defaultPort), nil
}
if indexOfPortSeparator == 0 {
// address is :1234
return net.JoinHostPort(defaultHost, addressParts[1]), nil
}
// address is host:
return net.JoinHostPort(addressParts[0], defaultPort), nil
}
// PromptForSatellite handles user input for a satellite address to be used with wizards
func PromptForSatellite(cmd *cobra.Command) (string, error) {
satellites := []string{"us-central-1.tardigrade.io", "europe-west-1.tardigrade.io", "asia-east-1.tardigrade.io"}
_, err := fmt.Print("Select your satellite:\n")
if err != nil {
return "", err
}
for iterator, value := range satellites {
_, err := fmt.Printf("\t[%d] %s\n", iterator+1, value)
if err != nil {
return "", nil
}
}
_, err = fmt.Print("Please enter numeric choice or enter satellite address manually [1]: ")
if err != nil {
return "", err
}
var satelliteAddress string
n, err := fmt.Scanln(&satelliteAddress)
if err != nil {
if n == 0 {
// fmt.Scanln cannot handle empty input
satelliteAddress = satellites[0]
} else {
return "", err
}
}
// TODO add better validation
if satelliteAddress == "" {
return "", errs.New("satellite address cannot be empty")
} else if len(satelliteAddress) == 1 {
switch satelliteAddress {
case "1":
satelliteAddress = satellites[0]
case "2":
satelliteAddress = satellites[1]
case "3":
satelliteAddress = satellites[2]
default:
return "", errs.New("Satellite address cannot be one character")
}
}
return applyDefaultHostAndPortToAddr(satelliteAddress, cmd.Flags().Lookup("satellite-addr").Value.String())
}
// 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("Enter your encryption passphrase: ")
if err != nil {
return "", err
}
encKey, err := terminal.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 := terminal.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
}