Skip to content

Commit

Permalink
Merge pull request #1148 from ripienaar/iu_rafactor
Browse files Browse the repository at this point in the history
Further refactoring into internal utils package
  • Loading branch information
ripienaar committed Sep 10, 2024
2 parents 3ad589d + 5a4ef08 commit d49f316
Show file tree
Hide file tree
Showing 20 changed files with 302 additions and 292 deletions.
3 changes: 2 additions & 1 deletion cli/account_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"github.com/nats-io/nats-server/v2/server"
"github.com/nats-io/nats.go"
"github.com/nats-io/natscli/columns"
iu "github.com/nats-io/natscli/internal/util"
)

type actCmd struct {
Expand Down Expand Up @@ -403,7 +404,7 @@ func (c *actCmd) infoAction(_ *fisk.ParseContext) error {
tlsc, _ := nc.TLSConnectionState()

var ui *server.UserInfo
if serverMinVersion(nc.ConnectedServerVersion(), 2, 10, 0) {
if iu.ServerMinVersion(nc, 2, 10, 0) {
subj := "$SYS.REQ.USER.INFO"
if opts().Trace {
log.Printf(">>> %s: {}\n", subj)
Expand Down
20 changes: 11 additions & 9 deletions cli/bench_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"bytes"
"context"
"fmt"
"github.com/nats-io/nats.go/jetstream"
"math"
"math/rand"
"os"
Expand All @@ -27,6 +26,9 @@ import (
"sync"
"time"

"github.com/nats-io/nats.go/jetstream"
iu "github.com/nats-io/natscli/internal/util"

"github.com/choria-io/fisk"
"github.com/dustin/go-humanize"
"github.com/gosuri/uiprogress"
Expand Down Expand Up @@ -1722,7 +1724,7 @@ func (c *benchCmd) runCorePublisher(bm *bench.Benchmark, errChan chan error, nc
}

progress = uiprogress.AddBar(barTotal).AppendCompleted().PrependElapsed()
progress.Width = progressWidth()
progress.Width = iu.ProgressWidth()

if numMsg == 0 {
progress.PrependFunc(func(b *uiprogress.Bar) string {
Expand Down Expand Up @@ -1779,7 +1781,7 @@ func (c *benchCmd) runCoreSubscriber(bm *bench.Benchmark, errChan chan error, nc

if c.progressBar {
progress = uiprogress.AddBar(numMsg).AppendCompleted().PrependElapsed()
progress.Width = progressWidth()
progress.Width = iu.ProgressWidth()
}

state := "Setup "
Expand Down Expand Up @@ -1855,7 +1857,7 @@ func (c *benchCmd) runCoreRequester(bm *bench.Benchmark, errChan chan error, nc

if c.progressBar {
progress = uiprogress.AddBar(numMsg).AppendCompleted().PrependElapsed()
progress.Width = progressWidth()
progress.Width = iu.ProgressWidth()
}

var msg []byte
Expand Down Expand Up @@ -1948,7 +1950,7 @@ func (c *benchCmd) runJSPublisher(bm *bench.Benchmark, errChan chan error, nc *n

if c.progressBar {
progress = uiprogress.AddBar(numMsg).AppendCompleted().PrependElapsed()
progress.Width = progressWidth()
progress.Width = iu.ProgressWidth()
}

var msg []byte
Expand Down Expand Up @@ -1996,7 +1998,7 @@ func (c *benchCmd) runJSSubscriber(bm *bench.Benchmark, errChan chan error, nc *

if c.progressBar {
progress = uiprogress.AddBar(numMsg).AppendCompleted().PrependElapsed()
progress.Width = progressWidth()
progress.Width = iu.ProgressWidth()
}

state := "Setup "
Expand Down Expand Up @@ -2186,7 +2188,7 @@ func (c *benchCmd) runKVPutter(bm *bench.Benchmark, errChan chan error, nc *nats

if c.progressBar {
progress = uiprogress.AddBar(numMsg).AppendCompleted().PrependElapsed()
progress.Width = progressWidth()
progress.Width = iu.ProgressWidth()
}

var msg []byte
Expand Down Expand Up @@ -2232,7 +2234,7 @@ func (c *benchCmd) runKVGetter(bm *bench.Benchmark, errChan chan error, nc *nats

if c.progressBar {
progress = uiprogress.AddBar(numMsg).AppendCompleted().PrependElapsed()
progress.Width = progressWidth()
progress.Width = iu.ProgressWidth()
}

state := "Setup "
Expand Down Expand Up @@ -2326,7 +2328,7 @@ func (c *benchCmd) runOldJSSubscriber(bm *bench.Benchmark, errChan chan error, n

if c.progressBar {
progress = uiprogress.AddBar(numMsg).AppendCompleted().PrependElapsed()
progress.Width = progressWidth()
progress.Width = iu.ProgressWidth()
}

state := "Setup "
Expand Down
6 changes: 3 additions & 3 deletions cli/consumer_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ func (c *consumerCmd) graphAction(_ *fisk.ParseContext) error {
asciigraph.Precision(0),
)

asciigraph.Clear()
iu.ClearScreen()

fmt.Printf("Consumer Statistics for %s > %s\n", c.stream, c.consumer)
fmt.Println()
Expand All @@ -483,7 +483,7 @@ func (c *consumerCmd) graphAction(_ *fisk.ParseContext) error {
fmt.Println(deliveredPlot)

case <-ctx.Done():
asciigraph.Clear()
iu.ClearScreen()
return nil
}
}
Expand Down Expand Up @@ -941,7 +941,7 @@ func (c *consumerCmd) showInfo(config api.ConsumerConfig, state api.ConsumerInfo

if len(config.Metadata) > 0 {
cols.AddSectionTitle("Metadata")
maxLen := progressWidth()
maxLen := iu.ProgressWidth()
for k, v := range config.Metadata {
if len(v) > maxLen && maxLen > 20 {
w := maxLen/2 - 10
Expand Down
4 changes: 2 additions & 2 deletions cli/object_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,7 @@ func (c *objCommand) putAction(_ *fisk.ParseContext) error {
progress = uiprogress.AddBar(int(stat.Size())).PrependFunc(func(b *uiprogress.Bar) string {
return fmt.Sprintf("%s / %s", humanize.IBytes(uint64(b.Current())), hs)
})
progress.Width = progressWidth()
progress.Width = util.ProgressWidth()

fmt.Println()
uiprogress.Start()
Expand Down Expand Up @@ -576,7 +576,7 @@ func (c *objCommand) getAction(_ *fisk.ParseContext) error {
progress = uiprogress.AddBar(int(nfo.Size)).PrependFunc(func(b *uiprogress.Bar) string {
return fmt.Sprintf("%s / %s", humanize.IBytes(uint64(b.Current())), hs)
})
progress.Width = progressWidth()
progress.Width = util.ProgressWidth()

fmt.Println()
uiprogress.Start()
Expand Down
3 changes: 2 additions & 1 deletion cli/pub_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/gosuri/uiprogress"
"github.com/nats-io/jsm.go"
"github.com/nats-io/nats.go"
iu "github.com/nats-io/natscli/internal/util"
terminal "golang.org/x/term"
)

Expand Down Expand Up @@ -305,7 +306,7 @@ func (c *pubCmd) publish(_ *fisk.ParseContext) error {
progress = uiprogress.AddBar(c.cnt).PrependFunc(func(b *uiprogress.Bar) string {
return fmt.Sprintf(progressFormat, b.Current(), c.cnt)
}).AppendElapsed()
progress.Width = progressWidth()
progress.Width = iu.ProgressWidth()

fmt.Println()
uiprogress.Start()
Expand Down
7 changes: 7 additions & 0 deletions cli/server_generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ func (c *serverGenerateCmd) generateAction(_ *fisk.ParseContext) error {
return fmt.Errorf("target directory %s already exist", c.target)
}

fmt.Println("This tool generates NATS Server configurations based on a question and answer")
fmt.Println("form-based approach and then renders the result into a directory.")
fmt.Println()
fmt.Println("It supports rendering local bundles compiled into the 'nats' command but can also")
fmt.Println("fetch and render remote ones using a URL.")
fmt.Println()

switch {
case c.source == "":
err = c.pickEmbedded()
Expand Down
6 changes: 3 additions & 3 deletions cli/server_report_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,16 +152,16 @@ func (c *SrvReportCmd) reportCpuOrMem(mem bool) error {
return iu.PrintJSON(usage)
}

width := progressWidth() / 2
width := iu.ProgressWidth() / 2
if width > 30 {
width = 30
}

if mem {
return barGraph(os.Stdout, usage, "Memory Usage", width, true)
return iu.BarGraph(os.Stdout, usage, "Memory Usage", width, true)
}

return barGraph(os.Stdout, usage, "CPU Usage", width, false)
return iu.BarGraph(os.Stdout, usage, "CPU Usage", width, false)
}

func (c *SrvReportCmd) reportJetStream(_ *fisk.ParseContext) error {
Expand Down
3 changes: 2 additions & 1 deletion cli/server_request_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ import (
"bytes"
"encoding/json"
"fmt"
"github.com/nats-io/natscli/options"
"os"
"path/filepath"
"time"

"github.com/nats-io/natscli/options"

"github.com/choria-io/fisk"
"github.com/nats-io/nats-server/v2/server"
"github.com/nats-io/nats.go"
Expand Down
19 changes: 10 additions & 9 deletions cli/server_watch_acct_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/choria-io/fisk"
"github.com/nats-io/nats-server/v2/server"
"github.com/nats-io/nats.go"
iu "github.com/nats-io/natscli/internal/util"
terminal "golang.org/x/term"
)

Expand All @@ -53,7 +54,7 @@ func configureServerWatchAccountCommand(watch *fisk.CmdClause) {
"slow": "Slow Consumers",
},
}
sortKeys := mapKeys(c.sortNames)
sortKeys := iu.MapKeys(c.sortNames)
sort.Strings(sortKeys)

accounts := watch.Command("accounts", "Watch account usage").Alias("account").Alias("acct").Action(c.accountsAction)
Expand Down Expand Up @@ -162,19 +163,19 @@ func (c *SrvWatchAccountCmd) redraw() error {

switch c.sort {
case "subs":
return sortMultiSort(ai.NumSubs, aj.NumSubs, ai.Conns, aj.Conns)
return iu.SortMultiSort(ai.NumSubs, aj.NumSubs, ai.Conns, aj.Conns)
case "slow":
return sortMultiSort(ai.SlowConsumers, aj.SlowConsumers, ai.Conns, aj.Conns)
return iu.SortMultiSort(ai.SlowConsumers, aj.SlowConsumers, ai.Conns, aj.Conns)
case "sentb":
return sortMultiSort(ai.Sent.Bytes, aj.Sent.Bytes, ai.Conns, aj.Conns)
return iu.SortMultiSort(ai.Sent.Bytes, aj.Sent.Bytes, ai.Conns, aj.Conns)
case "sentm":
return sortMultiSort(ai.Sent.Msgs, aj.Sent.Msgs, ai.Conns, aj.Conns)
return iu.SortMultiSort(ai.Sent.Msgs, aj.Sent.Msgs, ai.Conns, aj.Conns)
case "recvb":
return sortMultiSort(ai.Received.Bytes, aj.Received.Bytes, ai.Conns, aj.Conns)
return iu.SortMultiSort(ai.Received.Bytes, aj.Received.Bytes, ai.Conns, aj.Conns)
case "recvm":
return sortMultiSort(ai.Received.Msgs, aj.Received.Msgs, ai.Conns, aj.Conns)
return iu.SortMultiSort(ai.Received.Msgs, aj.Received.Msgs, ai.Conns, aj.Conns)
default:
return sortMultiSort(ai.Conns, aj.Conns, ai.Conns, aj.Conns)
return iu.SortMultiSort(ai.Conns, aj.Conns, ai.Conns, aj.Conns)
}
})

Expand All @@ -193,7 +194,7 @@ func (c *SrvWatchAccountCmd) redraw() error {
matched = accounts[:c.topCount]
}

clearScreen()
iu.ClearScreen()
for _, account := range matched {
acct := account.Account
if account.Name != "" {
Expand Down
15 changes: 8 additions & 7 deletions cli/server_watch_js_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/choria-io/fisk"
"github.com/nats-io/nats-server/v2/server"
"github.com/nats-io/nats.go"
iu "github.com/nats-io/natscli/internal/util"
terminal "golang.org/x/term"
)

Expand All @@ -52,7 +53,7 @@ func configureServerWatchJSCommand(watch *fisk.CmdClause) {
},
}

sortKeys := mapKeys(c.sortNames)
sortKeys := iu.MapKeys(c.sortNames)
sort.Strings(sortKeys)

js := watch.Command("jetstream", "Watch JetStream statistics").Alias("js").Alias("jsz").Action(c.jetstreamAction)
Expand Down Expand Up @@ -173,15 +174,15 @@ func (c *SrvWatchJSCmd) redraw() error {

switch c.sort {
case "mem":
return sortMultiSort(si.Memory, sj.Memory, servers[i].Server.Name, servers[j].Server.Name)
return iu.SortMultiSort(si.Memory, sj.Memory, servers[i].Server.Name, servers[j].Server.Name)
case "file":
return sortMultiSort(si.Store, sj.Store, servers[i].Server.Name, servers[j].Server.Name)
return iu.SortMultiSort(si.Store, sj.Store, servers[i].Server.Name, servers[j].Server.Name)
case "api":
return sortMultiSort(si.API.Total, sj.API.Total, servers[i].Server.Name, servers[j].Server.Name)
return iu.SortMultiSort(si.API.Total, sj.API.Total, servers[i].Server.Name, servers[j].Server.Name)
case "err":
return sortMultiSort(si.API.Errors, sj.API.Errors, servers[i].Server.Name, servers[j].Server.Name)
return iu.SortMultiSort(si.API.Errors, sj.API.Errors, servers[i].Server.Name, servers[j].Server.Name)
default:
return sortMultiSort(si.HAAssets, sj.HAAssets, servers[i].Server.Name, servers[j].Server.Name)
return iu.SortMultiSort(si.HAAssets, sj.HAAssets, servers[i].Server.Name, servers[j].Server.Name)
}
})

Expand Down Expand Up @@ -213,7 +214,7 @@ func (c *SrvWatchJSCmd) redraw() error {
}
table.AddFooter("Totals (All Servers)", f(assets), fiBytes(mem), fiBytes(store), f(api), f(apiError))

clearScreen()
iu.ClearScreen()
fmt.Print(table.Render())

return nil
Expand Down
27 changes: 14 additions & 13 deletions cli/server_watch_srv_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/choria-io/fisk"
"github.com/nats-io/nats-server/v2/server"
"github.com/nats-io/nats.go"
iu "github.com/nats-io/natscli/internal/util"
terminal "golang.org/x/term"
)

Expand Down Expand Up @@ -58,7 +59,7 @@ func configureServerWatchServerCommand(watch *fisk.CmdClause) {
},
}

sortKeys := mapKeys(c.sortNames)
sortKeys := iu.MapKeys(c.sortNames)
sort.Strings(sortKeys)

servers := watch.Command("servers", "Watch server statistics").Alias("server").Alias("srv").Action(c.serversAction)
Expand Down Expand Up @@ -178,27 +179,27 @@ func (c *SrvWatchServerCmd) redraw() error {

switch c.sort {
case "subs":
return sortMultiSort(si.NumSubs, sj.NumSubs, iName, jName)
return iu.SortMultiSort(si.NumSubs, sj.NumSubs, iName, jName)
case "sentb":
return sortMultiSort(si.Sent.Bytes, sj.Sent.Bytes, iName, jName)
return iu.SortMultiSort(si.Sent.Bytes, sj.Sent.Bytes, iName, jName)
case "sentm":
return sortMultiSort(si.Sent.Msgs, sj.Sent.Msgs, iName, jName)
return iu.SortMultiSort(si.Sent.Msgs, sj.Sent.Msgs, iName, jName)
case "recvb":
return sortMultiSort(si.Received.Bytes, sj.Received.Bytes, iName, jName)
return iu.SortMultiSort(si.Received.Bytes, sj.Received.Bytes, iName, jName)
case "recvm":
return sortMultiSort(si.Received.Msgs, sj.Received.Msgs, iName, jName)
return iu.SortMultiSort(si.Received.Msgs, sj.Received.Msgs, iName, jName)
case "slow":
return sortMultiSort(si.SlowConsumers, sj.SlowConsumers, iName, jName)
return iu.SortMultiSort(si.SlowConsumers, sj.SlowConsumers, iName, jName)
case "route":
return sortMultiSort(len(si.Routes), len(sj.Routes), iName, jName)
return iu.SortMultiSort(len(si.Routes), len(sj.Routes), iName, jName)
case "gway":
return sortMultiSort(len(si.Gateways), len(sj.Gateways), iName, jName)
return iu.SortMultiSort(len(si.Gateways), len(sj.Gateways), iName, jName)
case "mem":
return sortMultiSort(si.Mem, sj.Mem, iName, jName)
return iu.SortMultiSort(si.Mem, sj.Mem, iName, jName)
case "cpu":
return sortMultiSort(si.CPU, sj.CPU, iName, jName)
return iu.SortMultiSort(si.CPU, sj.CPU, iName, jName)
default:
return sortMultiSort(si.Connections, sj.Connections, iName, jName)
return iu.SortMultiSort(si.Connections, sj.Connections, iName, jName)
}
})

Expand Down Expand Up @@ -235,7 +236,7 @@ func (c *SrvWatchServerCmd) redraw() error {

table.AddFooter("Totals (All Servers)", f(conns), f(subs), f(slow), fiBytes(uint64(mem)), "", "", "", fmt.Sprintf("%s / %s", f(sentM), fiBytes(uint64(sentB))), fmt.Sprintf("%s / %s", f(recvM), fiBytes(uint64(recvB))))

clearScreen()
iu.ClearScreen()
fmt.Print(table.Render())
return nil
}
Loading

0 comments on commit d49f316

Please sign in to comment.