cosmos-sdk/client/keys/utils.go
Julián Toledano 6188f6e798
refactor!: key presentation outside keyring (#14151)
* refactor: move key presentation to client/keys

* refactor: move key presentation to client/keys

* update: changelog

Co-authored-by: Ezequiel Raynaudo <raynaudo.ee@gmail.com>
2022-12-07 10:22:19 +00:00

85 lines
1.4 KiB
Go

package keys
import (
"encoding/json"
"fmt"
"io"
"sigs.k8s.io/yaml"
cryptokeyring "github.com/cosmos/cosmos-sdk/crypto/keyring"
)
// available output formats.
const (
OutputFormatText = "text"
OutputFormatJSON = "json"
)
type bechKeyOutFn func(k *cryptokeyring.Record) (KeyOutput, error)
func printKeyringRecord(w io.Writer, k *cryptokeyring.Record, bechKeyOut bechKeyOutFn, output string) error {
ko, err := bechKeyOut(k)
if err != nil {
return err
}
switch output {
case OutputFormatText:
if err := printTextRecords(w, []KeyOutput{ko}); err != nil {
return err
}
case 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(w io.Writer, records []*cryptokeyring.Record, output string) error {
kos, err := MkAccKeysOutput(records)
if err != nil {
return err
}
switch output {
case OutputFormatText:
if err := printTextRecords(w, kos); err != nil {
return err
}
case 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
}