Repository: nats-io/nsc
Pull request #377 (2021-03-22 18:29:49)

Names and units
2 separate commits. one for units and one for jetstream arguments to start with js-


Patch:
diff --git a/cmd/common.go b/cmd/common.go
index da77f8f..5eab251 100644
--- a/cmd/common.go
+++ b/cmd/common.go
@@ -26,7 +26,6 @@ import (
 	"os"
 	"path"
 	"path/filepath"
-	"regexp"
 	"strconv"
 	"strings"
 	"time"
@@ -141,40 +140,18 @@ func ParseNumber(s string) (int64, error) {
 	if s == "" {
 		return 0, nil
 	}
-	s = strings.ToUpper(s)
-	re := regexp.MustCompile(`(-?\d+$)`)
-	m := re.FindStringSubmatch(s)
-	if m != nil {
-		v, err := strconv.ParseInt(m[0], 10, 64)
-		if err != nil {
-			return 0, err
-		}
-		return v, nil
+	isNeg := strings.HasPrefix(s, "-")
+	if isNeg {
+		s = strings.TrimPrefix(s, "-")
 	}
-	re = regexp.MustCompile(`(-?\d+)([BKMG])`)
-	m = re.FindStringSubmatch(s)
-	if m != nil {
-		v, err := strconv.ParseInt(m[1], 10, 64)
-		if err != nil {
-			return 0, err
-		}
-		if v < 0 {
-			return -1, nil
-		}
-		if m[2] == "B" {
-			return v, nil
-		}
-		if m[2] == "K" {
-			return v * 1024, nil
-		}
-		if m[2] == "M" {
-			return v * 1024 * 1024, nil
-		}
-		if m[2] == "G" {
-			return v * 1024 * 1024 * 1024, nil
-		}
+	i, err := humanize.ParseBytes(s)
+	if err != nil {
+		return 0, err
+	}
+	if isNeg {
+		return -(int64)(i), nil
 	}
-	return 0, fmt.Errorf("couldn't parse number: %v", s)
+	return (int64)(i), nil
 }
 
 func UnixToDate(d int64) string {
diff --git a/cmd/editaccount.go b/cmd/editaccount.go
index 1c73d24..d281be0 100644
--- a/cmd/editaccount.go
+++ b/cmd/editaccount.go
@@ -65,10 +65,20 @@ func createEditAccount() *cobra.Command {
 	cmd.Flags().Int64VarP(&params.imports.NumberValue, "imports", "", -1, "set maximum number of imports for the account (-1 is unlimited)")
 	cmd.Flags().StringVarP(&params.payload.Value, "payload", "", "-1", "set maximum message payload in bytes for the account (-1 is unlimited)")
 	cmd.Flags().Int64VarP(&params.subscriptions.NumberValue, "subscriptions", "", -1, "set maximum subscription for the account (-1 is unlimited)")
-	cmd.Flags().StringVarP(&params.memStorage.Value, "mem-storage", "", "0", "set maximum memory storage in bytes for the account (-1 is unlimited / 0 disabled)")
-	cmd.Flags().StringVarP(&params.diskStorage.Value, "disk-storage", "", "0", "set maximum disk storage in bytes for the account (-1 is unlimited / 0 disabled)")
-	cmd.Flags().Int64VarP(&params.streams.NumberValue, "streams", "", -1, "set maximum streams for the account (-1 is unlimited)")
-	cmd.Flags().Int64VarP(&params.consumer.NumberValue, "consumer", "", -1, "set maximum consumer for the account (-1 is unlimited)")
+	// changed flag names by prefixing them with js-
+	// to replace them mark the old names as hidden (so using them does not break) and add new flags with correct name
+	cmd.Flags().StringVarP(&params.memStorage.Value, "mem-storage", "", "0", "")
+	cmd.Flags().StringVarP(&params.diskStorage.Value, "disk-storage", "", "0", "")
+	cmd.Flags().Int64VarP(&params.streams.NumberValue, "streams", "", -1, "")
+	cmd.Flags().Int64VarP(&params.consumer.NumberValue, "consumer", "", -1, "")
+	cmd.Flags().MarkHidden("mem-storage")
+	cmd.Flags().MarkHidden("disk-storage")
+	cmd.Flags().MarkHidden("streams")
+	cmd.Flags().MarkHidden("consumer")
+	cmd.Flags().StringVarP(&params.memStorage.Value, "js-mem-storage", "", "0", "Jetstream: set maximum memory storage in bytes for the account (-1 is unlimited / 0 disabled)")
+	cmd.Flags().StringVarP(&params.diskStorage.Value, "js-disk-storage", "", "0", "Jetstream: set maximum disk storage in bytes for the account (-1 is unlimited / 0 disabled)")
+	cmd.Flags().Int64VarP(&params.streams.NumberValue, "js-streams", "", -1, "Jetstream: set maximum streams for the account (-1 is unlimited)")
+	cmd.Flags().Int64VarP(&params.consumer.NumberValue, "js-consumer", "", -1, "Jetstream: set maximum consumer for the account (-1 is unlimited)")
 	cmd.Flags().BoolVarP(&params.exportsWc, "wildcard-exports", "", true, "exports can contain wildcards")
 	cmd.Flags().StringSliceVarP(&params.rmSigningKeys, "rm-sk", "", nil, "remove signing key - comma separated list or option can be specified multiple times")
 	cmd.Flags().StringVarP(&params.description, "description", "", "", "Description for this account")
@@ -122,7 +132,8 @@ func (p *EditAccountParams) SetDefaults(ctx ActionCtx) error {
 		"start", "expiry", "tag", "rm-tag", "conns", "leaf-conns", "exports", "imports", "subscriptions",
 		"payload", "data", "wildcard-exports", "sk", "rm-sk", "description", "info-url", "response-ttl", "allow-pub-response",
 		"allow-pub-response", "allow-pub", "allow-pubsub", "allow-sub", "deny-pub", "deny-pubsub", "deny-sub",
-		"rm-response-perms", "rm", "max-responses", "mem-storage", "disk-storage", "streams", "consumer") {
+		"rm-response-perms", "rm", "max-responses", "mem-storage", "disk-storage", "streams", "consumer",
+		"js-mem-storage", "js-disk-storage", "js-streams", "js-consumer") {
 		ctx.CurrentCmd().SilenceUsage = false
 		return fmt.Errorf("specify an edit option")
 	}
@@ -177,19 +188,19 @@ func (p *EditAccountParams) Load(ctx ActionCtx) error {
 		p.subscriptions.NumberValue = p.claim.Limits.Subs
 	}
 
-	if !ctx.CurrentCmd().Flags().Changed("mem-storage") {
+	if !(ctx.CurrentCmd().Flags().Changed("js-mem-storage") || ctx.CurrentCmd().Flags().Changed("mem-storage")) {
 		p.memStorage.Value = fmt.Sprintf("%d", p.claim.Limits.MemoryStorage)
 	}
 
-	if !ctx.CurrentCmd().Flags().Changed("disk-storage") {
+	if !(ctx.CurrentCmd().Flags().Changed("js-disk-storage") || ctx.CurrentCmd().Flags().Changed("disk-storage")) {
 		p.diskStorage.Value = fmt.Sprintf("%d", p.claim.Limits.DiskStorage)
 	}
 
-	if !ctx.CurrentCmd().Flags().Changed("streams") {
+	if !(ctx.CurrentCmd().Flags().Changed("js-streams") || ctx.CurrentCmd().Flags().Changed("streams")) {
 		p.streams.NumberValue = p.claim.Limits.Streams
 	}
 
-	if !ctx.CurrentCmd().Flags().Changed("consumer") {
+	if !(ctx.CurrentCmd().Flags().Changed("js-consumer") || ctx.CurrentCmd().Flags().Changed("consumer")) {
 		p.consumer.NumberValue = p.claim.Limits.Consumer
 	}
 


Test patch:
diff --git a/cmd/common_test.go b/cmd/common_test.go
index f3c08e7..0cd66d1 100644
--- a/cmd/common_test.go
+++ b/cmd/common_test.go
@@ -168,12 +168,18 @@ func TestCommon_ParseNumber(t *testing.T) {
 		{"", 0, false},
 		{"0", 0, false},
 		{"1000", 1000, false},
-		{"1K", 1024, false},
-		{"1k", 1024, false},
-		{"1M", 1024 * 1024, false},
-		{"1m", 1024 * 1024, false},
-		{"1G", 1024 * 1024 * 1024, false},
-		{"1g", 1024 * 1024 * 1024, false},
+		{"1K", 1000, false},
+		{"1k", 1000, false},
+		{"1M", 1000 * 1000, false},
+		{"1m", 1000 * 1000, false},
+		{"1G", 1000 * 1000 * 1000, false},
+		{"1g", 1000 * 1000 * 1000, false},
+		{"1KIB", 1024, false},
+		{"1kib", 1024, false},
+		{"1MIB", 1024 * 1024, false},
+		{"1mib", 1024 * 1024, false},
+		{"1GIB", 1024 * 1024 * 1024, false},
+		{"1gib", 1024 * 1024 * 1024, false},
 		{"32a", 0, true},
 	}
 	for _, d := range tests {
diff --git a/cmd/dataparams_test.go b/cmd/dataparams_test.go
index b624a79..be4b889 100644
--- a/cmd/dataparams_test.go
+++ b/cmd/dataparams_test.go
@@ -36,15 +36,30 @@ func TestDataParams(t *testing.T) {
 	dp.Value = "1K"
 	v, err = dp.NumberValue()
 	require.NoError(t, err)
+	require.Equal(t, int64(1000), v)
+
+	dp.Value = "1KiB"
+	v, err = dp.NumberValue()
+	require.NoError(t, err)
 	require.Equal(t, int64(1024), v)
 
 	dp.Value = "1M"
 	v, err = dp.NumberValue()
 	require.NoError(t, err)
+	require.Equal(t, int64(1000*1000), v)
+
+	dp.Value = "1MiB"
+	v, err = dp.NumberValue()
+	require.NoError(t, err)
 	require.Equal(t, int64(1024*1024), v)
 
 	dp.Value = "1G"
 	v, err = dp.NumberValue()
 	require.NoError(t, err)
+	require.Equal(t, int64(1000*1000*1000), v)
+
+	dp.Value = "1Gib"
+	v, err = dp.NumberValue()
+	require.NoError(t, err)
 	require.Equal(t, int64(1024*1024*1024), v)
 }
diff --git a/cmd/editaccount_test.go b/cmd/editaccount_test.go
index 80753ac..e87217d 100644
--- a/cmd/editaccount_test.go
+++ b/cmd/editaccount_test.go
@@ -114,8 +114,8 @@ func Test_EditAccountLimits(t *testing.T) {
 	defer ts.Done(t)
 
 	ts.AddAccount(t, "A")
-	_, _, err := ExecuteCmd(createEditAccount(), "--conns", "5", "--data", "10M", "--exports", "15",
-		"--imports", "20", "--payload", "1K", "--subscriptions", "30", "--leaf-conns", "31",
+	_, _, err := ExecuteCmd(createEditAccount(), "--conns", "5", "--data", "10mib", "--exports", "15",
+		"--imports", "20", "--payload", "1Kib", "--subscriptions", "30", "--leaf-conns", "31",
 		"--streams", "5", "--consumer", "6", "--disk-storage", "7", "--mem-storage", "8")
 	require.NoError(t, err)
 
diff --git a/cmd/edituser_test.go b/cmd/edituser_test.go
index a4778d2..592070e 100644
--- a/cmd/edituser_test.go
+++ b/cmd/edituser_test.go
@@ -498,7 +498,7 @@ func Test_EditUserData(t *testing.T) {
 	require.NoError(t, err)
 	require.Equal(t, int64(-1), uc.Limits.Data)
 
-	_, _, err = ExecuteCmd(HoistRootFlags(createEditUserCmd()), "--data", "1K")
+	_, _, err = ExecuteCmd(HoistRootFlags(createEditUserCmd()), "--data", "1Kib")
 	require.NoError(t, err)
 	uc, err = ts.Store.ReadUserClaim("A", "U")
 	require.NoError(t, err)