Skip to content

Commit

Permalink
fix(cmd): Add checks for unique wallet/ab name
Browse files Browse the repository at this point in the history
Also adds
- ab alias for addressbook,
- rm alias for remove,
- mv alias for rename.
  • Loading branch information
matevz committed May 10, 2023
1 parent 9678258 commit 6e9f6ad
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 11 deletions.
22 changes: 15 additions & 7 deletions cmd/addressbook.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ import (

var (
addressBookCmd = &cobra.Command{
Use: "addressbook",
Short: "Manage addresses in the local address book",
Use: "addressbook",
Aliases: []string{"ab"},
Short: "Manage addresses in the local address book",
}

abListCmd = &cobra.Command{
Expand Down Expand Up @@ -57,6 +58,9 @@ var (
name := args[0]
address := args[1]

if _, exists := cfg.Wallet.All[name]; exists {
cobra.CheckErr(fmt.Errorf("account '%s' already exists in the wallet", name))
}
err := cfg.AddressBook.Add(name, address)
cobra.CheckErr(err)

Expand Down Expand Up @@ -85,8 +89,8 @@ var (
}

abRmCmd = &cobra.Command{
Use: "rm <name>",
Aliases: []string{"remove"},
Use: "remove <name>",
Aliases: []string{"rm"},
Short: "Remove an address from address book",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
Expand All @@ -102,13 +106,17 @@ var (
}

abRenameCmd = &cobra.Command{
Use: "rename <old> <new>",
Short: "Rename address",
Args: cobra.ExactArgs(2),
Use: "rename <old> <new>",
Aliases: []string{"mv"},
Short: "Rename address",
Args: cobra.ExactArgs(2),
Run: func(cmd *cobra.Command, args []string) {
cfg := config.Global()
oldName, newName := args[0], args[1]

if _, exists := cfg.Wallet.All[newName]; exists {
cobra.CheckErr(fmt.Errorf("account '%s' already exists in the wallet", newName))
}
err := cfg.AddressBook.Rename(oldName, newName)
cobra.CheckErr(err)

Expand Down
3 changes: 3 additions & 0 deletions cmd/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,9 @@ var (
cfg := config.Global()
oldName, newName := args[0], args[1]

if _, exists := cfg.AddressBook.All[newName]; exists {
cobra.CheckErr(fmt.Errorf("address named '%s' already exists in the address book", newName))
}
err := cfg.Wallet.Rename(oldName, newName)
cobra.CheckErr(err)

Expand Down
4 changes: 2 additions & 2 deletions config/addressbook.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ func (ab *AddressBook) Remove(name string) error {
func (ab *AddressBook) Rename(old, new string) error {
cfg, exists := ab.All[old]
if !exists {
return fmt.Errorf("address named '%s' does not exist", old)
return fmt.Errorf("address named '%s' does not exist in the address book", old)
}

if _, exists = ab.All[new]; exists {
return fmt.Errorf("address named '%s' already exists", new)
return fmt.Errorf("address named '%s' already exists in the address book", new)
}

if err := config.ValidateIdentifier(old); err != nil {
Expand Down
4 changes: 2 additions & 2 deletions config/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,11 +159,11 @@ func (w *Wallet) Remove(name string) error {
func (w *Wallet) Rename(old, new string) error {
cfg, exists := w.All[old]
if !exists {
return fmt.Errorf("account '%s' does not exist", old)
return fmt.Errorf("account '%s' does not exist in the wallet", old)
}

if _, exists = w.All[new]; exists {
return fmt.Errorf("account '%s' already exists", new)
return fmt.Errorf("account '%s' already exists in the wallet", new)
}

if err := config.ValidateIdentifier(old); err != nil {
Expand Down

0 comments on commit 6e9f6ad

Please sign in to comment.