code.vegaprotocol.io/vega@v0.79.0/cmd/vegawallet/commands/network_delete.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  	vgterm "code.vegaprotocol.io/vega/libs/term"
    28  	"code.vegaprotocol.io/vega/paths"
    29  	"code.vegaprotocol.io/vega/wallet/api"
    30  	networkStore "code.vegaprotocol.io/vega/wallet/network/store/v1"
    31  
    32  	"github.com/spf13/cobra"
    33  )
    34  
    35  var (
    36  	deleteNetworkLong = cli.LongDesc(`
    37  	    Delete the specified network
    38  	`)
    39  
    40  	deleteNetworkExample = cli.Examples(`
    41  		# Delete the specified network
    42  		{{.Software}} network delete --network NETWORK
    43  
    44  		# Delete the specified network without asking for confirmation
    45  		{{.Software}} delete --wallet WALLET --force
    46  	`)
    47  )
    48  
    49  type RemoveNetworkHandler func(api.AdminRemoveNetworkParams) error
    50  
    51  func NewCmdDeleteNetwork(w io.Writer, rf *RootFlags) *cobra.Command {
    52  	h := func(params api.AdminRemoveNetworkParams) error {
    53  		vegaPaths := paths.New(rf.Home)
    54  
    55  		s, err := networkStore.InitialiseStore(vegaPaths)
    56  		if err != nil {
    57  			return fmt.Errorf("couldn't initialise network store: %w", err)
    58  		}
    59  
    60  		deleteNetwork := api.NewAdminRemoveNetwork(s)
    61  
    62  		_, errDetails := deleteNetwork.Handle(context.Background(), params)
    63  		if errDetails != nil {
    64  			return errors.New(errDetails.Data)
    65  		}
    66  		return nil
    67  	}
    68  
    69  	return BuildCmdDeleteNetwork(w, h, rf)
    70  }
    71  
    72  func BuildCmdDeleteNetwork(w io.Writer, handler RemoveNetworkHandler, rf *RootFlags) *cobra.Command {
    73  	f := &DeleteNetworkFlags{}
    74  	cmd := &cobra.Command{
    75  		Use:     "delete",
    76  		Short:   "Delete the specified network",
    77  		Long:    deleteNetworkLong,
    78  		Example: deleteNetworkExample,
    79  		RunE: func(_ *cobra.Command, _ []string) error {
    80  			req, err := f.Validate()
    81  			if err != nil {
    82  				return err
    83  			}
    84  
    85  			if !f.Force && vgterm.HasTTY() {
    86  				if !flags.AreYouSure() {
    87  					return nil
    88  				}
    89  			}
    90  
    91  			if err = handler(req); err != nil {
    92  				return err
    93  			}
    94  
    95  			if rf.Output == flags.InteractiveOutput {
    96  				PrintDeleteNetworkResponse(w, f.Network)
    97  			}
    98  
    99  			return nil
   100  		},
   101  	}
   102  
   103  	cmd.Flags().StringVarP(&f.Network,
   104  		"network", "n",
   105  		"",
   106  		"Network to delete",
   107  	)
   108  	cmd.Flags().BoolVarP(&f.Force,
   109  		"force", "f",
   110  		false,
   111  		"Do not ask for confirmation",
   112  	)
   113  
   114  	autoCompleteNetwork(cmd, rf.Home)
   115  
   116  	return cmd
   117  }
   118  
   119  type DeleteNetworkFlags struct {
   120  	Network string
   121  	Force   bool
   122  }
   123  
   124  func (f *DeleteNetworkFlags) Validate() (api.AdminRemoveNetworkParams, error) {
   125  	req := api.AdminRemoveNetworkParams{}
   126  
   127  	if len(f.Network) == 0 {
   128  		return api.AdminRemoveNetworkParams{}, flags.MustBeSpecifiedError("network")
   129  	}
   130  	req.Name = f.Network
   131  
   132  	return req, nil
   133  }
   134  
   135  func PrintDeleteNetworkResponse(w io.Writer, networkName string) {
   136  	p := printer.NewInteractivePrinter(w)
   137  
   138  	str := p.String()
   139  	defer p.Print(str)
   140  
   141  	str.CheckMark().SuccessText("Network ").SuccessBold(networkName).SuccessText(" deleted").NextLine()
   142  }