/
config.go
255 lines (210 loc) · 6.18 KB
/
config.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
package main
import (
"fmt"
"os"
"os/user"
"strconv"
"strings"
"github.com/reiki4040/cstore"
"github.com/reiki4040/peco"
)
const (
HOST_TYPE_PUBLIC_IP = "public"
HOST_TYPE_PRIVATE_IP = "private"
HOST_TYPE_NAME_TAG = "name"
)
type Config struct {
Default RnsshConfig
}
func (c *Config) Validate() error {
return c.Default.Validate()
}
type RnsshConfig struct {
Name string `toml:"profile_name,omitempty"`
AWSRegion string `toml:"aws_region,omitempty"`
HostType string `toml:"host_type,omitempty"`
SshUser string `toml:"ssh_user,omitempty"`
SshIdentityFile string `toml:"ssh_identitiy_file,omitempty"`
SshPort int `toml:"ssh_port,omitzero"`
SshStrictHostKeyCheckingNo int `toml:"ssh_strict_host_key_checking_no,omitzero"`
UseSshConfig bool `toml:"use_ssh_config"`
//AWSKey string `toml:"aws_access_key_id"`
//AWSSecret string `toml:"aws_secret_access_key"`
}
func (c *RnsshConfig) Validate() error {
if err := HostTypeCheck(c.HostType); err != nil {
return err
}
if err := IdentityFileCheck(c.SshIdentityFile); err != nil {
return err
}
if err := StrictHostKeyCheckingNoCheck(c.SshStrictHostKeyCheckingNo); err != nil {
return err
}
return nil
}
func HostTypeCheck(t string) error {
switch t {
case HOST_TYPE_PUBLIC_IP:
fallthrough
case HOST_TYPE_PRIVATE_IP:
fallthrough
case HOST_TYPE_NAME_TAG:
fallthrough
case "":
return nil
default:
return fmt.Errorf("invalid HostType value: %s. allow public, private, name or \"\"(default)", t)
}
}
func StrictHostKeyCheckingNoCheck(v int) error {
switch v {
case 1:
fallthrough
case 0:
fallthrough
case -1:
return nil
default:
return fmt.Errorf("invalid StrictHostKeyCheckingNo value: %d. allow 0(off), 1(on), -1(not specify)", v)
}
}
func IdentityFileCheck(path string) error {
if path != "" {
// replace ~ -> home dir
if i := strings.Index(path, "~"); i == 0 {
user, err := user.Current()
if err != nil {
return fmt.Errorf("can not resolved home dir: %s", err.Error())
}
path = user.HomeDir + string(os.PathSeparator) + path[1:]
}
if _, err := os.Stat(path); os.IsNotExist(err) {
return fmt.Errorf("not exists: %s", path)
}
}
return nil
}
func DoConfigWizard(cs *cstore.CStore) error {
chosenResourceType, err := peco.Choose("rnssh ResourceType option", "Please select resource type", "", ResourceTypeList)
if err != nil {
return fmt.Errorf("ResourceType choose error:%s", err.Error())
}
resourceType := "ec2"
// later win
for _, c := range chosenResourceType {
resourceType = c.Value()
}
var c *Config
var region, hostType string
var useSshConfig bool
var strictHostKeyChecking int
switch resourceType {
case "ec2":
region, hostType, err = Ec2ConfigWizard()
if err != nil {
return err
}
strictHostKeyChecking, err = StrictHostKeyCheckingWizard()
if err != nil {
return err
}
useSshConfig = false
case "ssh_config":
strictHostKeyChecking, err = StrictHostKeyCheckingWizard()
if err != nil {
return err
}
chosenContinue, err := peco.Choose("next setting", "next, continue to AWS settings?", "", ContinueList)
if err != nil {
return err
}
chosen := ""
for _, c := range chosenContinue {
chosen = c.Value()
}
if chosen == "yes" {
region, hostType, err = Ec2ConfigWizard()
if err != nil {
return err
}
}
useSshConfig = true
}
c = &Config{
Default: RnsshConfig{
AWSRegion: region,
HostType: hostType,
UseSshConfig: useSshConfig,
SshStrictHostKeyCheckingNo: strictHostKeyChecking,
},
}
if err := cs.Save(c); err != nil {
return err
}
return nil
}
func Ec2ConfigWizard() (string, string, error) {
chosenRegion, err := peco.Choose("AWS region", "Please select default AWS region", "", AWSRegionList)
if err != nil {
return "", "", fmt.Errorf("region choose error:%s", err.Error())
}
region := ""
for _, c := range chosenRegion {
region = c.Value()
}
chosenHostType, err := peco.Choose("rnssh host type", "Please select default host type", "", HostTypeList)
if err != nil {
return "", "", fmt.Errorf("host type choose error:%s", err.Error())
}
hostType := ""
for _, c := range chosenHostType {
hostType = c.Value()
}
return region, hostType, nil
}
func StrictHostKeyCheckingWizard() (int, error) {
chosenStrict, err := peco.Choose("rnssh StrictHostKeyChecking option", "Please select about StrictHostKeyChecking (recommend to Not specify)", "", StrictHostKeyCheckingList)
if err != nil {
return -1, fmt.Errorf("StrictHostKeyChecking choose error:%s", err.Error())
}
var strict int
for _, c := range chosenStrict {
strict, err = strconv.Atoi(c.Value())
if err != nil {
// error then disabled
strict = 0
}
}
return strict, nil
}
var (
AWSRegionList = []peco.Choosable{
&peco.Choice{C: "ap-northeast-1 (Tokyo)", V: "ap-northeast-1"},
&peco.Choice{C: "ap-southeast-1 (Singapore)", V: "ap-southeast-1"},
&peco.Choice{C: "ap-southeast-2 (Sydney)", V: "ap-southeast-2"},
&peco.Choice{C: "eu-central-1 (Frankfurt)", V: "eu-central-1"},
&peco.Choice{C: "eu-west-1 (Ireland)", V: "eu-west-1"},
&peco.Choice{C: "sa-east-1 (Sao Paulo)", V: "sa-east-1"},
&peco.Choice{C: "us-east-1 (N. Virginia)", V: "us-east-1"},
&peco.Choice{C: "us-west-1 (N. California)", V: "us-west-1"},
&peco.Choice{C: "us-west-2 (Oregon)", V: "us-west-2"},
}
HostTypeList = []peco.Choosable{
&peco.Choice{C: "PublicIP (rnssh default)", V: "public"},
&peco.Choice{C: "PrivateIP (for VPN or bastion)", V: "private"},
&peco.Choice{C: "Name Tag (need ssh config settings)", V: "name"},
}
StrictHostKeyCheckingList = []peco.Choosable{
&peco.Choice{C: "Not specify (rnssh Default)", V: "0"},
&peco.Choice{C: "StrictHostKeyChecking=NO (if you don't know this ssh option, then deprecated)", V: "1"},
}
ResourceTypeList = []peco.Choosable{
&peco.Choice{C: "AWS EC2 (rnssh Default)", V: "ec2"},
&peco.Choice{C: "ssh config (load from ~/.ssh/config)", V: "ssh_config"},
}
ContinueList = []peco.Choosable{
&peco.Choice{C: "No.", V: "no"},
&peco.Choice{C: "Yes, continue to AWS settings", V: "yes"},
}
)