laconicd/client/keys/utils.go

59 lines
1.1 KiB
Go
Raw Normal View History

2021-04-17 10:00:07 +00:00
package keys
import (
"fmt"
"io"
2022-10-10 10:38:33 +00:00
"sigs.k8s.io/yaml"
2021-04-17 10:00:07 +00:00
"github.com/cosmos/cosmos-sdk/client/keys"
cryptokeyring "github.com/cosmos/cosmos-sdk/crypto/keyring"
)
// available output formats.
const (
OutputFormatText = "text"
OutputFormatJSON = "json"
)
2022-10-10 10:38:33 +00:00
type bechKeyOutFn func(k *cryptokeyring.Record) (cryptokeyring.KeyOutput, error)
2021-04-17 10:00:07 +00:00
2022-10-10 10:38:33 +00:00
func printKeyringRecord(w io.Writer, k *cryptokeyring.Record, bechKeyOut bechKeyOutFn, output string) error {
ko, err := bechKeyOut(k)
2021-04-17 10:00:07 +00:00
if err != nil {
2022-10-10 10:38:33 +00:00
return err
2021-04-17 10:00:07 +00:00
}
switch output {
case OutputFormatText:
2022-10-10 10:38:33 +00:00
if err := printTextRecords(w, []cryptokeyring.KeyOutput{ko}); err != nil {
return err
}
2021-04-17 10:00:07 +00:00
case OutputFormatJSON:
out, err := keys.KeysCdc.MarshalJSON(ko)
if err != nil {
2022-10-10 10:38:33 +00:00
return err
2021-04-17 10:00:07 +00:00
}
2022-10-10 10:38:33 +00:00
if _, err := fmt.Fprintln(w, string(out)); err != nil {
return err
}
2021-04-17 10:00:07 +00:00
}
2022-10-10 10:38:33 +00:00
return nil
2021-04-17 10:00:07 +00:00
}
2022-10-10 10:38:33 +00:00
func printTextRecords(w io.Writer, kos []cryptokeyring.KeyOutput) error {
2021-04-17 10:00:07 +00:00
out, err := yaml.Marshal(&kos)
if err != nil {
2022-10-10 10:38:33 +00:00
return err
2021-04-17 10:00:07 +00:00
}
2022-10-10 10:38:33 +00:00
if _, err := fmt.Fprintln(w, string(out)); err != nil {
return err
2021-04-17 10:00:07 +00:00
}
2022-10-10 10:38:33 +00:00
return nil
2021-04-17 10:00:07 +00:00
}