code.vegaprotocol.io/vega@v0.79.0/cmd/vegawallet/commands/wallet_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/wallet/api" 28 "code.vegaprotocol.io/vega/wallet/wallets" 29 30 "github.com/spf13/cobra" 31 ) 32 33 var ( 34 listWalletsLong = cli.LongDesc(` 35 List all registered wallets. 36 `) 37 38 listWalletsExample = cli.Examples(` 39 # List all registered wallets 40 {{.Software}} list 41 `) 42 ) 43 44 type ListWalletsHandler func() (api.AdminListWalletsResult, error) 45 46 func NewCmdListWallets(w io.Writer, rf *RootFlags) *cobra.Command { 47 h := func() (api.AdminListWalletsResult, error) { 48 walletStore, err := wallets.InitialiseStore(rf.Home, false) 49 if err != nil { 50 return api.AdminListWalletsResult{}, fmt.Errorf("couldn't initialise wallets store: %w", err) 51 } 52 defer walletStore.Close() 53 54 listWallet := api.NewAdminListWallets(walletStore) 55 rawResult, errorDetails := listWallet.Handle(context.Background(), nil) 56 if errorDetails != nil { 57 return api.AdminListWalletsResult{}, errors.New(errorDetails.Data) 58 } 59 return rawResult.(api.AdminListWalletsResult), nil 60 } 61 62 return BuildCmdListWallets(w, h, rf) 63 } 64 65 func BuildCmdListWallets(w io.Writer, handler ListWalletsHandler, rf *RootFlags) *cobra.Command { 66 cmd := &cobra.Command{ 67 Use: "list", 68 Short: "List all registered wallets", 69 Long: listWalletsLong, 70 Example: listWalletsExample, 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 PrintListWalletsResult(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 PrintListWalletsResult(w io.Writer, resp api.AdminListWalletsResult) { 92 p := printer.NewInteractivePrinter(w) 93 94 str := p.String() 95 defer p.Print(str) 96 97 if len(resp.Wallets) == 0 { 98 str.InfoText("No wallet registered").NextLine() 99 return 100 } 101 102 for _, w := range resp.Wallets { 103 str.Text(fmt.Sprintf("- %s", w)).NextLine() 104 } 105 }