Skip to content
This repository has been archived by the owner on Jul 28, 2021. It is now read-only.

Commit

Permalink
Merge pull request #126 from nats-io/humanize
Browse files Browse the repository at this point in the history
parse bytes when adding a stream and friendlier info
  • Loading branch information
ripienaar authored Mar 23, 2020
2 parents 61721a6 + 2491daa commit d62148e
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 11 deletions.
5 changes: 1 addition & 4 deletions nats/bench_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,7 @@ func (c *benchCmd) bench(_ *kingpin.ParseContext) error {
return fmt.Errorf("number of messages should be greater than 0")
}

log.Printf("Starting benchmark [msgs=%s, msgsize=%s, pubs=%d, subs=%d]\n", humanize.Comma(int64(c.numMsg)), humanize.IBytes(uint64(c.msgSize)), c.numPubs, c.numSubs)
if c.progress {
log.Print("Connections will flush after publish completed\n\n")
}
log.Printf("Starting benchmark [msgs=%s, msgsize=%s, pubs=%d, subs=%d]\n\n", humanize.Comma(int64(c.numMsg)), humanize.IBytes(uint64(c.msgSize)), c.numPubs, c.numSubs)

bm := bench.NewBenchmark("NATS", c.numSubs, c.numPubs)

Expand Down
32 changes: 26 additions & 6 deletions nats/stream_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -452,11 +452,31 @@ func (c *streamCmd) showStreamConfig(cfg api.StreamConfig) {
fmt.Printf(" Acknowledgements: %v\n", !cfg.NoAck)
fmt.Printf(" Retention: %s - %s\n", cfg.Storage.String(), cfg.Retention.String())
fmt.Printf(" Replicas: %d\n", cfg.Replicas)
fmt.Printf(" Maximum Messages: %d\n", cfg.MaxMsgs)
fmt.Printf(" Maximum Bytes: %d\n", cfg.MaxBytes)
fmt.Printf(" Maximum Age: %s\n", cfg.MaxAge.String())
fmt.Printf(" Maximum Message Size: %d\n", cfg.MaxMsgSize)
fmt.Printf(" Maximum Consumers: %d\n", cfg.MaxConsumers)
if cfg.MaxMsgs == -1 {
fmt.Println(" Maximum Messages: unlimited")
} else {
fmt.Printf(" Maximum Messages: %s\n", humanize.Comma(cfg.MaxMsgs))
}
if cfg.MaxBytes == -1 {
fmt.Println(" Maximum Bytes: unlimited")
} else {
fmt.Printf(" Maximum Bytes: %s\n", humanize.IBytes(uint64(cfg.MaxBytes)))
}
if cfg.MaxAge == -1 {
fmt.Println(" Maximum Age: unlimited")
} else {
fmt.Printf(" Maximum Age: %s\n", cfg.MaxAge.String())
}
if cfg.MaxMsgSize == -1 {
fmt.Println(" Maximum Message Size: unlimited")
} else {
fmt.Printf(" Maximum Message Size: %s\n", humanize.IBytes(uint64(cfg.MaxMsgSize)))
}
if cfg.MaxConsumers == -1 {
fmt.Println(" Maximum Consumers: unlimited")
} else {
fmt.Printf(" Maximum Consumers: %d\n", cfg.MaxConsumers)
}
if cfg.Template != "" {
fmt.Printf(" Managed by Template: %s\n", cfg.Template)
}
Expand Down Expand Up @@ -616,7 +636,7 @@ func (c *streamCmd) prepareConfig() (cfg api.StreamConfig) {
}

if c.maxBytesLimit == 0 {
c.maxBytesLimit, err = askOneInt("Message size limit", "-1", "Defines the combined size of all messages in a Stream, when exceeded oldest messages are removed, -1 for unlimited. Settable using --max-bytes")
c.maxBytesLimit, err = askOneBytes("Message size limit", "-1", "Defines the combined size of all messages in a Stream, when exceeded oldest messages are removed, -1 for unlimited. Settable using --max-bytes")
kingpin.FatalIfError(err, "invalid input")
}

Expand Down
22 changes: 21 additions & 1 deletion nats/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"unicode"

"github.com/AlecAivazis/survey/v2"
"github.com/dustin/go-humanize"
"github.com/nats-io/nats.go"

"github.com/nats-io/jsm.go"
Expand Down Expand Up @@ -209,13 +210,32 @@ func askConfirmation(prompt string, dflt bool) (bool, error) {
return ans, err
}

func askOneBytes(prompt string, dflt string, help string) (int64, error) {
val := ""
err := survey.AskOne(&survey.Input{
Message: prompt,
Default: dflt,
Help: help,
}, &val, survey.WithValidator(survey.Required))
if err != nil {
return 0, err
}

i, err := humanize.ParseBytes(val)
if err != nil {
return 0, err
}

return int64(i), nil
}

func askOneInt(prompt string, dflt string, help string) (int64, error) {
val := ""
err := survey.AskOne(&survey.Input{
Message: prompt,
Default: dflt,
Help: help,
}, &val)
}, &val, survey.WithValidator(survey.Required))
if err != nil {
return 0, err
}
Expand Down

0 comments on commit d62148e

Please sign in to comment.