code.vegaprotocol.io/vega@v0.79.0/cmd/vegawallet/commands/network_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/paths"
    28  	"code.vegaprotocol.io/vega/wallet/api"
    29  	networkStore "code.vegaprotocol.io/vega/wallet/network/store/v1"
    30  
    31  	"github.com/spf13/cobra"
    32  )
    33  
    34  var (
    35  	renameNetworkLong = cli.LongDesc(`
    36  	    Rename the network with the specified name.
    37  	`)
    38  
    39  	renameNetworkExample = cli.Examples(`
    40  		# Rename the specified network
    41  		{{.Software}} network rename --network NETWORK --new-name NEW_NETWORK_NAME
    42  	`)
    43  )
    44  
    45  type RenameNetworkHandler func(api.AdminRenameNetworkParams) error
    46  
    47  func NewCmdRenameNetwork(w io.Writer, rf *RootFlags) *cobra.Command {
    48  	h := func(params api.AdminRenameNetworkParams) error {
    49  		vegaPaths := paths.New(rf.Home)
    50  
    51  		s, err := networkStore.InitialiseStore(vegaPaths)
    52  		if err != nil {
    53  			return fmt.Errorf("couldn't initialise network store: %w", err)
    54  		}
    55  
    56  		renameNetwork := api.NewAdminRenameNetwork(s)
    57  
    58  		_, errDetails := renameNetwork.Handle(context.Background(), params)
    59  		if errDetails != nil {
    60  			return errors.New(errDetails.Data)
    61  		}
    62  		return nil
    63  	}
    64  
    65  	return BuildCmdRenameNetwork(w, h, rf)
    66  }
    67  
    68  func BuildCmdRenameNetwork(w io.Writer, handler RenameNetworkHandler, rf *RootFlags) *cobra.Command {
    69  	f := &RenameNetworkFlags{}
    70  	cmd := &cobra.Command{
    71  		Use:     "rename",
    72  		Short:   "Rename the specified network",
    73  		Long:    renameNetworkLong,
    74  		Example: renameNetworkExample,
    75  		RunE: func(_ *cobra.Command, _ []string) error {
    76  			req, err := f.Validate()
    77  			if err != nil {
    78  				return err
    79  			}
    80  
    81  			if err = handler(req); err != nil {
    82  				return err
    83  			}
    84  
    85  			switch rf.Output {
    86  			case flags.InteractiveOutput:
    87  				PrintRenameNetworkResponse(w, f)
    88  			case flags.JSONOutput:
    89  				return nil
    90  			}
    91  
    92  			return nil
    93  		},
    94  	}
    95  
    96  	cmd.Flags().StringVarP(&f.Network,
    97  		"network", "n",
    98  		"",
    99  		"Network to rename",
   100  	)
   101  	cmd.Flags().StringVar(&f.NewName,
   102  		"new-name",
   103  		"",
   104  		"New name for the network",
   105  	)
   106  
   107  	autoCompleteNetwork(cmd, rf.Home)
   108  
   109  	return cmd
   110  }
   111  
   112  type RenameNetworkFlags struct {
   113  	Network string
   114  	NewName string
   115  }
   116  
   117  func (f *RenameNetworkFlags) Validate() (api.AdminRenameNetworkParams, error) {
   118  	if len(f.Network) == 0 {
   119  		return api.AdminRenameNetworkParams{}, flags.MustBeSpecifiedError("network")
   120  	}
   121  
   122  	if len(f.NewName) == 0 {
   123  		return api.AdminRenameNetworkParams{}, flags.MustBeSpecifiedError("new-name")
   124  	}
   125  
   126  	return api.AdminRenameNetworkParams{
   127  		Network: f.Network,
   128  		NewName: f.NewName,
   129  	}, nil
   130  }
   131  
   132  func PrintRenameNetworkResponse(w io.Writer, f *RenameNetworkFlags) {
   133  	p := printer.NewInteractivePrinter(w)
   134  
   135  	str := p.String()
   136  	defer p.Print(str)
   137  
   138  	str.CheckMark().SuccessText("Network ").SuccessBold(f.Network).SuccessText(" has been renamed to ").SuccessBold(f.NewName).SuccessText(".").NextLine()
   139  }