github.com/Finschia/finschia-sdk@v0.48.1/client/keys/utils.go (about) 1 package keys 2 3 import ( 4 "fmt" 5 "io" 6 7 yaml "gopkg.in/yaml.v2" 8 9 cryptokeyring "github.com/Finschia/finschia-sdk/crypto/keyring" 10 ) 11 12 // available output formats. 13 const ( 14 OutputFormatText = "text" 15 OutputFormatJSON = "json" 16 ) 17 18 type bechKeyOutFn func(keyInfo cryptokeyring.Info) (cryptokeyring.KeyOutput, error) 19 20 func printKeyInfo(w io.Writer, keyInfo cryptokeyring.Info, bechKeyOut bechKeyOutFn, output string) { 21 ko, err := bechKeyOut(keyInfo) 22 if err != nil { 23 panic(err) 24 } 25 26 switch output { 27 case OutputFormatText: 28 printTextInfos(w, []cryptokeyring.KeyOutput{ko}) 29 30 case OutputFormatJSON: 31 out, err := KeysCdc.MarshalJSON(ko) 32 if err != nil { 33 panic(err) 34 } 35 36 fmt.Fprintln(w, string(out)) 37 } 38 } 39 40 func printInfos(w io.Writer, infos []cryptokeyring.Info, output string) { 41 kos, err := cryptokeyring.MkAccKeysOutput(infos) 42 if err != nil { 43 panic(err) 44 } 45 46 switch output { 47 case OutputFormatText: 48 printTextInfos(w, kos) 49 50 case OutputFormatJSON: 51 out, err := KeysCdc.MarshalJSON(kos) 52 if err != nil { 53 panic(err) 54 } 55 56 fmt.Fprintf(w, "%s", out) 57 } 58 } 59 60 func printTextInfos(w io.Writer, kos []cryptokeyring.KeyOutput) { 61 out, err := yaml.Marshal(&kos) 62 if err != nil { 63 panic(err) 64 } 65 fmt.Fprintln(w, string(out)) 66 }