-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.go
237 lines (198 loc) · 5.31 KB
/
options.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
package main
import (
"fmt"
"os/user"
"reflect"
"slices"
"strings"
)
type DstType int
const (
DstTypeAuto DstType = iota
DstTypeID
DstTypePrivateIP
DstTypePublicIP
DstTypeIPv6
DstTypePrivateDNSName
DstTypeNameTag
)
type AddrType int
const (
AddrTypeAuto AddrType = iota
AddrTypePrivate
AddrTypePublic
AddrTypeIPv6
)
type Options struct {
DstType DstType
AddrType AddrType
Region string
Profile string
EICEID string
UseEICE bool
NoSendKeys bool
Debug bool
SSHArgs []string
CommandWithArgs []string
Destination string
Port string
Login string
IdentityFile string
DoList bool
ListColumns string
}
func parseSSHDestination(destination string) (string, string, string) {
var login, host, port string
if strings.HasPrefix(destination, "ssh://") {
login, host, port = parseSSHURL(destination)
host = stripIPv6Brackets(host)
} else {
login, host = parseLoginHost(destination)
}
return login, host, port
}
func parseSSHURL(url string) (string, string, string) {
loginhostport := strings.TrimPrefix(url, "ssh://")
login, hostport := parseLoginHost(loginhostport)
host, port := parseHostPort(hostport)
return login, host, port
}
func parseLoginHost(loginhost string) (string, string) {
atIdx := strings.LastIndex(loginhost, "@")
if atIdx != -1 {
return loginhost[:atIdx], loginhost[atIdx+1:]
}
return "", loginhost
}
func parseHostPort(hostport string) (string, string) {
colonIdx := strings.LastIndex(hostport, ":")
/* handle square brackets around IPv6 addresses */
if colonIdx != -1 && strings.LastIndex(hostport, "]") < colonIdx {
return hostport[:colonIdx], hostport[colonIdx+1:]
}
return hostport, ""
}
func stripIPv6Brackets(host string) string {
if strings.HasPrefix(host, "[") && strings.HasSuffix(host, "]") {
return host[1 : len(host)-1]
}
return host
}
func parseType[T ~int](value string, mapping map[string]T) (T, error) { //nolint: ireturn // #37
if value, ok := mapping[value]; ok {
return value, nil
}
return 0, fmt.Errorf("%w: unknown type %s", ErrArgParse, value)
}
func parseDstType(value string) (DstType, error) {
return parseType(value, map[string]DstType{
"": DstTypeAuto,
"id": DstTypeID,
"private_ip": DstTypePrivateIP,
"public_ip": DstTypePublicIP,
"ipv6": DstTypeIPv6,
"private_dns": DstTypePrivateDNSName,
"name_tag": DstTypeNameTag,
})
}
func parseAddrType(value string) (AddrType, error) {
return parseType(value, map[string]AddrType{
"": AddrTypeAuto,
"private": AddrTypePrivate,
"public": AddrTypePublic,
"ipv6": AddrTypeIPv6,
})
}
func (options *Options) populateFromParsedArgsOptions(argsOptions map[string]string) error {
var err error
for option, variablePtr := range map[string]any{
"--region": &options.Region,
"--profile": &options.Profile,
"--eice-id": &options.EICEID,
"--destination": &options.Destination,
"-i": &options.IdentityFile,
"-l": &options.Login,
"-p": &options.Port,
"--use-eice": &options.UseEICE,
"--no-send-keys": &options.NoSendKeys,
"--debug": &options.Debug,
"--destination-type": &options.DstType,
"--address-type": &options.AddrType,
"--list": &options.DoList,
"--list-columns": &options.ListColumns,
} {
/* check if argument is not present or option is already set */
value, ok := argsOptions[option]
if !ok || !reflect.ValueOf(variablePtr).Elem().IsZero() {
continue
}
switch variable := variablePtr.(type) {
case *string:
*variable = value
case *bool:
*variable = true
case *DstType:
*variable, err = parseDstType(value)
case *AddrType:
*variable, err = parseAddrType(value)
default:
panic(fmt.Sprintf("unknown option type %T", variable))
}
if err != nil {
return err
}
}
return nil
}
func validateListOptions(options map[string]string) error {
allowedListOptions := [...]string{
"--list-columns",
"--region",
"--profile",
"--list",
}
listOnlyOptions := [...]string{
"--list-columns",
}
if _, ok := options["--list"]; !ok {
for option := range options {
if slices.Contains(listOnlyOptions[:], option) {
return fmt.Errorf("%w: option %s is only allowed when using --list", ErrArgParse, listOnlyOptions[0])
}
}
return nil
}
for option := range options {
if !slices.Contains(allowedListOptions[:], option) {
return fmt.Errorf("%w: option %s is not allowed when using --list", ErrArgParse, option)
}
}
return nil
}
func NewOptions(parsedArgs ParsedArgs) (Options, error) {
login, host, port := parseSSHDestination(parsedArgs.Destination)
options := Options{
Destination: host,
Login: login,
Port: port,
CommandWithArgs: parsedArgs.CommandWithArgs,
SSHArgs: parsedArgs.SSHArgs,
}
err := validateListOptions(parsedArgs.Options)
if err != nil {
return Options{}, err
}
err = options.populateFromParsedArgsOptions(parsedArgs.Options)
if err != nil {
return Options{}, err
}
options.UseEICE = options.UseEICE || options.EICEID != ""
if options.Login == "" {
user, err := user.Current()
if err != nil {
return Options{}, err
}
options.Login = user.Username
}
return options, nil
}