From f4b56856caebd902c76666a2e5b0a5fc8262a63e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=B0lker=20G=2E=20=C3=96zt=C3=BCrk?= Date: Fri, 5 Feb 2021 13:38:57 +0300 Subject: [PATCH] feat(cmd): make root command publicly accessible (#411) so, it can be reused as a sub command by other cobra compatible CLIs. e.g. (start relayer rly -h) --- cmd/root.go | 45 +++++++++++++++++++++++++-------------------- 1 file changed, 25 insertions(+), 20 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index fc0ac9d46..5141f0aaa 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -46,14 +46,31 @@ var ( dord = "ordered" ) -func init() { - cobra.EnableCommandSorting = false +// NewRootCmd returns the root command for relayer. +func NewRootCmd() *cobra.Command { + // RootCmd represents the base command when called without any subcommands + var rootCmd = &cobra.Command{ + Use: appName, + Short: "This application relays data between configured IBC enabled chains", + Long: strings.TrimSpace(`The relayer has commands for: + 1. Configuration of the Chains and Paths that the relayer with transfer packets over + 2. Management of keys and light clients on the local machine that will be used to sign and verify txs + 3. Query and transaction functionality for IBC + 4. A responsive relaying application that listens on a path + 5. Commands to assist with development, testnets, and versioning. - rootCmd.SilenceUsage = true +NOTE: Most of the commands have aliases that make typing them much quicker (i.e. 'rly tx', 'rly q', etc...)`), + } + + rootCmd.PersistentPreRunE = func(cmd *cobra.Command, _ []string) error { + // reads `homeDir/config/config.yaml` into `var config *Config` before each command + return initConfig(rootCmd) + } // Register top level flags --home and --debug rootCmd.PersistentFlags().StringVar(&homePath, flags.FlagHome, defaultHome, "set home directory") rootCmd.PersistentFlags().BoolVarP(&debug, "debug", "d", false, "debug output") + if err := viper.BindPFlag(flags.FlagHome, rootCmd.Flags().Lookup(flags.FlagHome)); err != nil { panic(err) } @@ -82,29 +99,17 @@ func init() { // This is a bit of a cheat :shushing_face: // cdc = codecstd.MakeCodec(simapp.ModuleBasics) // appCodec = codecstd.NewAppCodec(cdc) -} - -// rootCmd represents the base command when called without any subcommands -var rootCmd = &cobra.Command{ - Use: appName, - Short: "This application relays data between configured IBC enabled chains", - Long: strings.TrimSpace(`The relayer has commands for: - 1. Configuration of the Chains and Paths that the relayer with transfer packets over - 2. Management of keys and light clients on the local machine that will be used to sign and verify txs - 3. Query and transaction functionality for IBC - 4. A responsive relaying application that listens on a path - 5. Commands to assist with development, testnets, and versioning. -NOTE: Most of the commands have aliases that make typing them much quicker (i.e. 'rly tx', 'rly q', etc...)`), + return rootCmd } // Execute adds all child commands to the root command and sets flags appropriately. // This is called by main.main(). It only needs to happen once to the rootCmd. func Execute() { - rootCmd.PersistentPreRunE = func(_ *cobra.Command, _ []string) error { - // reads `homeDir/config/config.yaml` into `var config *Config` before each command - return initConfig(rootCmd) - } + cobra.EnableCommandSorting = false + + rootCmd := NewRootCmd() + rootCmd.SilenceUsage = true if err := rootCmd.Execute(); err != nil { os.Exit(1)