forked from bmatcuk/go-vagrant
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommand_port_response.go
46 lines (38 loc) · 1.13 KB
/
command_port_response.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
package vagrant
import (
"strconv"
)
// ForwardedPort defines the host port that maps to a guest port.
type ForwardedPort struct {
// Port on the guest OS
Guest int
// Port on the host which forwards to the guest
Host int
}
// PortResponse is the output from the vagrant port command.
type PortResponse struct {
ErrorResponse
// ForwardedPorts is a list of ports forwarded from the host OS to the guest
// OS for the requested vagrant machine.
ForwardedPorts []ForwardedPort
}
func newPortResponse() PortResponse {
return PortResponse{ForwardedPorts: []ForwardedPort{}}
}
func (resp *PortResponse) handleOutput(target, key string, message []string) {
// Only interested in:
// * target: X, key: forwarded_port, message: [Y, Z]
if target != "" && key == "forwarded_port" && len(message) == 2 {
var guest, host int
var err error
if guest, err = strconv.Atoi(message[0]); err != nil {
return
}
if host, err = strconv.Atoi(message[1]); err != nil {
return
}
resp.ForwardedPorts = append(resp.ForwardedPorts, ForwardedPort{Guest: guest, Host: host})
} else {
resp.ErrorResponse.handleOutput(target, key, message)
}
}