-
Notifications
You must be signed in to change notification settings - Fork 2k
/
plugin_reattach_config.go
72 lines (62 loc) · 1.56 KB
/
plugin_reattach_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
package structs
import (
"fmt"
"net"
plugin "github.com/hashicorp/go-plugin"
)
// ReattachConfig is a wrapper around plugin.ReattachConfig to better support
// serialization
type ReattachConfig struct {
Protocol string
Network string
Addr string
Pid int
}
// ReattachConfigToGoPlugin converts a ReattachConfig wrapper struct into a go
// plugin ReattachConfig struct
func ReattachConfigToGoPlugin(rc *ReattachConfig) (*plugin.ReattachConfig, error) {
if rc == nil {
return nil, fmt.Errorf("nil ReattachConfig cannot be converted")
}
plug := &plugin.ReattachConfig{
Protocol: plugin.Protocol(rc.Protocol),
Pid: rc.Pid,
}
switch rc.Network {
case "tcp", "tcp4", "tcp6":
addr, err := net.ResolveTCPAddr(rc.Network, rc.Addr)
if err != nil {
return nil, err
}
plug.Addr = addr
case "udp", "udp4", "udp6":
addr, err := net.ResolveUDPAddr(rc.Network, rc.Addr)
if err != nil {
return nil, err
}
plug.Addr = addr
case "unix", "unixgram", "unixpacket":
addr, err := net.ResolveUnixAddr(rc.Network, rc.Addr)
if err != nil {
return nil, err
}
plug.Addr = addr
default:
return nil, fmt.Errorf("unknown network: %s", rc.Network)
}
return plug, nil
}
// ReattachConfigFromGoPlugin converts a go plugin ReattachConfig into a
// ReattachConfig wrapper struct
func ReattachConfigFromGoPlugin(plug *plugin.ReattachConfig) *ReattachConfig {
if plug == nil {
return nil
}
rc := &ReattachConfig{
Protocol: string(plug.Protocol),
Network: plug.Addr.Network(),
Addr: plug.Addr.String(),
Pid: plug.Pid,
}
return rc
}