code.vegaprotocol.io/vega@v0.79.0/cmd/vegawallet/commands/network_list.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 listNetworkLong = cli.LongDesc(` 36 List all registered networks. 37 `) 38 39 listNetworkExample = cli.Examples(` 40 # List networks 41 {{.Software}} network list 42 `) 43 ) 44 45 type ListNetworksHandler func() (api.AdminListNetworksResult, error) 46 47 func NewCmdListNetworks(w io.Writer, rf *RootFlags) *cobra.Command { 48 h := func() (api.AdminListNetworksResult, error) { 49 vegaPaths := paths.New(rf.Home) 50 51 networkStore, err := networkStoreV1.InitialiseStore(vegaPaths) 52 if err != nil { 53 return api.AdminListNetworksResult{}, fmt.Errorf("couldn't initialise network store: %w", err) 54 } 55 56 listWallet := api.NewAdminListNetworks(networkStore) 57 rawResult, errorDetails := listWallet.Handle(context.Background(), nil) 58 if errorDetails != nil { 59 return api.AdminListNetworksResult{}, errors.New(errorDetails.Data) 60 } 61 return rawResult.(api.AdminListNetworksResult), nil 62 } 63 64 return BuildCmdListNetworks(w, h, rf) 65 } 66 67 func BuildCmdListNetworks(w io.Writer, handler ListNetworksHandler, rf *RootFlags) *cobra.Command { 68 cmd := &cobra.Command{ 69 Use: "list", 70 Short: "List all registered networks", 71 Long: listNetworkLong, 72 Example: listNetworkExample, 73 RunE: func(_ *cobra.Command, _ []string) error { 74 resp, err := handler() 75 if err != nil { 76 return err 77 } 78 79 switch rf.Output { 80 case flags.InteractiveOutput: 81 PrintListNetworksResult(w, resp) 82 case flags.JSONOutput: 83 return printer.FprintJSON(w, resp) 84 } 85 86 return nil 87 }, 88 } 89 90 return cmd 91 } 92 93 func PrintListNetworksResult(w io.Writer, resp api.AdminListNetworksResult) { 94 p := printer.NewInteractivePrinter(w) 95 96 str := p.String() 97 defer p.Print(str) 98 99 if len(resp.Networks) == 0 { 100 str.InfoText("No network registered").NextLine() 101 return 102 } 103 104 for i, net := range resp.Networks { 105 str.WarningText(net.Name).NextLine() 106 if len(net.Metadata) > 0 { 107 for j, metadata := range net.Metadata { 108 str.Text(metadata.Key).Text(": ").Text(metadata.Value) 109 if j < len(net.Metadata)-1 { 110 str.Text(", ") 111 } 112 } 113 } else { 114 str.Text("<no metadata>") 115 } 116 if i < len(resp.Networks)-1 { 117 str.NextSection() 118 } else { 119 str.NextLine() 120 } 121 } 122 }