Skip to content

Commit

Permalink
Add top command and remove inoperable registry
Browse files Browse the repository at this point in the history
Signed-off-by: R.I.Pienaar <[email protected]>
  • Loading branch information
ripienaar committed Dec 28, 2023
1 parent 9c71c11 commit 494fcb6
Show file tree
Hide file tree
Showing 10 changed files with 1,370 additions and 72 deletions.
1 change: 0 additions & 1 deletion cli/schema_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ func configureSchemaCommand(app commandHost) {
configureSchemaInfoCommand(schema)
configureSchemaValidateCommand(schema)
configureSchemaReqCommand(schema)
configureSchemaRegistryCommand(schema)
}

func init() {
Expand Down
63 changes: 0 additions & 63 deletions cli/schema_registry_command.go

This file was deleted.

94 changes: 94 additions & 0 deletions cli/top_command.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// Copyright 2023 The NATS Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cli

import (
"fmt"

"github.com/choria-io/fisk"
"github.com/nats-io/nats-server/v2/server"
"github.com/nats-io/natscli/top"
tu "github.com/nats-io/natscli/top/util"
ui "gopkg.in/gizak/termui.v1"
)

type topCmd struct {
host string
conns int
delay int
sort string
lookup bool
output string
outputDelimiter string
raw bool
maxRefresh int
showSubs bool
}

func configureTopCommand(app commandHost) {
c := &topCmd{}

top := app.Command("top", "Shows top-like statistic for connections on a specific server").Action(c.topAction)
top.Arg("name", "The server name to gather statistics for").Required().StringVar(&c.host)
top.Flag("conns", "Maximum number of connections to show").Default("1024").Short('n').IntVar(&c.conns)
top.Flag("interval", "Refresh interval").Default("1").Short('d').IntVar(&c.delay)
top.Flag("sort", "Sort connections by").Default("cid").EnumVar(&c.sort, "cid", "start", "subs", "pending", "msgs_to", "msgs_from", "bytes_to", "bytes_from", "last", "idle", "uptime", "stop", "reason", "rtt")
top.Flag("lookup", "Looks up client addresses in DNS").Default("false").UnNegatableBoolVar(&c.lookup)
top.Flag("output", "Saves the first snapshot to a file").Short('o').StringVar(&c.output)
top.Flag("delimiter", "Specifies a output delimiter, defaults to grid-like text").StringVar(&c.outputDelimiter)
top.Flag("raw", "Show raw bytes").Short('b').Default("false").UnNegatableBoolVar(&c.raw)
top.Flag("max-refresh", "Maximum refreshes").Short('r').Default("-1").IntVar(&c.maxRefresh)
top.Flag("subs", "Shows the subscriptions column").Default("false").UnNegatableBoolVar(&c.showSubs)
}

func init() {
registerCommand("top", 17, configureTopCommand)
}

func (c *topCmd) topAction(_ *fisk.ParseContext) error {
nc, _, err := prepareHelper("", natsOpts()...)
if err != nil {
return err
}

engine := tu.NewEngine(nc, c.host, c.conns, c.delay, opts.Trace)

_, err = engine.Request("VARZ")
if err != nil {
return fmt.Errorf("initial test request failed: %v", err)
}

sortOpt := server.SortOpt(c.sort)
if !sortOpt.IsValid() {
return fmt.Errorf("invalid sort option: %s", c.sort)
}
engine.SortOpt = sortOpt
engine.DisplaySubs = c.showSubs

if c.output != "" {
return top.SaveStatsSnapshotToFile(engine, c.output, c.outputDelimiter)
}

err = ui.Init()
if err != nil {
panic(err)
}
defer ui.Close()

go engine.MonitorStats()

top.StartUI(engine, c.lookup, c.raw, c.maxRefresh)

return nil
}
12 changes: 11 additions & 1 deletion cli/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,16 @@ func natsOpts() []nats.Option {
return append(copts, []nats.Option{
nats.Name(connectionName),
nats.MaxReconnects(-1),
nats.ConnectHandler(func(conn *nats.Conn) {
if opts.Trace {
log.Printf(">>> Connected to %s", conn.ConnectedUrlRedacted())
}
}),
nats.DiscoveredServersHandler(func(conn *nats.Conn) {
if opts.Trace {
log.Printf(">>> Discovered new servers, known servers are now %s", strings.Join(conn.Servers(), ", "))
}
}),
nats.DisconnectErrHandler(func(nc *nats.Conn, err error) {
if err != nil {
log.Printf("Disconnected due to: %s, will attempt reconnect", err)
Expand All @@ -370,7 +380,7 @@ func natsOpts() []nats.Option {
if url == "" {
log.Printf("Unexpected NATS error: %s", err)
} else {
log.Printf("Unexpected NATS error from server %s: %s", url, err)
log.Printf("Unexpected NATS error from server %s: %s", nc.ConnectedUrlRedacted(), err)
}
}),
}...)
Expand Down
10 changes: 7 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module github.com/nats-io/natscli

go 1.20
go 1.21

toolchain go1.21.2

Check failure on line 5 in go.mod

View workflow job for this annotation

GitHub Actions / test (1.20)

unknown directive: toolchain

require (
github.com/AlecAivazis/survey/v2 v2.3.7
Expand All @@ -21,7 +23,7 @@ require (
github.com/mattn/go-isatty v0.0.20
github.com/nats-io/jsm.go v0.1.1-0.20231218145142-927c0d660be1
github.com/nats-io/jwt/v2 v2.5.3
github.com/nats-io/nats-server/v2 v2.10.7
github.com/nats-io/nats-server/v2 v2.10.6
github.com/nats-io/nats.go v1.31.0
github.com/nats-io/nkeys v0.4.6
github.com/nats-io/nuid v1.0.1
Expand All @@ -30,8 +32,9 @@ require (
github.com/santhosh-tekuri/jsonschema/v5 v5.3.1
github.com/synadia-io/jwt-auth-builder.go v0.0.0-20231201125238-9011ec8a3346
github.com/tylertreat/hdrhistogram-writer v0.0.0-20210816161836-2e440612a39f
golang.org/x/crypto v0.16.0
golang.org/x/crypto v0.17.0
golang.org/x/term v0.15.0
gopkg.in/gizak/termui.v1 v1.0.0-20151021151108-e62b5929642a
gopkg.in/yaml.v3 v3.0.1
)

Expand All @@ -46,6 +49,7 @@ require (
github.com/minio/highwayhash v1.0.2 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/nats-io/nsc/v2 v2.8.6-0.20231117160437-b49262c20be8 // indirect
github.com/nsf/termbox-go v1.1.1 // indirect
github.com/prometheus/client_model v0.4.1-0.20230718164431-9a2bf3000d16 // indirect
github.com/prometheus/procfs v0.11.1 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
Expand Down
17 changes: 13 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51/go.mod h1:C
github.com/klauspost/compress v1.17.4 h1:Ej5ixsIri7BrIjBkRZLTo6ghwrEtHFk7ijlczPW4fZ4=
github.com/klauspost/compress v1.17.4/go.mod h1:/dCuZOvVtNoHsyb+cuJD3itjs3NbnF6KH9zAO4BDxPM=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
Expand All @@ -67,6 +68,7 @@ github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hd
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/mattn/go-runewidth v0.0.13 h1:lTGmDsbAYt5DmK6OnoV7EuIF1wEIFAcxld6ypU4OSgU=
github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0 h1:jWpvCLoY8Z/e3VKvlsiIGKtc+UG6U5vzxaoagmhXfyg=
Expand All @@ -82,8 +84,8 @@ github.com/nats-io/jsm.go v0.1.1-0.20231218145142-927c0d660be1 h1:oIkbX+ymE5Ddcy
github.com/nats-io/jsm.go v0.1.1-0.20231218145142-927c0d660be1/go.mod h1:4HtrppnSb8m+1bX9bH/IhqmYDluP8tXRVVEznXo+kIg=
github.com/nats-io/jwt/v2 v2.5.3 h1:/9SWvzc6hTfamcgXJ3uYRpgj+QuY2aLNqRiqrKcrpEo=
github.com/nats-io/jwt/v2 v2.5.3/go.mod h1:iysuPemFcc7p4IoYots3IuELSI4EDe9Y0bQMe+I3Bf4=
github.com/nats-io/nats-server/v2 v2.10.7 h1:f5VDy+GMu7JyuFA0Fef+6TfulfCs5nBTgq7MMkFJx5Y=
github.com/nats-io/nats-server/v2 v2.10.7/go.mod h1:V2JHOvPiPdtfDXTuEUsthUnCvSDeFrK4Xn9hRo6du7c=
github.com/nats-io/nats-server/v2 v2.10.6 h1:40U3ngyAKyC1tNT4Kw7PjuvivY74NTYD3qyIHxZUHKQ=
github.com/nats-io/nats-server/v2 v2.10.6/go.mod h1:IrTXS8o4Roa3G2kW8L5mEtSdmSrFjKhYb/m2g0gQ/vc=
github.com/nats-io/nats.go v1.31.0 h1:/WFBHEc/dOKBF6qf1TZhrdEfTmOZ5JzdJ+Y3m6Y/p7E=
github.com/nats-io/nats.go v1.31.0/go.mod h1:di3Bm5MLsoB4Bx61CBTsxuarI36WbhAwOm8QrW39+i8=
github.com/nats-io/nkeys v0.4.6 h1:IzVe95ru2CT6ta874rt9saQRkWfe2nFj1NtvYSLqMzY=
Expand All @@ -93,6 +95,8 @@ github.com/nats-io/nsc/v2 v2.8.6-0.20231117160437-b49262c20be8/go.mod h1:iOC7u3x
github.com/nats-io/nuid v1.0.1 h1:5iA8DT8V7q8WK2EScv2padNa/rTESc1KdnPw4TC2paw=
github.com/nats-io/nuid v1.0.1/go.mod h1:19wcPz3Ph3q0Jbyiqsd0kePYG7A95tJPxeL+1OSON2c=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
github.com/nsf/termbox-go v1.1.1 h1:nksUPLCb73Q++DwbYUBEglYBRPZyoXJdrj5L+TkjyZY=
github.com/nsf/termbox-go v1.1.1/go.mod h1:T0cTdVuOwf7pHQNtfhnEbzHbcNyCEcVU4YPpouCbVxo=
github.com/pkg/profile v1.6.0/go.mod h1:qBsxPvzyUincmltOk6iyRVxHYg4adc0OFOv72ZdLa18=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
Expand All @@ -107,6 +111,7 @@ github.com/prometheus/procfs v0.11.1/go.mod h1:eesXgaPo1q7lBpVMoMy0ZOFTth9hBn4W/
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ=
github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog=
github.com/santhosh-tekuri/jsonschema/v5 v5.3.1 h1:lZUw3E0/J3roVtGQ+SCrUrg3ON6NgVqpn3+iol9aGu4=
github.com/santhosh-tekuri/jsonschema/v5 v5.3.1/go.mod h1:uToXkOrWAZ6/Oc07xWQrPOhJotwFIyu2bBVN41fcDUY=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand All @@ -116,6 +121,7 @@ github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.4/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/synadia-io/jwt-auth-builder.go v0.0.0-20231201125238-9011ec8a3346 h1:6oelWMT/lshfaMBFuMJ5ueCX67KXh7C8xpiXNnDMqFg=
github.com/synadia-io/jwt-auth-builder.go v0.0.0-20231201125238-9011ec8a3346/go.mod h1:vm/hZ9cVN5pPp1OVAzNZF7RPkTY8wMszri9MeaAjiHQ=
github.com/tylertreat/hdrhistogram-writer v0.0.0-20210816161836-2e440612a39f h1:SGznmvCovewbaSgBsHgdThtWsLj5aCLX/3ZXMLd1UD0=
Expand All @@ -124,8 +130,8 @@ github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5t
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.16.0 h1:mMMrFzRSCF0GvB7Ne27XVtVAaXLrPmgPC7/v0tkwHaY=
golang.org/x/crypto v0.16.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4=
golang.org/x/crypto v0.17.0 h1:r8bRNjWL3GshPW3gkd+RpvzWrZAwPS49OmTGZ/uhM4k=
golang.org/x/crypto v0.17.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4=
golang.org/x/exp v0.0.0-20180321215751-8460e604b9de/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20180807140117-3d87b88a115f/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20190125153040-c74c464bbbf2/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
Expand Down Expand Up @@ -192,6 +198,9 @@ google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqw
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/gizak/termui.v1 v1.0.0-20151021151108-e62b5929642a h1:3xTF5ZxN6yR6SZlNMCN7z4X633Zi87YC8FkxFsdgdM0=
gopkg.in/gizak/termui.v1 v1.0.0-20151021151108-e62b5929642a/go.mod h1:An8M7oA8GI+74WiR1qJo67ptrdlb7LJLqoXspnSrMUA=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Expand Down
22 changes: 22 additions & 0 deletions top/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
The MIT License (MIT)

Copyright (c) 2015-2021 NATS Messaging System

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

Loading

0 comments on commit 494fcb6

Please sign in to comment.