code.vegaprotocol.io/vega@v0.79.0/cmd/vegawallet/commands/cmd.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 "os" 24 "time" 25 26 "code.vegaprotocol.io/vega/cmd/vegawallet/commands/flags" 27 "code.vegaprotocol.io/vega/cmd/vegawallet/commands/printer" 28 vgterm "code.vegaprotocol.io/vega/libs/term" 29 vgzap "code.vegaprotocol.io/vega/libs/zap" 30 "code.vegaprotocol.io/vega/paths" 31 "code.vegaprotocol.io/vega/wallet/api" 32 netstore "code.vegaprotocol.io/vega/wallet/network/store/v1" 33 "code.vegaprotocol.io/vega/wallet/wallets" 34 35 "github.com/spf13/cobra" 36 "go.uber.org/zap" 37 ) 38 39 const ( 40 defaultRequestRetryCount = 5 41 defaultMaxRequestDuration = 5 * time.Second 42 ) 43 44 type Error struct { 45 Err string `json:"error"` 46 } 47 48 type Writer struct { 49 Out io.Writer 50 Err io.Writer 51 } 52 53 func Execute(w *Writer) { 54 c := NewCmdRoot(w.Out) 55 56 execErr := c.Execute() 57 if execErr == nil { 58 return 59 } 60 61 defer os.Exit(1) 62 63 if errors.Is(execErr, flags.ErrUnsupportedOutput) { 64 _, _ = fmt.Fprintln(w.Err, execErr) 65 } 66 67 output, _ := c.Flags().GetString("output") 68 switch output { 69 case flags.InteractiveOutput: 70 fprintErrorInteractive(w, execErr) 71 case flags.JSONOutput: 72 fprintErrorJSON(w.Err, execErr) 73 } 74 } 75 76 func fprintErrorInteractive(w *Writer, execErr error) { 77 if vgterm.HasTTY() { 78 p := printer.NewInteractivePrinter(w.Out) 79 p.Print(p.String().CrossMark().DangerText("Error: ").DangerText(execErr.Error()).NextLine()) 80 } else { 81 _, _ = fmt.Fprintln(w.Err, execErr) 82 } 83 } 84 85 func fprintErrorJSON(w io.Writer, err error) { 86 jsonErr := printer.FprintJSON(w, Error{ 87 Err: err.Error(), 88 }) 89 if jsonErr != nil { 90 _, _ = fmt.Fprintf(os.Stderr, "couldn't format error as JSON: %v\n", jsonErr) 91 _, _ = fmt.Fprintf(os.Stderr, "original error: %v\n", err) 92 } 93 } 94 95 func autoCompleteWallet(cmd *cobra.Command, vegaHome string, property string) { 96 err := cmd.RegisterFlagCompletionFunc(property, func(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) { 97 walletStore, err := wallets.InitialiseStore(vegaHome, false) 98 if err != nil { 99 return nil, cobra.ShellCompDirectiveDefault 100 } 101 defer walletStore.Close() 102 103 listWallet := api.NewAdminListWallets(walletStore) 104 rawResult, errorDetails := listWallet.Handle(context.Background(), nil) 105 if errorDetails != nil { 106 return nil, cobra.ShellCompDirectiveDefault 107 } 108 return rawResult.(api.AdminListWalletsResult).Wallets, cobra.ShellCompDirectiveDefault 109 }) 110 if err != nil { 111 panic(err) 112 } 113 } 114 115 func autoCompleteNetwork(cmd *cobra.Command, vegaHome string) { 116 err := cmd.RegisterFlagCompletionFunc("network", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { 117 vegaPaths := paths.New(vegaHome) 118 119 netStore, err := netstore.InitialiseStore(vegaPaths) 120 if err != nil { 121 return nil, cobra.ShellCompDirectiveDefault 122 } 123 124 nets, err := netStore.ListNetworks() 125 if err != nil { 126 return nil, cobra.ShellCompDirectiveDefault 127 } 128 return nets, cobra.ShellCompDirectiveDefault 129 }) 130 if err != nil { 131 panic(err) 132 } 133 } 134 135 func autoCompleteLogLevel(cmd *cobra.Command) { 136 err := cmd.RegisterFlagCompletionFunc("level", func(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) { 137 return vgzap.SupportedLogLevels, cobra.ShellCompDirectiveDefault 138 }) 139 if err != nil { 140 panic(err) 141 } 142 } 143 144 func buildCmdLogger(output, level string) (*zap.Logger, error) { 145 if output == flags.InteractiveOutput { 146 return vgzap.BuildStandardConsoleLogger(level) 147 } 148 149 return vgzap.BuildStandardJSONLogger(level) 150 }