recv now has -json for pretty print
This commit is contained in:
@@ -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,12 +56,26 @@ func main() {
|
||||
}
|
||||
d, _ := msg.Header.Date()
|
||||
d = d.In(time.Local)
|
||||
fmt.Printf(
|
||||
"@%+v @%+v: \n\t%+v: \n\t%s\n",
|
||||
d.Format("06-01-02T15:04Z07"),
|
||||
msg.Header.Get("From"),
|
||||
msg.Header.Get("Subject"),
|
||||
s,
|
||||
)
|
||||
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"),
|
||||
msg.Header.Get("From"),
|
||||
msg.Header.Get("Subject"),
|
||||
s,
|
||||
)
|
||||
}
|
||||
}
|
||||
if as.GetBool("json") {
|
||||
enc := json.NewEncoder(os.Stdout)
|
||||
enc.SetIndent("", " ")
|
||||
enc.Encode(emails)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user