github.com/nats-io/nsc/v2@v2.8.7-0.20240307184528-efd7023c6896/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 nats "github.com/nats-io/nats.go" 25 "github.com/nats-io/nsc/v2/cmd/store" 26 "github.com/spf13/cobra" 27 ) 28 29 func createToolRTTCmd() *cobra.Command { 30 var params RttParams 31 var cmd = &cobra.Command{ 32 Use: "rtt", 33 Short: "Calculate the round trip time to the server", 34 Example: "nsc tool rtt", 35 Args: cobra.MinimumNArgs(0), 36 RunE: func(cmd *cobra.Command, args []string) error { 37 return RunAction(cmd, args, ¶ms) 38 }, 39 } 40 params.BindFlags(cmd) 41 return cmd 42 } 43 44 func init() { 45 toolCmd.AddCommand(createToolRTTCmd()) 46 hidden := createToolRTTCmd() 47 hidden.Hidden = true 48 hidden.Example = "nsc tool rtt" 49 GetRootCmd().AddCommand(hidden) 50 } 51 52 type RttParams struct { 53 AccountUserContextParams 54 credsPath string 55 natsURLs []string 56 } 57 58 func (p *RttParams) SetDefaults(ctx ActionCtx) error { 59 return p.AccountUserContextParams.SetDefaults(ctx) 60 } 61 62 func (p *RttParams) PreInteractive(ctx ActionCtx) error { 63 return p.AccountUserContextParams.Edit(ctx) 64 } 65 66 func (p *RttParams) Load(ctx ActionCtx) error { 67 p.credsPath = ctx.StoreCtx().KeyStore.CalcUserCredsPath(p.AccountContextParams.Name, p.UserContextParams.Name) 68 69 if natsURLFlag != "" { 70 p.natsURLs = []string{natsURLFlag} 71 return nil 72 } 73 74 oc, err := ctx.StoreCtx().Store.ReadOperatorClaim() 75 if err != nil { 76 return err 77 } 78 p.natsURLs = oc.OperatorServiceURLs 79 return nil 80 } 81 82 func (p *RttParams) PostInteractive(ctx ActionCtx) error { 83 return nil 84 } 85 86 func (p *RttParams) Validate(ctx ActionCtx) error { 87 if err := p.AccountUserContextParams.Validate(ctx); err != nil { 88 return err 89 } 90 91 if p.credsPath == "" { 92 return fmt.Errorf("a creds file for account %q/%q was not found", p.AccountContextParams.Name, p.UserContextParams.Name) 93 } 94 _, err := os.Stat(p.credsPath) 95 if os.IsNotExist(err) { 96 return err 97 } 98 if len(p.natsURLs) == 0 { 99 return fmt.Errorf("operator %q doesn't have operator_service_urls set", ctx.StoreCtx().Operator.Name) 100 } 101 return nil 102 } 103 104 func (p *RttParams) Run(ctx ActionCtx) (store.Status, error) { 105 nc, err := nats.Connect(strings.Join(p.natsURLs, ", "), 106 createDefaultToolOptions("nsc_rtt", ctx, nats.UserCredentials(p.credsPath))...) 107 if err != nil { 108 return nil, err 109 } 110 defer nc.Close() 111 112 if err != nil { 113 return nil, err 114 } 115 start := time.Now() 116 if err := nc.Flush(); err != nil { 117 return nil, err 118 } 119 rtt := time.Since(start) 120 ctx.CurrentCmd().Printf("round trip time to [%s] = %v\n", nc.ConnectedUrl(), rtt) 121 return nil, nil 122 }