• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

nats-io / nsc / 14765656943

30 Apr 2025 10:34PM UTC coverage: 69.949% (-4.2%) from 74.148%
14765656943

Pull #691

github

web-flow
Merge b4ee16f94 into a9c0df586
Pull Request #691: Generate docs

1 of 1 new or added line in 1 file covered. (100.0%)

3680 existing lines in 93 files now uncovered.

12325 of 17620 relevant lines covered (69.95%)

1.66 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

22.97
/cmd/rtttool.go
1
// Copyright 2018-2025 The NATS Authors
2
// Licensed under the Apache License, Version 2.0 (the "License");
3
// you may not use this file except in compliance with the License.
4
// You may obtain a copy of the License at
5
//
6
// http://www.apache.org/licenses/LICENSE-2.0
7
//
8
// Unless required by applicable law or agreed to in writing, software
9
// distributed under the License is distributed on an "AS IS" BASIS,
10
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
// See the License for the specific language governing permissions and
12
// limitations under the License.
13

14
package cmd
15

16
import (
17
        "fmt"
18
        "os"
19
        "strings"
20
        "time"
21

22
        nats "github.com/nats-io/nats.go"
23
        "github.com/nats-io/nsc/v2/cmd/store"
24
        "github.com/spf13/cobra"
25
)
26

27
func createToolRTTCmd() *cobra.Command {
28
        var params RttParams
29
        var cmd = &cobra.Command{
2✔
30
                Use:     "rtt",
2✔
31
                Short:   "Calculate the round trip time to the server",
2✔
32
                Example: "nsc tool rtt",
2✔
33
                Args:    cobra.MinimumNArgs(0),
2✔
34
                RunE: func(cmd *cobra.Command, args []string) error {
2✔
35
                        return RunAction(cmd, args, &params)
2✔
36
                },
2✔
UNCOV
37
        }
×
UNCOV
38
        params.BindFlags(cmd)
×
39
        return cmd
40
}
2✔
41

2✔
42
func init() {
43
        toolCmd.AddCommand(createToolRTTCmd())
44
        hidden := createToolRTTCmd()
2✔
45
        hidden.Hidden = true
2✔
46
        hidden.Example = "nsc tool rtt"
2✔
47
        GetRootCmd().AddCommand(hidden)
2✔
48
}
2✔
49

2✔
50
type RttParams struct {
2✔
51
        AccountUserContextParams
52
        credsPath string
53
        natsURLs  []string
54
}
55

56
func (p *RttParams) SetDefaults(ctx ActionCtx) error {
57
        return p.AccountUserContextParams.SetDefaults(ctx)
UNCOV
58
}
×
UNCOV
59

×
60
func (p *RttParams) PreInteractive(ctx ActionCtx) error {
×
61
        return p.AccountUserContextParams.Edit(ctx)
62
}
×
UNCOV
63

×
UNCOV
64
func (p *RttParams) Load(ctx ActionCtx) error {
×
65
        p.credsPath = ctx.StoreCtx().KeyStore.CalcUserCredsPath(p.AccountContextParams.Name, p.UserContextParams.Name)
UNCOV
66

×
UNCOV
67
        if natsURLFlag != "" {
×
68
                p.natsURLs = []string{natsURLFlag}
×
69
                return nil
×
70
        }
×
UNCOV
71

×
UNCOV
72
        oc, err := ctx.StoreCtx().Store.ReadOperatorClaim()
×
73
        if err != nil {
74
                return err
×
75
        }
×
UNCOV
76
        p.natsURLs = oc.OperatorServiceURLs
×
UNCOV
77
        return nil
×
UNCOV
78
}
×
UNCOV
79

×
80
func (p *RttParams) PostInteractive(ctx ActionCtx) error {
81
        return nil
82
}
×
UNCOV
83

×
UNCOV
84
func (p *RttParams) Validate(ctx ActionCtx) error {
×
85
        if err := p.AccountUserContextParams.Validate(ctx); err != nil {
86
                return err
×
87
        }
×
UNCOV
88

×
UNCOV
89
        if p.credsPath == "" {
×
90
                return fmt.Errorf("a creds file for account %q/%q was not found", p.AccountContextParams.Name, p.UserContextParams.Name)
91
        }
×
UNCOV
92
        _, err := os.Stat(p.credsPath)
×
UNCOV
93
        if os.IsNotExist(err) {
×
94
                return err
×
95
        }
×
UNCOV
96
        if len(p.natsURLs) == 0 {
×
97
                return fmt.Errorf("operator %q doesn't have operator_service_urls set", ctx.StoreCtx().Operator.Name)
×
98
        }
×
UNCOV
99
        return nil
×
UNCOV
100
}
×
UNCOV
101

×
102
func (p *RttParams) Run(ctx ActionCtx) (store.Status, error) {
103
        nc, err := nats.Connect(strings.Join(p.natsURLs, ", "),
UNCOV
104
                createDefaultToolOptions("nsc_rtt", ctx, nats.UserCredentials(p.credsPath))...)
×
UNCOV
105
        if err != nil {
×
106
                return nil, err
×
107
        }
×
UNCOV
108
        defer nc.Close()
×
UNCOV
109

×
UNCOV
110
        start := time.Now()
×
UNCOV
111
        if err := nc.Flush(); err != nil {
×
112
                return nil, err
×
113
        }
×
UNCOV
114
        rtt := time.Since(start)
×
UNCOV
115
        _, err = fmt.Fprintf(ctx.CurrentCmd().OutOrStdout(), "round trip time to [%s] = %v\n", nc.ConnectedUrl(), rtt)
×
UNCOV
116
        return nil, err
×
UNCOV
117
}
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc