code.vegaprotocol.io/vega@v0.79.0/cmd/vegawallet/commands/wallet_rename.go (about)

     1  // Copyright (C) 2023 Gobalsky Labs Limited
     2  //
     3  // This program is free software: you can redistribute it and/or modify
     4  // it under the terms of the GNU Affero General Public License as
     5  // published by the Free Software Foundation, either version 3 of the
     6  // License, or (at your option) any later version.
     7  //
     8  // This program is distributed in the hope that it will be useful,
     9  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    10  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    11  // GNU Affero General Public License for more details.
    12  //
    13  // You should have received a copy of the GNU Affero General Public License
    14  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    15  
    16  package cmd
    17  
    18  import (
    19  	"context"
    20  	"errors"
    21  	"fmt"
    22  	"io"
    23  
    24  	"code.vegaprotocol.io/vega/cmd/vegawallet/commands/cli"
    25  	"code.vegaprotocol.io/vega/cmd/vegawallet/commands/flags"
    26  	"code.vegaprotocol.io/vega/cmd/vegawallet/commands/printer"
    27  	"code.vegaprotocol.io/vega/wallet/api"
    28  	"code.vegaprotocol.io/vega/wallet/wallets"
    29  
    30  	"github.com/spf13/cobra"
    31  )
    32  
    33  var (
    34  	renameWalletLong = cli.LongDesc(`
    35  		Rename the wallet with the specified name.
    36  	`)
    37  
    38  	renameWalletExample = cli.Examples(`
    39  		# Rename the specified wallet
    40  		{{.Software}} rename --wallet WALLET --new-name NEW_WALLET_NAME
    41  	`)
    42  )
    43  
    44  type RenameWalletHandler func(api.AdminRenameWalletParams) error
    45  
    46  func NewCmdRenameWallet(w io.Writer, rf *RootFlags) *cobra.Command {
    47  	h := func(params api.AdminRenameWalletParams) error {
    48  		walletStore, err := wallets.InitialiseStore(rf.Home, false)
    49  		if err != nil {
    50  			return fmt.Errorf("couldn't initialise wallets store: %w", err)
    51  		}
    52  		defer walletStore.Close()
    53  
    54  		renameWallet := api.NewAdminRenameWallet(walletStore)
    55  
    56  		_, errDetails := renameWallet.Handle(context.Background(), params)
    57  		if errDetails != nil {
    58  			return errors.New(errDetails.Data)
    59  		}
    60  		return nil
    61  	}
    62  
    63  	return BuildCmdRenameWallet(w, h, rf)
    64  }
    65  
    66  func BuildCmdRenameWallet(w io.Writer, handler RenameWalletHandler, rf *RootFlags) *cobra.Command {
    67  	f := &RenameWalletFlags{}
    68  
    69  	cmd := &cobra.Command{
    70  		Use:     "rename",
    71  		Short:   "Rename the specified wallet",
    72  		Long:    renameWalletLong,
    73  		Example: renameWalletExample,
    74  		RunE: func(_ *cobra.Command, _ []string) error {
    75  			params, err := f.Validate()
    76  			if err != nil {
    77  				return err
    78  			}
    79  
    80  			if err := handler(params); err != nil {
    81  				return err
    82  			}
    83  
    84  			switch rf.Output {
    85  			case flags.InteractiveOutput:
    86  				PrintRenameWalletResponse(w, f)
    87  			case flags.JSONOutput:
    88  				return nil
    89  			}
    90  
    91  			return nil
    92  		},
    93  	}
    94  
    95  	cmd.Flags().StringVarP(&f.Wallet,
    96  		"wallet", "w",
    97  		"",
    98  		"Wallet to rename",
    99  	)
   100  	cmd.Flags().StringVar(&f.NewName,
   101  		"new-name",
   102  		"",
   103  		"New name for the wallet",
   104  	)
   105  
   106  	autoCompleteWallet(cmd, rf.Home, "wallet")
   107  
   108  	return cmd
   109  }
   110  
   111  type RenameWalletFlags struct {
   112  	Wallet  string
   113  	NewName string
   114  }
   115  
   116  func (f *RenameWalletFlags) Validate() (api.AdminRenameWalletParams, error) {
   117  	if len(f.Wallet) == 0 {
   118  		return api.AdminRenameWalletParams{}, flags.MustBeSpecifiedError("wallet")
   119  	}
   120  
   121  	if len(f.NewName) == 0 {
   122  		return api.AdminRenameWalletParams{}, flags.MustBeSpecifiedError("new-name")
   123  	}
   124  
   125  	return api.AdminRenameWalletParams{
   126  		Wallet:  f.Wallet,
   127  		NewName: f.NewName,
   128  	}, nil
   129  }
   130  
   131  func PrintRenameWalletResponse(w io.Writer, f *RenameWalletFlags) {
   132  	p := printer.NewInteractivePrinter(w)
   133  
   134  	str := p.String()
   135  	defer p.Print(str)
   136  
   137  	str.CheckMark().SuccessText("The wallet ").SuccessBold(f.Wallet).SuccessText(" has been renamed to ").SuccessBold(f.NewName).SuccessText(".").NextLine()
   138  }