code.vegaprotocol.io/vega@v0.79.0/cmd/vegawallet/commands/root.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  	"os"
    22  
    23  	"code.vegaprotocol.io/vega/cmd/vegawallet/commands/cli"
    24  	"code.vegaprotocol.io/vega/cmd/vegawallet/commands/flags"
    25  
    26  	"github.com/spf13/cobra"
    27  )
    28  
    29  var rootExamples = cli.Examples(`
    30  	# Specify a custom Vega home directory
    31  	{{.Software}} --home PATH_TO_DIR COMMAND
    32  
    33  	# Change the output to JSON
    34  	{{.Software}} --output json COMMAND
    35  
    36  	# Disable colors on output using environment variable
    37  	NO_COLOR=1 {{.Software}} COMMAND
    38  `)
    39  
    40  func NewCmdRoot(w io.Writer) *cobra.Command {
    41  	return BuildCmdRoot(w)
    42  }
    43  
    44  func BuildCmdRoot(w io.Writer) *cobra.Command {
    45  	f := &RootFlags{}
    46  
    47  	cmd := &cobra.Command{
    48  		Use:           os.Args[0],
    49  		Short:         "The Vega wallet",
    50  		Long:          "The Vega wallet",
    51  		Example:       rootExamples,
    52  		SilenceUsage:  true,
    53  		SilenceErrors: true,
    54  		PersistentPreRunE: func(cmd *cobra.Command, _ []string) error {
    55  			// The `__complete` command is being run to build up the auto-completion
    56  			// file. We should skip any verification to not temper with the process.
    57  			// Any additional printing will end up in the auto-completion registry.
    58  			// The `completion` command output the completion script for a given
    59  			// shell, that should not be tempered with. We should skip it as well.
    60  			if cmd.Name() == "__complete" || cmd.Name() == "completion" {
    61  				return nil
    62  			}
    63  
    64  			if err := f.Validate(); err != nil {
    65  				return err
    66  			}
    67  
    68  			return nil
    69  		},
    70  	}
    71  
    72  	cmd.PersistentFlags().StringVarP(&f.Output,
    73  		"output", "o",
    74  		flags.InteractiveOutput,
    75  		fmt.Sprintf("Specify the output format: %v", flags.AvailableOutputs),
    76  	)
    77  	cmd.PersistentFlags().StringVar(&f.Home,
    78  		"home",
    79  		"",
    80  		"Specify the location of a custom Vega home",
    81  	)
    82  
    83  	_ = cmd.MarkPersistentFlagDirname("home")
    84  	_ = cmd.RegisterFlagCompletionFunc("output", func(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
    85  		return flags.AvailableOutputs, cobra.ShellCompDirectiveDefault
    86  	})
    87  
    88  	// Root commands
    89  	cmd.AddCommand(NewCmdInit(w, f))
    90  
    91  	cmd.AddCommand(NewCmdDisclaimer(w, f))
    92  	// Sub-commands
    93  	cmd.AddCommand(NewCmdAPIToken(w, f))
    94  	cmd.AddCommand(NewCmdKey(w, f))
    95  	cmd.AddCommand(NewCmdMessage(w, f))
    96  	cmd.AddCommand(NewCmdNetwork(w, f))
    97  	cmd.AddCommand(NewCmdPassphrase(w, f))
    98  	cmd.AddCommand(NewCmdPermissions(w, f))
    99  	cmd.AddCommand(NewCmdRawTransaction(w, f))
   100  	cmd.AddCommand(NewCmdService(w, f))
   101  	cmd.AddCommand(NewCmdSession(w, f))
   102  	cmd.AddCommand(NewCmdShell(w, f))
   103  	cmd.AddCommand(NewCmdSoftware(w, f))
   104  	cmd.AddCommand(NewCmdTransaction(w, f))
   105  
   106  	// Wallet commands
   107  	// We don't have a wrapper sub-command for wallet commands.
   108  	cmd.AddCommand(NewCmdCreateWallet(w, f))
   109  	cmd.AddCommand(NewCmdDeleteWallet(w, f))
   110  	cmd.AddCommand(NewCmdDescribeWallet(w, f))
   111  	cmd.AddCommand(NewCmdImportWallet(w, f))
   112  	cmd.AddCommand(NewCmdListWallets(w, f))
   113  	cmd.AddCommand(NewCmdLocateWallets(w, f))
   114  	cmd.AddCommand(NewCmdRenameWallet(w, f))
   115  
   116  	return cmd
   117  }
   118  
   119  type RootFlags struct {
   120  	Output string
   121  	Home   string
   122  }
   123  
   124  func (f *RootFlags) Validate() error {
   125  	return flags.ValidateOutput(f.Output)
   126  }