diff --git a/cmd/accountcontextparams.go b/cmd/accountcontextparams.go index f785a4c8..f5c6a547 100644 --- a/cmd/accountcontextparams.go +++ b/cmd/accountcontextparams.go @@ -55,7 +55,7 @@ func (p *AccountContextParams) SetDefaults(ctx ActionCtx) error { func (p *AccountContextParams) Edit(ctx ActionCtx) error { var err error - name, err := ctx.StoreCtx().PickAccount(p.Name) + name, err := PickAccount(ctx.StoreCtx(), p.Name) if err != nil { return err } diff --git a/cmd/context_ui_utils.go b/cmd/context_ui_utils.go new file mode 100644 index 00000000..eba7f499 --- /dev/null +++ b/cmd/context_ui_utils.go @@ -0,0 +1,71 @@ +package cmd + +import ( + "fmt" + cli "github.com/nats-io/cliprompts/v2" + "github.com/nats-io/nsc/v2/cmd/store" +) + +func PickAccount(ctx *store.Context, name string) (string, error) { + if name == "" { + name = ctx.Account.Name + } + + accounts, err := ctx.Store.ListSubContainers(store.Accounts) + if err != nil { + return "", err + } + if len(accounts) == 0 { + return "", fmt.Errorf("no accounts defined - add one first") + } + if len(accounts) == 1 { + name = accounts[0] + } + if len(accounts) > 1 { + i, err := cli.Select("select account", name, accounts) + if err != nil { + return "", err + } + name = accounts[i] + } + + // allow downstream use of context to have account + ctx.Account.Name = name + + return name, nil +} + +func PickUser(ctx *store.Context, accountName string) (string, error) { + var err error + if accountName == "" { + accountName = ctx.Account.Name + } + + if accountName == "" { + accountName, err = PickAccount(ctx, accountName) + if err != nil { + return "", err + } + } + // allow downstream use of context to have account + ctx.Account.Name = accountName + + users, err := ctx.Store.ListEntries(store.Accounts, accountName, store.Users) + if err != nil { + return "", err + } + if len(users) == 0 { + return "", fmt.Errorf("account %q doesn't have any users - add one first", accountName) + } + if len(users) == 1 { + return users[0], nil + } + if len(users) > 1 { + i, err := cli.Select("select user", "", users) + if err != nil { + return "", err + } + return users[i], nil + } + return "", nil +} diff --git a/cmd/describeuser.go b/cmd/describeuser.go index ac7d7c7e..e1abc181 100644 --- a/cmd/describeuser.go +++ b/cmd/describeuser.go @@ -66,7 +66,7 @@ func (p *DescribeUserParams) PreInteractive(ctx ActionCtx) error { return err } if p.user == "" { - p.user, err = ctx.StoreCtx().PickUser(p.AccountContextParams.Name) + p.user, err = PickUser(ctx.StoreCtx(), p.AccountContextParams.Name) if err != nil { return err } diff --git a/cmd/editoperator.go b/cmd/editoperator.go index b67467a8..548067f8 100644 --- a/cmd/editoperator.go +++ b/cmd/editoperator.go @@ -176,7 +176,7 @@ func (p *EditOperatorParams) PostInteractive(ctx ActionCtx) error { if ok, err := cli.Confirm("Set system account", false); err != nil { return err } else if ok { - p.sysAcc, err = ctx.StoreCtx().PickAccount("") + p.sysAcc, err = PickAccount(ctx.StoreCtx(), "") if err != nil { return err } diff --git a/cmd/edituser.go b/cmd/edituser.go index 24c31707..9269ec6e 100644 --- a/cmd/edituser.go +++ b/cmd/edituser.go @@ -119,7 +119,7 @@ func (p *EditUserParams) PreInteractive(ctx ActionCtx) error { } if p.name == "" { - p.name, err = ctx.StoreCtx().PickUser(p.AccountContextParams.Name) + p.name, err = PickUser(ctx.StoreCtx(), p.AccountContextParams.Name) if err != nil { return err } diff --git a/cmd/generatecreds.go b/cmd/generatecreds.go index 5c2e6238..34da395b 100644 --- a/cmd/generatecreds.go +++ b/cmd/generatecreds.go @@ -88,7 +88,7 @@ func (p *GenerateCredsParams) PreInteractive(ctx ActionCtx) error { if err = p.AccountContextParams.Edit(ctx); err != nil { return err } - p.user, err = ctx.StoreCtx().PickUser(p.AccountContextParams.Name) + p.user, err = PickUser(ctx.StoreCtx(), p.AccountContextParams.Name) if err != nil { return err } diff --git a/cmd/push.go b/cmd/push.go index d4f902b8..c5506fa4 100644 --- a/cmd/push.go +++ b/cmd/push.go @@ -273,12 +273,12 @@ func (p *PushCmdParams) PreInteractive(ctx ActionCtx) error { } if IsResolverURL(p.ASU) { if p.sysAcc == "" { - if p.sysAcc, err = ctx.StoreCtx().PickAccount(p.sysAcc); err != nil { + if p.sysAcc, err = PickAccount(ctx.StoreCtx(), p.sysAcc); err != nil { return err } } if p.sysAccUser == "" { - p.sysAccUser, err = ctx.StoreCtx().PickUser(p.sysAcc) + p.sysAccUser, err = PickUser(ctx.StoreCtx(), p.sysAcc) } } return err diff --git a/cmd/store/store.go b/cmd/store/store.go index 57709c3a..d353c5bf 100644 --- a/cmd/store/store.go +++ b/cmd/store/store.go @@ -32,7 +32,6 @@ import ( "sync" "time" - cli "github.com/nats-io/cliprompts/v2" "github.com/nats-io/jwt/v2" "github.com/nats-io/nkeys" ) @@ -875,35 +874,6 @@ func (ctx *Context) ResolveKey(flagValue string, kinds ...nkeys.PrefixByte) (nke return kp, err } -func (ctx *Context) PickAccount(name string) (string, error) { - if name == "" { - name = ctx.Account.Name - } - - accounts, err := ctx.Store.ListSubContainers(Accounts) - if err != nil { - return "", err - } - if len(accounts) == 0 { - return "", fmt.Errorf("no accounts defined - add one first") - } - if len(accounts) == 1 { - name = accounts[0] - } - if len(accounts) > 1 { - i, err := cli.Select("select account", name, accounts) - if err != nil { - return "", err - } - name = accounts[i] - } - - // allow downstream use of context to have account - ctx.Account.Name = name - - return name, nil -} - // Returns an user name for the account if there's only one user func (ctx *Context) DefaultUser(accountName string) *string { users, err := ctx.Store.ListEntries(Accounts, accountName, Users) @@ -928,41 +898,6 @@ func (ctx *Context) DefaultUserClaim(accountName string) (*jwt.UserClaims, error return nil, fmt.Errorf("no default user available for account %s", accountName) } -func (ctx *Context) PickUser(accountName string) (string, error) { - var err error - if accountName == "" { - accountName = ctx.Account.Name - } - - if accountName == "" { - accountName, err = ctx.PickAccount(accountName) - if err != nil { - return "", err - } - } - // allow downstream use of context to have account - ctx.Account.Name = accountName - - users, err := ctx.Store.ListEntries(Accounts, accountName, Users) - if err != nil { - return "", err - } - if len(users) == 0 { - return "", fmt.Errorf("account %q doesn't have any users - add one first", accountName) - } - if len(users) == 1 { - return users[0], nil - } - if len(users) > 1 { - i, err := cli.Select("select user", "", users) - if err != nil { - return "", err - } - return users[i], nil - } - return "", nil -} - // GetAccountKeys returns the public keys for the named account followed // by its signing keys func (ctx *Context) GetAccountKeys(name string) ([]string, error) { diff --git a/cmd/usercontextparams.go b/cmd/usercontextparams.go index 2fd143d2..12ea261a 100644 --- a/cmd/usercontextparams.go +++ b/cmd/usercontextparams.go @@ -59,7 +59,7 @@ func (p *UserContextParams) SetDefaults(ctx ActionCtx) error { func (p *UserContextParams) Edit(ctx ActionCtx) error { config := GetConfig() var err error - p.Name, err = ctx.StoreCtx().PickUser(config.Account) + p.Name, err = PickUser(ctx.StoreCtx(), config.Account) if err != nil { return err }