github.com/willyham/dosa@v2.3.1-0.20171024181418-1e446d37ee71+incompatible/cmd/dosa/options.go (about) 1 // Copyright (c) 2017 Uber Technologies, Inc. 2 // 3 // Permission is hereby granted, free of charge, to any person obtaining a copy 4 // of this software and associated documentation files (the "Software"), to deal 5 // in the Software without restriction, including without limitation the rights 6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 // copies of the Software, and to permit persons to whom the Software is 8 // furnished to do so, subject to the following conditions: 9 // 10 // The above copyright notice and this permission notice shall be included in 11 // all copies or substantial portions of the Software. 12 // 13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 // THE SOFTWARE. 20 21 package main 22 23 import ( 24 "fmt" 25 "os" 26 "regexp" 27 "strconv" 28 "strings" 29 "time" 30 31 "github.com/uber-go/dosa" 32 ) 33 34 const ( 35 _defServiceName = "dosa-dev-gateway" 36 _prodServiceName = "dosa-gateway" 37 _prodScope = "production" 38 ) 39 40 var validNameRegex = regexp.MustCompile("^[a-z]+([a-z0-9]|[^-]-)*[^-]$") 41 42 type callerFlag string 43 44 func (s *callerFlag) setString(value string) { 45 *s = callerFlag(strings.Replace(value, ".", "-", -1)) 46 } 47 48 // String implements the stringer interface 49 func (s *callerFlag) String() string { 50 return string(*s) 51 } 52 53 func (s *callerFlag) UnmarshalFlag(value string) error { 54 if value == "" || value == "dosacli-$USER" { 55 value = fmt.Sprintf("dosacli-%s", os.Getenv("USER")) 56 } 57 s.setString(value) 58 return nil 59 } 60 61 type timeFlag time.Duration 62 63 func (t *timeFlag) setDuration(d time.Duration) { 64 *t = timeFlag(d) 65 } 66 67 // Duration returns the flag value as a time.Duration 68 func (t timeFlag) Duration() time.Duration { 69 return time.Duration(t) 70 } 71 72 // UmarshalFlag satisfies the flag interface 73 func (t *timeFlag) UnmarshalFlag(value string) error { 74 valueInt, err := strconv.Atoi(value) 75 if err == nil { 76 // We received a number without a unit, assume milliseconds. 77 t.setDuration(time.Duration(valueInt) * time.Millisecond) 78 return nil 79 } 80 81 d, err := time.ParseDuration(value) 82 if err != nil { 83 return err 84 } 85 86 t.setDuration(d) 87 return nil 88 } 89 90 func getAdminClient(opts GlobalOptions) (dosa.AdminClient, error) { 91 // from YARPC: "must begin with a letter and consist only of dash-delimited 92 // lower-case ASCII alphanumeric words" -- we do this here because YARPC 93 // will panic if caller name is invalid. 94 if !validNameRegex.MatchString(string(opts.CallerName)) { 95 return nil, fmt.Errorf("invalid caller name: %s, must begin with a letter and consist only of dash-delimited lower-case ASCII alphanumeric words", opts.CallerName) 96 } 97 98 // create connector 99 conn, err := dosa.GetConnector(opts.Connector, map[string]interface{}{ 100 "transport": opts.Transport, 101 "host": opts.Host, 102 "port": opts.Port, 103 "servicename": opts.ServiceName, 104 "callername": opts.CallerName.String(), 105 }) 106 if err != nil { 107 return nil, err 108 } 109 client := dosa.NewAdminClient(conn) 110 111 return client, nil 112 }