Skip to content

Commit

Permalink
Harry/rly address (#1204)
Browse files Browse the repository at this point in the history
* 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 <harrycosmos@Harrys-MacBook-Pro.local>
Co-authored-by: Andrew Gouin <andrew@gouin.io>
  • Loading branch information
3 people committed Jun 1, 2023
1 parent 736e48b commit de95076
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 42 deletions.
104 changes: 62 additions & 42 deletions cmd/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ func keysCmd(a *appState) *cobra.Command {
keysRestoreCmd(a),
keysDeleteCmd(a),
keysListCmd(a),
keysShowCmd(a),
keysExportCmd(a),
keysShowCmd(a),
)

return cmd
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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
}
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ func NewRootCmd(log *zap.Logger) *cobra.Command {
startCmd(a),
lineBreakCommand(),
getVersionCmd(a),
addressCmd(a),
)

return rootCmd
Expand Down

0 comments on commit de95076

Please sign in to comment.