Skip to content

Commit

Permalink
[FIX] remove some ui deps from store (#625)
Browse files Browse the repository at this point in the history
  • Loading branch information
aricart authored Nov 16, 2023
1 parent 98665d7 commit 235a2cc
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 73 deletions.
2 changes: 1 addition & 1 deletion cmd/accountcontextparams.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
71 changes: 71 additions & 0 deletions cmd/context_ui_utils.go
Original file line number Diff line number Diff line change
@@ -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
}
2 changes: 1 addition & 1 deletion cmd/describeuser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/editoperator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/edituser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/generatecreds.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
65 changes: 0 additions & 65 deletions cmd/store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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)
Expand All @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion cmd/usercontextparams.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit 235a2cc

Please sign in to comment.