From de95076ff607789ec325d70cb974a9dfb0176085 Mon Sep 17 00:00:00 2001 From: Cosmos-Harry <110472914+Cosmos-Harry@users.noreply.github.com> Date: Thu, 1 Jun 2023 00:45:45 -0400 Subject: [PATCH] Harry/rly address (#1204) * added addresCmd to root and keys.go * nicks * nick * made a common method "showAddressByChainAndKey" to be used by both addressCmd and keysShowCmd --------- Co-authored-by: Harry Co-authored-by: Andrew Gouin --- cmd/keys.go | 104 +++++++++++++++++++++++++++++++--------------------- cmd/root.go | 1 + 2 files changed, 63 insertions(+), 42 deletions(-) diff --git a/cmd/keys.go b/cmd/keys.go index aa915d065..0ddd447d4 100644 --- a/cmd/keys.go +++ b/cmd/keys.go @@ -48,8 +48,8 @@ func keysCmd(a *appState) *cobra.Command { keysRestoreCmd(a), keysDeleteCmd(a), keysListCmd(a), - keysShowCmd(a), keysExportCmd(a), + keysShowCmd(a), ) return cmd @@ -289,47 +289,6 @@ $ %s k l ibc-1`, appName, appName)), return cmd } -// keysShowCmd respresents the `keys show` command -func keysShowCmd(a *appState) *cobra.Command { - cmd := &cobra.Command{ - Use: "show chain_name [key_name]", - Aliases: []string{"s"}, - Short: "Shows a key from the keychain associated with a particular chain", - Args: withUsage(cobra.RangeArgs(1, 2)), - Example: strings.TrimSpace(fmt.Sprintf(` -$ %s keys show ibc-0 -$ %s keys show ibc-1 key2 -$ %s k s ibc-2 testkey`, appName, appName, appName)), - RunE: func(cmd *cobra.Command, args []string) error { - chain, ok := a.config.Chains[args[0]] - if !ok { - return errChainNotFound(args[0]) - } - - var keyName string - if len(args) == 2 { - keyName = args[1] - } else { - keyName = chain.ChainProvider.Key() - } - - if !chain.ChainProvider.KeyExists(keyName) { - return errKeyDoesntExist(keyName) - } - - address, err := chain.ChainProvider.ShowAddress(keyName) - if err != nil { - return err - } - - fmt.Fprintln(cmd.OutOrStdout(), address) - return nil - }, - } - - return cmd -} - // keysExportCmd respresents the `keys export` command func keysExportCmd(a *appState) *cobra.Command { cmd := &cobra.Command{ @@ -363,3 +322,64 @@ $ %s k e cosmoshub testkey`, appName, appName)), return cmd } + +// ShowAddressByChainAndKey represents the logic for showing relayer address by chain_name and key_name +func (a *appState) showAddressByChainAndKey(cmd *cobra.Command, args []string) error { + chain, ok := a.config.Chains[args[0]] + if !ok { + return errChainNotFound(args[0]) + } + + var keyName string + if len(args) == 2 { + keyName = args[1] + } else { + keyName = chain.ChainProvider.Key() + } + + if !chain.ChainProvider.KeyExists(keyName) { + return errKeyDoesntExist(keyName) + } + + address, err := chain.ChainProvider.ShowAddress(keyName) + if err != nil { + return err + } + + fmt.Fprintln(cmd.OutOrStdout(), address) + return nil +} + +// keysShowCmd respresents the `keys show` command +func keysShowCmd(a *appState) *cobra.Command { + cmd := &cobra.Command{ + Use: "show chain_name [key_name]", + Aliases: []string{"s"}, + Short: "Shows a key from the keychain associated with a particular chain", + Args: withUsage(cobra.RangeArgs(1, 2)), + Example: strings.TrimSpace(fmt.Sprintf(` +$ %s keys show ibc-0 +$ %s keys show ibc-1 key2 +$ %s k s ibc-2 testkey`, appName, appName, appName)), + RunE: a.showAddressByChainAndKey, + } + + return cmd +} + +// addressCmd represents the address of a relayer +func addressCmd(a *appState) *cobra.Command { + cmd := &cobra.Command{ + Use: "address chain_name [key_name]", + Aliases: []string{"a"}, + Short: "Shows the address of a relayer", + Args: withUsage(cobra.RangeArgs(1, 2)), + Example: strings.TrimSpace(fmt.Sprintf(` +$ %s address ibc-0 +$ %s address ibc-1 key2 +$ %s a ibc-2 testkey`, appName, appName, appName)), + RunE: a.showAddressByChainAndKey, + } + + return cmd +} diff --git a/cmd/root.go b/cmd/root.go index eb8367409..1a6cfc3d3 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -120,6 +120,7 @@ func NewRootCmd(log *zap.Logger) *cobra.Command { startCmd(a), lineBreakCommand(), getVersionCmd(a), + addressCmd(a), ) return rootCmd