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 (
account string
address v1.JsonAddress
address *v1.JsonAddress
ListKeysCmd = &cobra.Command{
Use: "list <phone number or UUID>",
Short: "list all keys for an account",
Use: "list [<phone number or UUID>]",
Short: "list all known keys for another account",
PreRun: func(cmd *cobra.Command, args []string) {
if account == "" {
account = config.Config.DefaultAccount
@ -43,46 +43,17 @@ var (
common.Must(cmd.Help())
log.Fatal("No account specified. Please specify with --account or set a default")
}
if len(args) != 1 {
common.Must(cmd.Help())
log.Fatal("please specify an address")
if len(args) >= 1 {
a := common.StringToAddress(args[0])
address = &a
}
address = common.StringToAddress(args[0])
},
Run: func(_ *cobra.Command, _ []string) {
go common.Signald.Listen(nil)
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")
if address == nil {
getAll()
} else {
getOne()
}
},
}
@ -91,3 +62,77 @@ var (
func init() {
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")
}
}