github.com/kbehouse/nsc@v0.0.6/cmd/rtttool.go (about) 1 /* 2 * Copyright 2018-2019 The NATS Authors 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 package cmd 17 18 import ( 19 "fmt" 20 "os" 21 "strings" 22 "time" 23 24 "github.com/kbehouse/nsc/cmd/store" 25 26 nats "github.com/nats-io/nats.go" 27 "github.com/spf13/cobra" 28 ) 29 30 func createToolRTTCmd() *cobra.Command { 31 var params RttParams 32 var cmd = &cobra.Command{ 33 Use: "rtt", 34 Short: "Calculate the round trip time to the server", 35 Example: "nsc tool rtt", 36 Args: cobra.MinimumNArgs(0), 37 RunE: func(cmd *cobra.Command, args []string) error { 38 return RunAction(cmd, args, ¶ms) 39 }, 40 } 41 params.BindFlags(cmd) 42 return cmd 43 } 44 45 func init() { 46 toolCmd.AddCommand(createToolRTTCmd()) 47 hidden := createToolRTTCmd() 48 hidden.Hidden = true 49 hidden.Example = "nsc tool rtt" 50 GetRootCmd().AddCommand(hidden) 51 } 52 53 type RttParams struct { 54 AccountUserContextParams 55 credsPath string 56 natsURLs []string 57 } 58 59 func (p *RttParams) SetDefaults(ctx ActionCtx) error { 60 return p.AccountUserContextParams.SetDefaults(ctx) 61 } 62 63 func (p *RttParams) PreInteractive(ctx ActionCtx) error { 64 return p.AccountUserContextParams.Edit(ctx) 65 } 66 67 func (p *RttParams) Load(ctx ActionCtx) error { 68 p.credsPath = ctx.StoreCtx().KeyStore.CalcUserCredsPath(p.AccountContextParams.Name, p.UserContextParams.Name) 69 70 if natsURLFlag != "" { 71 p.natsURLs = []string{natsURLFlag} 72 return nil 73 } 74 75 oc, err := ctx.StoreCtx().Store.ReadOperatorClaim() 76 if err != nil { 77 return err 78 } 79 p.natsURLs = oc.OperatorServiceURLs 80 return nil 81 } 82 83 func (p *RttParams) PostInteractive(ctx ActionCtx) error { 84 return nil 85 } 86 87 func (p *RttParams) Validate(ctx ActionCtx) error { 88 if err := p.AccountUserContextParams.Validate(ctx); err != nil { 89 return err 90 } 91 92 if p.credsPath == "" { 93 return fmt.Errorf("a creds file for account %q/%q was not found", p.AccountContextParams.Name, p.UserContextParams.Name) 94 } 95 _, err := os.Stat(p.credsPath) 96 if os.IsNotExist(err) { 97 return err 98 } 99 if len(p.natsURLs) == 0 { 100 return fmt.Errorf("operator %q doesn't have operator_service_urls set", ctx.StoreCtx().Operator.Name) 101 } 102 return nil 103 } 104 105 func (p *RttParams) Run(ctx ActionCtx) (store.Status, error) { 106 nc, err := nats.Connect(strings.Join(p.natsURLs, ", "), 107 createDefaultToolOptions("nsc_rtt", ctx, nats.UserCredentials(p.credsPath))...) 108 if err != nil { 109 return nil, err 110 } 111 defer nc.Close() 112 113 if err != nil { 114 return nil, err 115 } 116 start := time.Now() 117 if err := nc.Flush(); err != nil { 118 return nil, err 119 } 120 rtt := time.Since(start) 121 ctx.CurrentCmd().Printf("round trip time to [%s] = %v\n", nc.ConnectedUrl(), rtt) 122 return nil, nil 123 }