-
Notifications
You must be signed in to change notification settings - Fork 4.4k
/
flag_upstreams.go
72 lines (59 loc) · 1.58 KB
/
flag_upstreams.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 proxy
import (
"fmt"
"strconv"
"strings"
"github.com/hashicorp/consul/api"
"github.com/hashicorp/consul/connect/proxy"
)
// FlagUpstreams implements the flag.Value interface and allows specifying
// the -upstream flag multiple times and keeping track of the name of the
// upstream and the local port.
//
// The syntax of the value is "name:addr" where addr can be "port" or
// "host:port". Examples: "db:8181", "db:127.0.0.10:8282", etc.
type FlagUpstreams map[string]proxy.UpstreamConfig
func (f *FlagUpstreams) String() string {
return fmt.Sprintf("%v", *f)
}
func (f *FlagUpstreams) Set(value string) error {
idx := strings.Index(value, ":")
if idx == -1 {
return fmt.Errorf("Upstream value should be name:addr in %q", value)
}
addr := ""
name := value[:idx]
portRaw := value[idx+1:]
if idx := strings.Index(portRaw, ":"); idx != -1 {
addr = portRaw[:idx]
portRaw = portRaw[idx+1:]
}
destinationType := "service"
if idx := strings.Index(name, "."); idx != -1 {
typ := name[idx+1:]
name = name[:idx]
switch typ {
case "", "service":
destinationType = "service"
case "query":
destinationType = "prepared_query"
default:
return fmt.Errorf(
"Upstream type must be blank, 'service', or 'query'. Got: %q", typ)
}
}
port, err := strconv.ParseInt(portRaw, 0, 0)
if err != nil {
return err
}
if *f == nil {
*f = make(map[string]proxy.UpstreamConfig)
}
(*f)[name] = proxy.UpstreamConfig{
LocalBindAddress: addr,
LocalBindPort: int(port),
DestinationName: name,
DestinationType: api.UpstreamDestType(destinationType),
}
return nil
}