From 74cb4d37f0a30b882d97e5bb12775237ffb55894 Mon Sep 17 00:00:00 2001 From: wheelercj Date: Sat, 9 Dec 2023 00:07:26 -0800 Subject: [PATCH] Add a flag for printing the output as JSON --- cmd/find_spam.go | 32 ++++++++++++++++++++------------ cmd/root.go | 19 +++++++++---------- 2 files changed, 29 insertions(+), 22 deletions(-) diff --git a/cmd/find_spam.go b/cmd/find_spam.go index 537a5f3..f85ef20 100644 --- a/cmd/find_spam.go +++ b/cmd/find_spam.go @@ -15,6 +15,7 @@ package cmd import ( + "encoding/json" "fmt" "slices" "strings" @@ -144,21 +145,28 @@ func printAddresses(singleUseAddresses []string, toAndFrom map[string][]string) fmt.Println("No single-use addresses found.") return } - fmt.Printf( - "Your inbox's %d single-use addresses and those they received from:\n", - len(singleUseAddresses), - ) - var printedYet bool + for to := range toAndFrom { slices.Sort(toAndFrom[to]) toAndFrom[to] = slices.Compact(toAndFrom[to]) - fmt.Println(to) - for _, from := range toAndFrom[to] { - fmt.Printf("\t%s\n", from) - } - printedYet = true } - if !printedYet { - fmt.Println("None of the single-use addresses have received emails.") + + if PrintJson { + bytes, err := json.Marshal(toAndFrom) + if err != nil { + panic(err) + } + fmt.Println(string(bytes)) + } else { + fmt.Printf( + "Your inbox's %d single-use addresses and those they received from:\n", + len(singleUseAddresses), + ) + for to := range toAndFrom { + fmt.Println(to) + for _, from := range toAndFrom[to] { + fmt.Printf("\t%s\n", from) + } + } } } diff --git a/cmd/root.go b/cmd/root.go index 77932df..48c2adc 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -24,7 +24,7 @@ import ( var Verbose bool var ApiSessionUrl string var Domains string -var Json bool +var PrintJson bool func runFunc(cmd *cobra.Command, args []string) { token := getApiToken() @@ -38,7 +38,7 @@ func runFunc(cmd *cobra.Command, args []string) { // rootCmd represents the base command when called without any subcommands. var rootCmd = &cobra.Command{ Use: "email-linter", - Version: "0.0.1", + Version: "0.0.2", Run: runFunc, Short: "Easily find spam and phishing emails received at single-use email addresses.", } @@ -72,14 +72,13 @@ func init() { "duck.com mozmail.com icloud.com", "email protection service domains to search for", ) - // TODO - // rootCmd.Flags().BoolVarP( - // &Json, - // "json", - // "j", - // false, - // "print output as JSON", - // ) + rootCmd.Flags().BoolVarP( + &PrintJson, + "json", + "j", + false, + "print output as JSON", + ) } // getApiToken looks for a API_TOKEN environment variable, or asks for the token to be