-
Notifications
You must be signed in to change notification settings - Fork 43
/
backend_prompt.go
168 lines (148 loc) · 3.97 KB
/
backend_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
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
package util
import (
"errors"
"fmt"
"os"
"strings"
"github.com/joyent/triton-kubernetes/backend"
"github.com/joyent/triton-kubernetes/backend/local"
"github.com/joyent/triton-kubernetes/backend/manta"
"github.com/manifoldco/promptui"
homedir "github.com/mitchellh/go-homedir"
"github.com/spf13/viper"
)
func PromptForBackend() (backend.Backend, error) {
nonInteractiveMode := viper.GetBool("non-interactive")
// Ask user what backend to use
selectedBackendProvider := ""
if viper.IsSet("backend_provider") {
selectedBackendProvider = viper.GetString("backend_provider")
} else if nonInteractiveMode {
return nil, errors.New("backend_provider must be provided")
} else {
prompt := promptui.Select{
Label: "Backend to persist data",
Items: []string{"Local", "Manta"},
Templates: &promptui.SelectTemplates{
Label: "{{ . }}?",
Active: fmt.Sprintf(`%s {{ . | underline }}`, promptui.IconSelect),
Inactive: ` {{ . }}`,
Selected: fmt.Sprintf(`{{ "%s" | green }} {{ "Backend Provider:" | bold}} {{ . }}`, promptui.IconGood),
},
}
_, value, err := prompt.Run()
if err != nil {
return nil, err
}
selectedBackendProvider = strings.ToLower(value)
}
switch selectedBackendProvider {
case "local":
return local.New()
case "manta":
// Triton Account
tritonAccount := ""
if viper.IsSet("triton_account") {
tritonAccount = viper.GetString("triton_account")
} else if nonInteractiveMode {
return nil, errors.New("triton_account must be specified")
} else {
prompt := promptui.Prompt{
Label: "Triton Account Name",
Validate: func(input string) error {
if len(input) == 0 {
return errors.New("Invalid Triton Account")
}
return nil
},
}
result, err := prompt.Run()
if err != nil {
return nil, err
}
tritonAccount = result
}
// Triton Key Path
rawTritonKeyPath := ""
if viper.IsSet("triton_key_path") {
rawTritonKeyPath = viper.GetString("triton_key_path")
} else if nonInteractiveMode {
return nil, errors.New("triton_key_path must be specified")
} else {
prompt := promptui.Prompt{
Label: "Triton Key Path",
Validate: func(input string) error {
expandedPath, err := homedir.Expand(input)
if err != nil {
return err
}
_, err = os.Stat(expandedPath)
if err != nil {
if os.IsNotExist(err) {
return errors.New("File not found")
}
}
return nil
},
Default: "~/.ssh/id_rsa",
}
result, err := prompt.Run()
if err != nil {
return nil, err
}
rawTritonKeyPath = result
}
expandedTritonKeyPath, err := homedir.Expand(rawTritonKeyPath)
if err != nil {
return nil, err
}
tritonKeyPath := expandedTritonKeyPath
// Triton Key ID
tritonKeyID := ""
if viper.IsSet("triton_key_id") {
tritonKeyID = viper.GetString("triton_key_id")
} else {
keyID, err := GetPublicKeyFingerprintFromPrivateKey(tritonKeyPath)
if err != nil {
return nil, err
}
tritonKeyID = keyID
}
// Triton URL
tritonURL := ""
if viper.IsSet("triton_url") {
tritonURL = viper.GetString("triton_url")
} else if nonInteractiveMode {
return nil, errors.New("triton_url must be specified")
} else {
prompt := promptui.Prompt{
Label: "Triton URL",
Default: "https://us-east-1.api.joyent.com",
}
result, err := prompt.Run()
if err != nil {
return nil, err
}
tritonURL = result
}
// Manta URL
mantaURL := ""
if viper.IsSet("manta_url") {
mantaURL = viper.GetString("manta_url")
} else if nonInteractiveMode {
return nil, errors.New("manta_url must be specified")
} else {
prompt := promptui.Prompt{
Label: "Manta URL",
Default: "https://us-east.manta.joyent.com",
}
result, err := prompt.Run()
if err != nil {
return nil, err
}
mantaURL = result
}
return manta.New(tritonAccount, tritonKeyPath, tritonKeyID, tritonURL, mantaURL)
}
return nil, fmt.Errorf("Unsupported backend provider '%s'", selectedBackendProvider)
}