(chore) setup kmsvc-cli
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"text/tabwriter"
|
||||
|
||||
kmsvc "forgejo.riotpiao.homelab.com/homelab/kmsvc-sdk"
|
||||
)
|
||||
|
||||
// renderMessages writes msgs to w in the requested format ("table" or
|
||||
// "json"). Unrecognized formats fall back to "table".
|
||||
func renderMessages(w io.Writer, format string, msgs []kmsvc.Message) error {
|
||||
if format == "json" {
|
||||
return json.NewEncoder(w).Encode(msgs)
|
||||
}
|
||||
|
||||
tw := tabwriter.NewWriter(w, 0, 0, 2, ' ', 0)
|
||||
fmt.Fprintln(tw, "MESSAGE_ID\tRECEIPT_HANDLE\tRECEIVE_COUNT\tBODY")
|
||||
for _, m := range msgs {
|
||||
fmt.Fprintf(tw, "%s\t%s\t%d\t%s\n", m.MessageID, m.ReceiptHandle, m.ReceiveCount, string(m.Body))
|
||||
}
|
||||
return tw.Flush()
|
||||
}
|
||||
|
||||
// renderBatchResult writes a batch send/delete result to w.
|
||||
func renderBatchResult(w io.Writer, format string, successful, failed []kmsvc.BatchResultEntry) error {
|
||||
if format == "json" {
|
||||
return json.NewEncoder(w).Encode(struct {
|
||||
Successful []kmsvc.BatchResultEntry `json:"successful"`
|
||||
Failed []kmsvc.BatchResultEntry `json:"failed"`
|
||||
}{successful, failed})
|
||||
}
|
||||
|
||||
tw := tabwriter.NewWriter(w, 0, 0, 2, ' ', 0)
|
||||
fmt.Fprintln(tw, "ID\tSTATUS\tMESSAGE_ID\tERROR")
|
||||
for _, e := range successful {
|
||||
fmt.Fprintf(tw, "%s\tok\t%s\t\n", e.ID, e.MessageID)
|
||||
}
|
||||
for _, e := range failed {
|
||||
fmt.Fprintf(tw, "%s\tfailed\t\t%s\n", e.ID, e.Error)
|
||||
}
|
||||
return tw.Flush()
|
||||
}
|
||||
Reference in New Issue
Block a user