Skip to content

Commit

Permalink
Add a flag for printing the output as JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
wheelercj committed Dec 9, 2023
1 parent 6be6ffd commit 74cb4d3
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 22 deletions.
32 changes: 20 additions & 12 deletions cmd/find_spam.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package cmd

import (
"encoding/json"
"fmt"
"slices"
"strings"
Expand Down Expand Up @@ -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)
}
}
}
}
19 changes: 9 additions & 10 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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.",
}
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 74cb4d3

Please sign in to comment.