github.com/cosmos/cosmos-sdk@v0.50.10/client/keys/utils.go (about)

     1  package keys
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"io"
     7  
     8  	"sigs.k8s.io/yaml"
     9  
    10  	"github.com/cosmos/cosmos-sdk/client/flags"
    11  	cryptokeyring "github.com/cosmos/cosmos-sdk/crypto/keyring"
    12  )
    13  
    14  type bechKeyOutFn func(k *cryptokeyring.Record) (KeyOutput, error)
    15  
    16  func printKeyringRecord(w io.Writer, k *cryptokeyring.Record, bechKeyOut bechKeyOutFn, output string) error {
    17  	ko, err := bechKeyOut(k)
    18  	if err != nil {
    19  		return err
    20  	}
    21  
    22  	switch output {
    23  	case flags.OutputFormatText:
    24  		if err := printTextRecords(w, []KeyOutput{ko}); err != nil {
    25  			return err
    26  		}
    27  
    28  	case flags.OutputFormatJSON:
    29  		out, err := json.Marshal(ko)
    30  		if err != nil {
    31  			return err
    32  		}
    33  
    34  		if _, err := fmt.Fprintln(w, string(out)); err != nil {
    35  			return err
    36  		}
    37  	}
    38  
    39  	return nil
    40  }
    41  
    42  func printKeyringRecords(w io.Writer, records []*cryptokeyring.Record, output string) error {
    43  	kos, err := MkAccKeysOutput(records)
    44  	if err != nil {
    45  		return err
    46  	}
    47  
    48  	switch output {
    49  	case flags.OutputFormatText:
    50  		if err := printTextRecords(w, kos); err != nil {
    51  			return err
    52  		}
    53  
    54  	case flags.OutputFormatJSON:
    55  		out, err := json.Marshal(kos)
    56  		if err != nil {
    57  			return err
    58  		}
    59  
    60  		if _, err := fmt.Fprintf(w, "%s", out); err != nil {
    61  			return err
    62  		}
    63  	}
    64  
    65  	return nil
    66  }
    67  
    68  func printTextRecords(w io.Writer, kos []KeyOutput) error {
    69  	out, err := yaml.Marshal(&kos)
    70  	if err != nil {
    71  		return err
    72  	}
    73  
    74  	if _, err := fmt.Fprintln(w, string(out)); err != nil {
    75  		return err
    76  	}
    77  
    78  	return nil
    79  }