forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ssh_port_forwarding.go
45 lines (38 loc) · 1.17 KB
/
ssh_port_forwarding.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
package flag
import (
"fmt"
"regexp"
"strings"
flags "github.com/jessevdk/go-flags"
)
const DefaultLocalAddress = "localhost"
type SSHPortForwarding struct {
LocalAddress string
RemoteAddress string
}
func (s *SSHPortForwarding) UnmarshalFlag(val string) error {
splitHosts := strings.Split(val, ":")
for _, piece := range splitHosts {
if len(piece) == 0 {
return &flags.Error{
Type: flags.ErrRequired,
Message: fmt.Sprintf("Bad local forwarding specification '%s'", val),
}
}
}
re := regexp.MustCompile(`^\d+$`)
switch {
case len(splitHosts) == 3 && re.MatchString(splitHosts[0]) && re.MatchString(splitHosts[2]):
s.LocalAddress = fmt.Sprintf("%s:%s", DefaultLocalAddress, splitHosts[0])
s.RemoteAddress = fmt.Sprintf("%s:%s", splitHosts[1], splitHosts[2])
case len(splitHosts) == 4 && re.MatchString(splitHosts[1]) && re.MatchString(splitHosts[3]):
s.LocalAddress = fmt.Sprintf("%s:%s", splitHosts[0], splitHosts[1])
s.RemoteAddress = fmt.Sprintf("%s:%s", splitHosts[2], splitHosts[3])
default:
return &flags.Error{
Type: flags.ErrRequired,
Message: fmt.Sprintf("Bad local forwarding specification '%s'", val),
}
}
return nil
}