generated from okp4/template-oss
-
Notifications
You must be signed in to change notification settings - Fork 126
/
list.go
53 lines (43 loc) · 1.24 KB
/
list.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package keys
import (
"github.com/spf13/cobra"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
)
const (
flagListNames = "list-names"
listKeysCmd = "list"
)
// EnhanceListCmd replaces the original 'list' command implementation with our own 'list' command which
// will allow us to list did:key of the keys as well as the original keys.
func EnhanceListCmd(cmd *cobra.Command) {
for _, c := range cmd.Commands() {
if c.Name() == listKeysCmd {
c.RunE = runListCmd
break
}
}
}
// runListCmd retrieves all keys from the keyring and prints them to the console.
// This is an improved copy of the runListCmd from the keys module of the cosmos-sdk.
func runListCmd(cmd *cobra.Command, _ []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
records, err := clientCtx.Keyring.List()
if err != nil {
return err
}
if len(records) == 0 && clientCtx.OutputFormat == flags.OutputFormatText {
cmd.Println("No records were found in keyring")
return nil
}
if ok, _ := cmd.Flags().GetBool(flagListNames); !ok {
return printKeyringRecords(cmd.OutOrStdout(), records, clientCtx.OutputFormat)
}
for _, k := range records {
cmd.Println(k.Name)
}
return nil
}