code.vegaprotocol.io/vega@v0.79.0/cmd/vegawallet/commands/network_locate.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  	"fmt"
    20  	"io"
    21  
    22  	"code.vegaprotocol.io/vega/cmd/vegawallet/commands/cli"
    23  	"code.vegaprotocol.io/vega/cmd/vegawallet/commands/flags"
    24  	"code.vegaprotocol.io/vega/cmd/vegawallet/commands/printer"
    25  	"code.vegaprotocol.io/vega/paths"
    26  	netstore "code.vegaprotocol.io/vega/wallet/network/store/v1"
    27  
    28  	"github.com/spf13/cobra"
    29  )
    30  
    31  var (
    32  	locateNetworkLong = cli.LongDesc(`
    33  		Locate the folder in which all the network configuration files are stored.
    34  	`)
    35  
    36  	locateNetworkExample = cli.Examples(`
    37  		# Locate network configuration files
    38  		{{.Software}} network locate
    39  	`)
    40  )
    41  
    42  type LocateNetworksResponse struct {
    43  	Path string `json:"path"`
    44  }
    45  
    46  type LocateNetworksHandler func() (*LocateNetworksResponse, error)
    47  
    48  func NewCmdLocateNetworks(w io.Writer, rf *RootFlags) *cobra.Command {
    49  	h := func() (*LocateNetworksResponse, error) {
    50  		vegaPaths := paths.New(rf.Home)
    51  
    52  		netStore, err := netstore.InitialiseStore(vegaPaths)
    53  		if err != nil {
    54  			return nil, fmt.Errorf("couldn't initialise networks store: %w", err)
    55  		}
    56  
    57  		return &LocateNetworksResponse{
    58  			Path: netStore.GetNetworksPath(),
    59  		}, nil
    60  	}
    61  
    62  	return BuildCmdLocateNetworks(w, h, rf)
    63  }
    64  
    65  func BuildCmdLocateNetworks(w io.Writer, handler LocateNetworksHandler, rf *RootFlags) *cobra.Command {
    66  	cmd := &cobra.Command{
    67  		Use:     "locate",
    68  		Short:   "Locate the folder of network configuration files",
    69  		Long:    locateNetworkLong,
    70  		Example: locateNetworkExample,
    71  		RunE: func(_ *cobra.Command, _ []string) error {
    72  			resp, err := handler()
    73  			if err != nil {
    74  				return err
    75  			}
    76  
    77  			switch rf.Output {
    78  			case flags.InteractiveOutput:
    79  				PrintLocateNetworksResponse(w, resp)
    80  			case flags.JSONOutput:
    81  				return printer.FprintJSON(w, resp)
    82  			}
    83  
    84  			return nil
    85  		},
    86  	}
    87  
    88  	return cmd
    89  }
    90  
    91  func PrintLocateNetworksResponse(w io.Writer, resp *LocateNetworksResponse) {
    92  	p := printer.NewInteractivePrinter(w)
    93  
    94  	str := p.String()
    95  	defer p.Print(str)
    96  
    97  	str.Text("Network configuration files are located at: ").SuccessText(resp.Path).NextLine()
    98  }