@@ -10,15 +10,15 @@ import (
10
10
11
11
var re = regexp .MustCompile (`(?P<user>.+@)?(?P<host>[[:alpha:][:digit:]\_\-\.]+)?(?P<port>:[0-9]+)?` )
12
12
13
- // App contains main settings of application .
13
+ // App contains all supported CLI arguments given by the user .
14
14
type App struct {
15
15
args []string
16
16
flag * flag.FlagSet
17
17
18
18
Command string
19
- Local HostInput
20
- Remote HostInput
21
- Server HostInput
19
+ Local AddressInputList
20
+ Remote AddressInputList
21
+ Server AddressInput
22
22
Key string
23
23
Verbose bool
24
24
Help bool
@@ -47,8 +47,8 @@ func (c *App) Parse() error {
47
47
f .BoolVar (& c .AliasDelete , "delete" , false , "delete a tunnel alias (must be used with -alias)" )
48
48
f .BoolVar (& c .AliasList , "aliases" , false , "list all aliases" )
49
49
f .StringVar (& c .Start , "start" , "" , "Start a tunnel using a given alias" )
50
- f .Var (& c .Local , "local" , "(optional) Set local endpoint address: [<host>]:<port>" )
51
- f .Var (& c .Remote , "remote" , "set remote endpoint address: [<host>]:<port>" )
50
+ f .Var (& c .Local , "local" , "(optional) Set local endpoint address: [<host>]:<port>. Multiple -local args can be provided. " )
51
+ f .Var (& c .Remote , "remote" , "(optional) Set remote endpoint address: [<host>]:<port>. Multiple -remote args can be provided. " )
52
52
f .Var (& c .Server , "server" , "set server address: [<user>@]<host>[:<port>]" )
53
53
f .StringVar (& c .Key , "key" , "" , "(optional) Set server authentication key file path" )
54
54
f .BoolVar (& c .Verbose , "v" , false , "(optional) Increase log verbosity" )
@@ -95,26 +95,24 @@ func (c App) Validate() error {
95
95
96
96
switch c .Command {
97
97
case "new-alias" :
98
- if c .Remote .String () == "" {
99
- return fmt .Errorf ("required flag is missing: -remote" )
100
- } else if c .Server .String () == "" {
98
+ if c .Server .String () == "" {
101
99
return fmt .Errorf ("required flag is missing: -server" )
102
100
}
103
101
case "start" :
104
102
if c .Server .String () == "" {
105
103
return fmt .Errorf ("required flag is missing: -server" )
106
104
}
107
-
108
105
}
106
+
109
107
return nil
110
108
}
111
109
112
110
// PrintUsage prints, to the standard output, the informational text on how to
113
111
// use the tool.
114
112
func (c * App ) PrintUsage () {
115
113
fmt .Fprintf (os .Stderr , "%s\n \n " , `usage:
116
- mole [-v] [-insecure] [-detach] [ -local [<host>]:<port>] -remote [<host>]:<port> -server [<user>@]<host>[:<port>] [-key <key_path>]
117
- mole -alias <alias_name> [-v] [ -local [<host>]:<port>] -remote [<host>]:<port> -server [<user>@]<host>[:<port>] [-key <key_path>]
114
+ mole [-v] [-insecure] [-detach] ( -local [<host>]:<port>)... ( -remote [<host>]:<port>)... -server [<user>@]<host>[:<port>] [-key <key_path>]
115
+ mole -alias <alias_name> [-v] ( -local [<host>]:<port>)... ( -remote [<host>]:<port>)... -server [<user>@]<host>[:<port>] [-key <key_path>]
118
116
mole -alias <alias_name> -delete
119
117
mole -start <alias_name>
120
118
mole -help
@@ -128,15 +126,15 @@ func (c App) String() string {
128
126
c .Local , c .Remote , c .Server , c .Key , c .Verbose , c .Help , c .Version , c .Detach )
129
127
}
130
128
131
- // HostInput holds information about a host
132
- type HostInput struct {
129
+ // AddressInput holds information about a host
130
+ type AddressInput struct {
133
131
User string
134
132
Host string
135
133
Port string
136
134
}
137
135
138
- // String returns a string representation of a HostInput
139
- func (h HostInput ) String () string {
136
+ // String returns a string representation of a AddressInput
137
+ func (h AddressInput ) String () string {
140
138
var s string
141
139
if h .User == "" {
142
140
s = h .Address ()
@@ -147,8 +145,8 @@ func (h HostInput) String() string {
147
145
return s
148
146
}
149
147
150
- // Set parses a string representation of HostInput into its proper attributes.
151
- func (h * HostInput ) Set (value string ) error {
148
+ // Set parses a string representation of AddressInput into its proper attributes.
149
+ func (h * AddressInput ) Set (value string ) error {
152
150
result := parseServerInput (value )
153
151
h .User = strings .Trim (result ["user" ], "@" )
154
152
h .Host = result ["host" ]
@@ -157,9 +155,9 @@ func (h *HostInput) Set(value string) error {
157
155
return nil
158
156
}
159
157
160
- // Address returns a string representation of HostInput to be used to perform
158
+ // Address returns a string representation of AddressInput to be used to perform
161
159
// network connections.
162
- func (h HostInput ) Address () string {
160
+ func (h AddressInput ) Address () string {
163
161
if h .Port == "" {
164
162
return fmt .Sprintf ("%s" , h .Host )
165
163
}
@@ -180,3 +178,39 @@ func parseServerInput(input string) map[string]string {
180
178
181
179
return result
182
180
}
181
+
182
+ type AddressInputList []AddressInput
183
+
184
+ func (il AddressInputList ) String () string {
185
+ ils := []string {}
186
+
187
+ for _ , i := range il {
188
+ ils = append (ils , i .String ())
189
+ }
190
+
191
+ return strings .Join (ils , "," )
192
+ }
193
+
194
+ func (il * AddressInputList ) Set (value string ) error {
195
+ i := AddressInput {}
196
+
197
+ err := i .Set (value )
198
+ if err != nil {
199
+ return err
200
+ }
201
+
202
+ * il = append (* il , i )
203
+
204
+ return nil
205
+ }
206
+
207
+ func (il AddressInputList ) List () []string {
208
+ sl := []string {}
209
+
210
+ for _ , i := range il {
211
+ sl = append (sl , i .String ())
212
+ }
213
+
214
+ return sl
215
+
216
+ }
0 commit comments