Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/nats-io/nsc
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephen Asbury committed Dec 6, 2018
2 parents 500564c + 4683e26 commit 9c7a9fd
Show file tree
Hide file tree
Showing 7 changed files with 164 additions and 20 deletions.
22 changes: 20 additions & 2 deletions cmd/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"time"

"github.com/dustin/go-humanize"
"github.com/mitchellh/go-homedir"
"github.com/nats-io/nkeys"
"github.com/nats-io/nsc/cli"
"github.com/nats-io/nsc/cmd/store"
Expand Down Expand Up @@ -216,7 +217,7 @@ func ParseNumber(s string) (int64, error) {
return 0, nil
}
s = strings.ToUpper(s)
re := regexp.MustCompile(`(\d+$)`)
re := regexp.MustCompile(`(-?\d+$)`)
m := re.FindStringSubmatch(s)
if m != nil {
v, err := strconv.ParseInt(m[0], 10, 64)
Expand All @@ -225,13 +226,19 @@ func ParseNumber(s string) (int64, error) {
}
return v, nil
}
re = regexp.MustCompile(`(\d+)([K|M|G])`)
re = regexp.MustCompile(`(-?\d+)([B|K|M|G])`)
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 * 1000, nil
}
Expand Down Expand Up @@ -339,3 +346,14 @@ func MaybeMakeDir(dir string) error {
}
return nil
}

func AbbrevHomePaths(fp string) string {
h, err := homedir.Dir()
if err != nil {
return fp
}
if strings.HasPrefix(fp, h) {
return strings.Replace(fp, h, "~", 1)
}
return fp
}
49 changes: 49 additions & 0 deletions cmd/dataparams.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright 2018 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 cmd

import (
"github.com/nats-io/nsc/cli"
)

type DataParams struct {
Value string
Number int64
}

func (e *DataParams) Valid() error {
// flag already insures this is a number
return nil
}

func (e *DataParams) Edit(prompt string) error {
var err error
var nv int64
sv, err := cli.Prompt(prompt, e.Value, true, func(s string) error {
nv, err = ParseNumber(s)
return err
})
if err != nil {
return err
}
e.Number = nv
e.Value = sv
return nil
}

func (e *DataParams) NumberValue() (int64, error) {
return ParseNumber(e.Value)
}
4 changes: 2 additions & 2 deletions cmd/describer.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (a *AccountDescriber) Describe() string {
}

if lim.Data > 0 {
table.AddRow("Max Data", humanize.Bytes(uint64(lim.Data)))
table.AddRow("Max Data", fmt.Sprintf("%s (%d bytes)", humanize.Bytes(uint64(lim.Data)), lim.Data))
} else {
table.AddRow("Max Data", "Unlimited")
}
Expand All @@ -75,7 +75,7 @@ func (a *AccountDescriber) Describe() string {
}

if lim.Payload > 0 {
table.AddRow("Max Msg Payload", humanize.Bytes(uint64(lim.Payload)))
table.AddRow("Max Msg Payload", fmt.Sprintf("%s (%d bytes)", humanize.Bytes(uint64(lim.Payload)), lim.Payload))
} else {
table.AddRow("Max Msg Payload", "Unlimited")
}
Expand Down
82 changes: 73 additions & 9 deletions cmd/editaccount.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,12 @@ func createEditAccount() *cobra.Command {
}
cmd.Flags().StringSliceVarP(&params.tags, "tag", "", nil, "add tags for user - comma separated list or option can be specified multiple times")
cmd.Flags().StringSliceVarP(&params.rmTags, "rm-tag", "", nil, "remove tag - comma separated list or option can be specified multiple times")
cmd.Flags().Int64VarP(&params.conns.NumberValue, "conns", "", 0, "set maximum active connections for the account")
cmd.Flags().Int64VarP(&params.conns.NumberValue, "conns", "", -1, "set maximum active connections for the account (-1 is unlimited)")
cmd.Flags().StringVarP(&params.data.Value, "data", "", "-1", "set maximum data in bytes for the account (-1 is unlimited)")
cmd.Flags().Int64VarP(&params.exports.NumberValue, "exports", "", -1, "set maximum number of exports for the account (-1 is unlimited)")
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)")

params.AccountContextParams.BindFlags(cmd)
params.TimeParams.BindFlags(cmd)
Expand All @@ -76,18 +81,23 @@ type EditAccountParams struct {
AccountContextParams
SignerParams
TimeParams
claim *jwt.AccountClaims
token string
tags []string
rmTags []string
conns NumberParams
claim *jwt.AccountClaims
token string
tags []string
rmTags []string
conns NumberParams
exports NumberParams
imports NumberParams
subscriptions NumberParams
payload DataParams
data DataParams
}

func (p *EditAccountParams) SetDefaults(ctx ActionCtx) error {
p.AccountContextParams.SetDefaults(ctx)
p.SignerParams.SetDefaults(nkeys.PrefixByteOperator, true, ctx)

if !InteractiveFlag && ctx.NothingToDo("start", "expiry", "tag", "rm-tag", "conns") {
if !InteractiveFlag && ctx.NothingToDo("start", "expiry", "tag", "rm-tag", "conns", "exports", "imports", "subscriptions", "payload", "data") {
ctx.CurrentCmd().SilenceUsage = false
return fmt.Errorf("specify an edit option")
}
Expand All @@ -113,6 +123,31 @@ func (p *EditAccountParams) Load(ctx ActionCtx) error {
if err != nil {
return err
}

if !ctx.CurrentCmd().Flags().Changed("conns") {
p.conns.NumberValue = p.claim.Limits.Conn
}

if !ctx.CurrentCmd().Flags().Changed("data") {
p.data.Value = fmt.Sprintf("%d", p.claim.Limits.Data)
}

if !ctx.CurrentCmd().Flags().Changed("exports") {
p.exports.NumberValue = p.claim.Limits.Exports
}

if !ctx.CurrentCmd().Flags().Changed("imports") {
p.imports.NumberValue = p.claim.Limits.Imports
}

if !ctx.CurrentCmd().Flags().Changed("payload") {
p.payload.Value = fmt.Sprintf("%d", p.claim.Limits.Payload)
}

if !ctx.CurrentCmd().Flags().Changed("subscriptions") {
p.subscriptions.NumberValue = p.claim.Limits.Subs
}

return err
}

Expand All @@ -122,6 +157,26 @@ func (p *EditAccountParams) PostInteractive(ctx ActionCtx) error {
return err
}

if err = p.data.Edit("max data"); err != nil {
return err
}

if err = p.exports.Edit("max exports (-1 unlimited)"); err != nil {
return err
}

if err = p.imports.Edit("max imports (-1 unlimited)"); err != nil {
return err
}

if err = p.payload.Edit("max payload (-1 unlimited)"); err != nil {
return err
}

if err = p.subscriptions.Edit("max subscriptions (-1 unlimited)"); err != nil {
return err
}

if err = p.TimeParams.Edit(); err != nil {
return err
}
Expand Down Expand Up @@ -157,9 +212,18 @@ func (p *EditAccountParams) Run(ctx ActionCtx) error {
p.claim.Tags.Remove(p.rmTags...)
sort.Strings(p.claim.Tags)

if p.conns.NumberValue > 0 {
p.claim.Limits.Conn = p.conns.NumberValue
p.claim.Limits.Conn = p.conns.NumberValue
p.claim.Limits.Data, err = p.data.NumberValue()
if err != nil {
return fmt.Errorf("error parsing %s: %s", "data", p.data.Value)
}
p.claim.Limits.Exports = p.exports.NumberValue
p.claim.Limits.Imports = p.imports.NumberValue
p.claim.Limits.Payload, err = p.payload.NumberValue()
if err != nil {
return fmt.Errorf("error parsing %s: %s", "payload", p.data.Value)
}
p.claim.Limits.Subs = p.subscriptions.NumberValue

p.token, err = p.claim.Encode(p.signerKP)
if err != nil {
Expand Down
13 changes: 9 additions & 4 deletions cmd/editaccount_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,17 +94,22 @@ func Test_EditAccount_Times(t *testing.T) {
require.Equal(t, expiry, ac.Expires)
}

func Test_EditAccountMaxConns(t *testing.T) {
func Test_EditAccountLimits(t *testing.T) {
ts := NewTestStore(t, "edit account")
defer ts.Done(t)

ts.AddAccount(t, "A")

_, _, err := ExecuteCmd(createEditAccount(), "--conns", "10")
_, _, err := ExecuteCmd(createEditAccount(), "--conns", "5", "--data", "10M", "--exports", "15",
"--imports", "20", "--payload", "1K", "--subscriptions", "30")
require.NoError(t, err)

ac, err := ts.Store.ReadAccountClaim("A")
require.NoError(t, err)
require.NotNil(t, ac)
require.Equal(t, int64(10), ac.Limits.Conn)
require.Equal(t, int64(5), ac.Limits.Conn)
require.Equal(t, int64(1000*1000*10), ac.Limits.Data)
require.Equal(t, int64(15), ac.Limits.Exports)
require.Equal(t, int64(20), ac.Limits.Imports)
require.Equal(t, int64(1000), ac.Limits.Payload)
require.Equal(t, int64(30), ac.Limits.Subs)
}
9 changes: 6 additions & 3 deletions cmd/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ func (p *SetContextParams) Run() error {
if err != nil {
return err
}

if p.Operator != "" {
if err := c.SetOperator(p.Operator); err != nil {
return err
Expand All @@ -98,6 +99,8 @@ func (p *SetContextParams) Run() error {
return err
}
}
c.SetDefaults()

current.ContextConfig = *c

return current.Save()
Expand All @@ -109,14 +112,14 @@ func (p *SetContextParams) PrintEnv(cmd *cobra.Command) {
table.UTF8Box()
table.AddTitle("NSC Environment")
table.AddHeaders("Setting", "Set", "Effective Value")
table.AddRow("$"+store.NKeysPathEnv, envSet(store.NKeysPathEnv), store.GetKeysDir())
table.AddRow("$"+homeEnv, envSet(homeEnv), toolHome)
table.AddRow("$"+store.NKeysPathEnv, envSet(store.NKeysPathEnv), AbbrevHomePaths(store.GetKeysDir()))
table.AddRow("$"+homeEnv, envSet(homeEnv), AbbrevHomePaths(toolHome))
table.AddSeparator()
r := conf.StoreRoot
if r == "" {
r = "Not Set"
}
table.AddRow("Stores Dir", "", r)
table.AddRow("Stores Dir", "", AbbrevHomePaths(r))
table.AddRow("Default Operator", "", conf.Operator)
table.AddRow("Default Account", "", conf.Account)
table.AddRow("Default Cluster", "", conf.Cluster)
Expand Down
5 changes: 5 additions & 0 deletions cmd/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package cmd

import (
"fmt"
"os"
"time"

"github.com/blang/semver"
Expand Down Expand Up @@ -60,6 +61,8 @@ update --release-notes

var latest *selfupdate.Release
if updateFn == nil {
// the library freak out if GITHUB_TOKEN is set - don't break travis :)
_ = os.Setenv("GITHUB_TOKEN", "")
latest, err = selfupdate.UpdateSelf(v, GetConfig().GithubUpdates)
} else {
latest, err = updateFn(v, GetConfig().GithubUpdates)
Expand Down Expand Up @@ -153,6 +156,8 @@ func (u *SelfUpdate) doCheck() (*semver.Version, error) {
var found bool
var err error
if updateCheckFn == nil {
// the library freak out if GITHUB_TOKEN is set - don't break travis :)
_ = os.Setenv("GITHUB_TOKEN", "")
latest, found, err = selfupdate.DetectLatest(config.GithubUpdates)
} else {
latest, found, err = updateCheckFn(config.GithubUpdates)
Expand Down

0 comments on commit 9c7a9fd

Please sign in to comment.