code.vegaprotocol.io/vega@v0.79.0/cmd/vegawallet/commands/network_describe.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  	networkStoreV1 "code.vegaprotocol.io/vega/wallet/network/store/v1"
    30  
    31  	"github.com/spf13/cobra"
    32  )
    33  
    34  var (
    35  	describeNetworkLong = cli.LongDesc(`
    36  	    Describe all known information about the specified network.
    37  	`)
    38  
    39  	describeNetworkExample = cli.Examples(`
    40  		# Describe a network
    41  		{{.Software}} network describe --network NETWORK
    42  	`)
    43  )
    44  
    45  type DescribeNetworkHandler func(api.AdminDescribeNetworkParams) (api.AdminNetwork, error)
    46  
    47  func NewCmdDescribeNetwork(w io.Writer, rf *RootFlags) *cobra.Command {
    48  	h := func(params api.AdminDescribeNetworkParams) (api.AdminNetwork, error) {
    49  		vegaPaths := paths.New(rf.Home)
    50  
    51  		networkStore, err := networkStoreV1.InitialiseStore(vegaPaths)
    52  		if err != nil {
    53  			return api.AdminNetwork{}, fmt.Errorf("couldn't initialise network store: %w", err)
    54  		}
    55  
    56  		describeNetwork := api.NewAdminDescribeNetwork(networkStore)
    57  		rawResult, errorDetails := describeNetwork.Handle(context.Background(), params)
    58  		if errorDetails != nil {
    59  			return api.AdminNetwork{}, errors.New(errorDetails.Data)
    60  		}
    61  		return rawResult.(api.AdminNetwork), nil
    62  	}
    63  
    64  	return BuildCmdDescribeNetwork(w, h, rf)
    65  }
    66  
    67  type DescribeNetworkFlags struct {
    68  	Network string
    69  }
    70  
    71  func (f *DescribeNetworkFlags) Validate() (api.AdminDescribeNetworkParams, error) {
    72  	req := api.AdminDescribeNetworkParams{}
    73  
    74  	if len(f.Network) == 0 {
    75  		return api.AdminDescribeNetworkParams{}, flags.MustBeSpecifiedError("network")
    76  	}
    77  	req.Name = f.Network
    78  
    79  	return req, nil
    80  }
    81  
    82  func BuildCmdDescribeNetwork(w io.Writer, handler DescribeNetworkHandler, rf *RootFlags) *cobra.Command {
    83  	f := &DescribeNetworkFlags{}
    84  	cmd := &cobra.Command{
    85  		Use:     "describe",
    86  		Short:   "Describe the specified network",
    87  		Long:    describeNetworkLong,
    88  		Example: describeNetworkExample,
    89  		RunE: func(_ *cobra.Command, _ []string) error {
    90  			req, err := f.Validate()
    91  			if err != nil {
    92  				return err
    93  			}
    94  			resp, err := handler(req)
    95  			if err != nil {
    96  				return err
    97  			}
    98  
    99  			switch rf.Output {
   100  			case flags.InteractiveOutput:
   101  				PrintDescribeNetworkResponse(w, resp)
   102  			case flags.JSONOutput:
   103  				return printer.FprintJSON(w, resp)
   104  			}
   105  
   106  			return nil
   107  		},
   108  	}
   109  
   110  	cmd.Flags().StringVarP(&f.Network,
   111  		"network", "n",
   112  		"",
   113  		"Network to describe",
   114  	)
   115  
   116  	autoCompleteNetwork(cmd, rf.Home)
   117  
   118  	return cmd
   119  }
   120  
   121  func PrintDescribeNetworkResponse(w io.Writer, resp api.AdminNetwork) {
   122  	p := printer.NewInteractivePrinter(w)
   123  
   124  	str := p.String()
   125  	defer p.Print(str)
   126  
   127  	str.NextLine().Text("Network").NextLine()
   128  	str.Text("  Name: ").WarningText(resp.Name).NextLine()
   129  	str.Text("  Metadata: ")
   130  	if len(resp.Metadata) > 0 {
   131  		str.NextLine()
   132  		padding := 0
   133  		for _, m := range resp.Metadata {
   134  			keyLen := len(m.Key)
   135  			if keyLen > padding {
   136  				padding = keyLen
   137  			}
   138  		}
   139  
   140  		for _, m := range resp.Metadata {
   141  			str.ListItem().WarningText(fmt.Sprintf("%-*s", padding, m.Key)).Text(" | ").WarningText(m.Value).NextLine()
   142  		}
   143  		str.NextLine()
   144  	} else {
   145  		str.DangerText(" <not set>").NextSection()
   146  	}
   147  
   148  	str.NextLine().Text("Linked applications").NextLine()
   149  	str.ListItem().Text("- Console: ")
   150  	PrintDescribeNetworkWithValueNotSet(str, resp.Apps.Console)
   151  	str.ListItem().Text("- Governance: ")
   152  	PrintDescribeNetworkWithValueNotSet(str, resp.Apps.Governance)
   153  	str.ListItem().Text("- Explorer: ")
   154  	PrintDescribeNetworkWithValueNotSet(str, resp.Apps.Explorer)
   155  	str.NextSection()
   156  
   157  	str.Text("API.GRPC").NextLine()
   158  	str.Text("  Hosts:")
   159  	PrintDescribeNetworkWithValuesNotSet(str, resp.API.GRPC.Hosts)
   160  	str.NextLine()
   161  
   162  	str.Text("API.REST").NextLine()
   163  	str.Text("  Hosts:")
   164  	PrintDescribeNetworkWithValuesNotSet(str, resp.API.REST.Hosts)
   165  	str.NextLine()
   166  
   167  	str.Text("API.GraphQL").NextLine()
   168  	str.Text("  Hosts:")
   169  	PrintDescribeNetworkWithValuesNotSet(str, resp.API.GraphQL.Hosts)
   170  	str.NextLine()
   171  }
   172  
   173  func PrintDescribeNetworkWithValueNotSet(str *printer.FormattedString, value string) {
   174  	if value == "" {
   175  		str.DangerText("<not set>")
   176  	} else {
   177  		str.WarningText(value)
   178  	}
   179  	str.NextLine()
   180  }
   181  
   182  func PrintDescribeNetworkWithValuesNotSet(str *printer.FormattedString, hosts []string) {
   183  	if len(hosts) == 0 {
   184  		str.DangerText(" <not set>").NextLine()
   185  		return
   186  	}
   187  
   188  	str.NextLine()
   189  	for _, h := range hosts {
   190  		str.ListItem().Text("- ").WarningText(h).NextLine()
   191  	}
   192  }