diff --git a/cmd/signaldctl/cmd/key/list/list-keys.go b/cmd/signaldctl/cmd/key/list/list-keys.go index d77766a..30f5104 100644 --- a/cmd/signaldctl/cmd/key/list/list-keys.go +++ b/cmd/signaldctl/cmd/key/list/list-keys.go @@ -31,10 +31,10 @@ import ( var ( account string - address v1.JsonAddress + address *v1.JsonAddress ListKeysCmd = &cobra.Command{ - Use: "list ", - Short: "list all keys for an account", + Use: "list []", + 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") + } +}