Allow listing all known keys

This commit is contained in:
Finn 2021-06-12 16:22:31 -07:00
parent a2f2cf9c05
commit 6ed4784138

View file

@ -31,10 +31,10 @@ import (
var ( var (
account string account string
address v1.JsonAddress address *v1.JsonAddress
ListKeysCmd = &cobra.Command{ ListKeysCmd = &cobra.Command{
Use: "list <phone number or UUID>", Use: "list [<phone number or UUID>]",
Short: "list all keys for an account", Short: "list all known keys for another account",
PreRun: func(cmd *cobra.Command, args []string) { PreRun: func(cmd *cobra.Command, args []string) {
if account == "" { if account == "" {
account = config.Config.DefaultAccount account = config.Config.DefaultAccount
@ -43,46 +43,17 @@ var (
common.Must(cmd.Help()) common.Must(cmd.Help())
log.Fatal("No account specified. Please specify with --account or set a default") log.Fatal("No account specified. Please specify with --account or set a default")
} }
if len(args) != 1 { if len(args) >= 1 {
common.Must(cmd.Help()) a := common.StringToAddress(args[0])
log.Fatal("please specify an address") address = &a
} }
address = common.StringToAddress(args[0])
}, },
Run: func(_ *cobra.Command, _ []string) { Run: func(_ *cobra.Command, _ []string) {
go common.Signald.Listen(nil) go common.Signald.Listen(nil)
req := v1.GetIdentitiesRequest{Account: account, Address: &address} if address == nil {
resp, err := req.Submit(common.Signald) getAll()
if err != nil { } else {
log.Fatal(err, "error communicating with signald") getOne()
}
switch common.OutputFormat {
case common.OutputFormatJSON:
err := json.NewEncoder(os.Stdout).Encode(resp)
if err != nil {
log.Fatal(err, "error encoding response to stdout")
}
case common.OutputFormatYAML:
err := yaml.NewEncoder(os.Stdout).Encode(resp)
if err != nil {
log.Fatal(err, "error encoding response to stdout")
}
case common.OutputFormatCSV, common.OutputFormatTable, common.OutputFormatDefault:
t := table.NewWriter()
t.SetOutputMirror(os.Stdout)
t.AppendHeader(table.Row{"Safety Number", "Trust Level"})
for _, identity := range resp.Identities {
t.AppendRow(table.Row{identity.SafetyNumber, identity.TrustLevel})
}
if common.OutputFormat == common.OutputFormatCSV {
t.RenderCSV()
} else {
common.StylizeTable(t)
t.Render()
}
default:
log.Fatal("Unsupported output format")
} }
}, },
} }
@ -91,3 +62,77 @@ var (
func init() { func init() {
ListKeysCmd.Flags().StringVarP(&account, "account", "a", "", "the signald account to use") ListKeysCmd.Flags().StringVarP(&account, "account", "a", "", "the signald account to use")
} }
func getOne() {
req := v1.GetIdentitiesRequest{Account: account, Address: address}
resp, err := req.Submit(common.Signald)
if err != nil {
log.Fatal(err, "error communicating with signald")
}
switch common.OutputFormat {
case common.OutputFormatJSON:
err := json.NewEncoder(os.Stdout).Encode(resp)
if err != nil {
log.Fatal(err, "error encoding response to stdout")
}
case common.OutputFormatYAML:
err := yaml.NewEncoder(os.Stdout).Encode(resp)
if err != nil {
log.Fatal(err, "error encoding response to stdout")
}
case common.OutputFormatCSV, common.OutputFormatTable, common.OutputFormatDefault:
t := table.NewWriter()
t.SetOutputMirror(os.Stdout)
t.AppendHeader(table.Row{"Safety Number", "Trust Level"})
for _, identity := range resp.Identities {
t.AppendRow(table.Row{identity.SafetyNumber, identity.TrustLevel})
}
if common.OutputFormat == common.OutputFormatCSV {
t.RenderCSV()
} else {
common.StylizeTable(t)
t.Render()
}
default:
log.Fatal("Unsupported output format")
}
}
func getAll() {
req := v1.GetAllIdentities{Account: account}
resp, err := req.Submit(common.Signald)
if err != nil {
log.Fatal(err, "error communicating with signald")
}
switch common.OutputFormat {
case common.OutputFormatJSON:
err := json.NewEncoder(os.Stdout).Encode(resp)
if err != nil {
log.Fatal(err, "error encoding response to stdout")
}
case common.OutputFormatYAML:
err := yaml.NewEncoder(os.Stdout).Encode(resp)
if err != nil {
log.Fatal(err, "error encoding response to stdout")
}
case common.OutputFormatCSV, common.OutputFormatTable, common.OutputFormatDefault:
t := table.NewWriter()
t.SetOutputMirror(os.Stdout)
t.AppendHeader(table.Row{"Number", "UUID", "Safety Number", "Trust Level"})
for _, keys := range resp.IdentityKeys {
for _, identity := range keys.Identities {
t.AppendRow(table.Row{keys.Address.Number, keys.Address.UUID, identity.SafetyNumber, identity.TrustLevel})
}
}
if common.OutputFormat == common.OutputFormatCSV {
t.RenderCSV()
} else {
common.StylizeTable(t)
t.Render()
}
default:
log.Fatal("Unsupported output format")
}
}