recv now has -json for pretty print

This commit is contained in:
Bel LaPointe
2023-10-16 08:50:59 -06:00
parent 7d372adb78
commit e3d5a7e221

View File

@@ -1,10 +1,12 @@
package main
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/mail"
"os"
"strings"
"time"
@@ -21,6 +23,7 @@ func main() {
as.Append(args.STRING, "u", "username", emailer.From)
as.Append(args.STRING, "p", "password", emailer.Password)
as.Append(args.INT, "b", "dont read more than this many characters", 4096)
as.Append(args.BOOL, "json", "output as json", false)
if err := as.Parse(); err != nil {
panic(err)
}
@@ -42,6 +45,7 @@ func main() {
if err != nil {
panic(err)
}
emails := make([]map[string]any, 0, emailer.Limit)
for msg := range msgs {
b, _ := ioutil.ReadAll(io.LimitReader(msg.Body, int64(as.GetInt("b"))))
s := strings.ReplaceAll(string(b), "\r\n", "\n")
@@ -52,6 +56,14 @@ func main() {
}
d, _ := msg.Header.Date()
d = d.In(time.Local)
if as.GetBool("json") {
emails = append(emails, map[string]any{
"t": d.Format("06-01-02T15:04Z07"),
"from": msg.Header.Get("From"),
"subject": msg.Header.Get("Subject"),
"body": s,
})
} else {
fmt.Printf(
"@%+v @%+v: \n\t%+v: \n\t%s\n",
d.Format("06-01-02T15:04Z07"),
@@ -60,4 +72,10 @@ func main() {
s,
)
}
}
if as.GetBool("json") {
enc := json.NewEncoder(os.Stdout)
enc.SetIndent("", " ")
enc.Encode(emails)
}
}