cosmos-sdk/client/keys/utils.go
2023-08-23 10:43:47 +00:00

74 lines
1.3 KiB
Go

package keys
import (
"encoding/json"
"fmt"
"io"
"sigs.k8s.io/yaml"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
cryptokeyring "github.com/cosmos/cosmos-sdk/crypto/keyring"
)
func printKeyringRecord(w io.Writer, ko KeyOutput, output string) error {
switch output {
case flags.OutputFormatText:
if err := printTextRecords(w, []KeyOutput{ko}); err != nil {
return err
}
case flags.OutputFormatJSON:
out, err := json.Marshal(ko)
if err != nil {
return err
}
if _, err := fmt.Fprintln(w, string(out)); err != nil {
return err
}
}
return nil
}
func printKeyringRecords(clientCtx client.Context, w io.Writer, records []*cryptokeyring.Record, output string) error {
kos, err := MkAccKeysOutput(records, clientCtx.AddressCodec)
if err != nil {
return err
}
switch output {
case flags.OutputFormatText:
if err := printTextRecords(w, kos); err != nil {
return err
}
case flags.OutputFormatJSON:
out, err := json.Marshal(kos)
if err != nil {
return err
}
if _, err := fmt.Fprintf(w, "%s", out); err != nil {
return err
}
}
return nil
}
func printTextRecords(w io.Writer, kos []KeyOutput) error {
out, err := yaml.Marshal(&kos)
if err != nil {
return err
}
if _, err := fmt.Fprintln(w, string(out)); err != nil {
return err
}
return nil
}