github.com/lmb/consul@v1.4.1/command/connect/proxy/flag_upstreams.go (about) 1 package proxy 2 3 import ( 4 "fmt" 5 "strconv" 6 "strings" 7 8 "github.com/hashicorp/consul/api" 9 "github.com/hashicorp/consul/connect/proxy" 10 ) 11 12 // FlagUpstreams implements the flag.Value interface and allows specifying 13 // the -upstream flag multiple times and keeping track of the name of the 14 // upstream and the local port. 15 // 16 // The syntax of the value is "name:addr" where addr can be "port" or 17 // "host:port". Examples: "db:8181", "db:127.0.0.10:8282", etc. 18 type FlagUpstreams map[string]proxy.UpstreamConfig 19 20 func (f *FlagUpstreams) String() string { 21 return fmt.Sprintf("%v", *f) 22 } 23 24 func (f *FlagUpstreams) Set(value string) error { 25 idx := strings.Index(value, ":") 26 if idx == -1 { 27 return fmt.Errorf("Upstream value should be name:addr in %q", value) 28 } 29 30 addr := "" 31 name := value[:idx] 32 portRaw := value[idx+1:] 33 if idx := strings.Index(portRaw, ":"); idx != -1 { 34 addr = portRaw[:idx] 35 portRaw = portRaw[idx+1:] 36 } 37 38 destinationType := "service" 39 if idx := strings.Index(name, "."); idx != -1 { 40 typ := name[idx+1:] 41 name = name[:idx] 42 switch typ { 43 case "", "service": 44 destinationType = "service" 45 46 case "query": 47 destinationType = "prepared_query" 48 49 default: 50 return fmt.Errorf( 51 "Upstream type must be blank, 'service', or 'query'. Got: %q", typ) 52 } 53 } 54 55 port, err := strconv.ParseInt(portRaw, 0, 0) 56 if err != nil { 57 return err 58 } 59 60 if *f == nil { 61 *f = make(map[string]proxy.UpstreamConfig) 62 } 63 64 (*f)[name] = proxy.UpstreamConfig{ 65 LocalBindAddress: addr, 66 LocalBindPort: int(port), 67 DestinationName: name, 68 DestinationType: api.UpstreamDestType(destinationType), 69 } 70 71 return nil 72 }