From 7d63867f365163f149db5d768c71f518f9eaf711 Mon Sep 17 00:00:00 2001 From: s Date: Fri, 7 Nov 2025 11:50:47 -0500 Subject: refactor: extract json output formatting to utils.PrintJSON and add colorized json output with new dns tld command --- cmd/admin.go | 10 ++-------- cmd/dns.go | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ cmd/npd.go | 10 ++-------- cmd/osint.go | 17 +++-------------- cmd/reddit.go | 42 ++++++----------------------------------- cmd/skiptrace.go | 26 ++++++++------------------ cmd/sl.go | 10 ++-------- cmd/x.go | 13 +++---------- 8 files changed, 83 insertions(+), 102 deletions(-) create mode 100644 cmd/dns.go (limited to 'cmd') diff --git a/cmd/admin.go b/cmd/admin.go index 5637724..e0f0653 100644 --- a/cmd/admin.go +++ b/cmd/admin.go @@ -1,11 +1,11 @@ package cmd import ( - "encoding/json" "fmt" "git.db.org.ai/dborg/internal/client" "git.db.org.ai/dborg/internal/config" "git.db.org.ai/dborg/internal/models" + "git.db.org.ai/dborg/internal/utils" "strconv" "github.com/spf13/cobra" @@ -94,13 +94,7 @@ func runAdminList(cmd *cobra.Command, args []string) error { return fmt.Errorf("API error: %s", response.Error) } - output, err := json.MarshalIndent(response.Accounts, "", " ") - if err != nil { - return fmt.Errorf("failed to format response: %w", err) - } - - fmt.Println(string(output)) - return nil + return utils.PrintJSON(response.Accounts) } func runAdminCreate(cmd *cobra.Command, args []string) error { diff --git a/cmd/dns.go b/cmd/dns.go new file mode 100644 index 0000000..71a0d83 --- /dev/null +++ b/cmd/dns.go @@ -0,0 +1,57 @@ +package cmd + +import ( + "encoding/json" + "fmt" + + "git.db.org.ai/dborg/internal/client" + "git.db.org.ai/dborg/internal/config" + "git.db.org.ai/dborg/internal/models" + "github.com/spf13/cobra" +) + +var dnsCmd = &cobra.Command{ + Use: "dns", + Short: "DNS operations", + Long: "DNS-related operations for domain checking", +} + +var dnsTLDCmd = &cobra.Command{ + Use: "tld [term]", + Short: "Check NXDOMAIN for custom term against all TLDs", + Long: "Streams NDJSON results checking each TLD. For NXDOMAIN domains, returns status. For existing domains, runs httpx to get page title and tech stack.", + Args: cobra.ExactArgs(1), + RunE: runDNSTLDCheck, +} + +func runDNSTLDCheck(cmd *cobra.Command, args []string) error { + term := args[0] + + cfg := config.New() + c, err := client.New(cfg) + if err != nil { + return fmt.Errorf("failed to create client: %w", err) + } + + params := &models.DNSTLDParams{ + Term: term, + } + + fmt.Printf("Checking TLDs for term: %s\n\n", term) + + err = c.CheckDNSTLDStream(params, func(result json.RawMessage) error { + fmt.Println(string(result)) + return nil + }) + + if err != nil { + return fmt.Errorf("TLD check failed: %w", err) + } + + return nil +} + +func init() { + rootCmd.AddCommand(dnsCmd) + dnsCmd.AddCommand(dnsTLDCmd) +} diff --git a/cmd/npd.go b/cmd/npd.go index 6e7ed4b..a6bd7b2 100644 --- a/cmd/npd.go +++ b/cmd/npd.go @@ -1,11 +1,11 @@ package cmd import ( - "encoding/json" "fmt" "git.db.org.ai/dborg/internal/client" "git.db.org.ai/dborg/internal/config" "git.db.org.ai/dborg/internal/models" + "git.db.org.ai/dborg/internal/utils" "github.com/spf13/cobra" ) @@ -85,11 +85,5 @@ func runNPDSearch(cmd *cobra.Command, args []string) error { return fmt.Errorf("API error: %s", response.Error) } - output, err := json.MarshalIndent(response.Results.Hits, "", " ") - if err != nil { - return fmt.Errorf("failed to format response: %w", err) - } - - fmt.Println(string(output)) - return nil + return utils.PrintJSON(response.Results.Hits) } diff --git a/cmd/osint.go b/cmd/osint.go index 49b94ab..5aa8799 100644 --- a/cmd/osint.go +++ b/cmd/osint.go @@ -6,6 +6,7 @@ import ( "git.db.org.ai/dborg/internal/client" "git.db.org.ai/dborg/internal/config" "git.db.org.ai/dborg/internal/models" + "git.db.org.ai/dborg/internal/utils" "github.com/spf13/cobra" ) @@ -106,13 +107,7 @@ func runOsintBSSIDLookup(cmd *cobra.Command, args []string) error { return err } - output, err := json.MarshalIndent(response, "", " ") - if err != nil { - return fmt.Errorf("failed to format response: %w", err) - } - - fmt.Println(string(output)) - return nil + return utils.PrintJSON(response) } func runOsintBreachForumSearch(cmd *cobra.Command, args []string) error { @@ -134,11 +129,5 @@ func runOsintBreachForumSearch(cmd *cobra.Command, args []string) error { return err } - output, err := json.MarshalIndent(response, "", " ") - if err != nil { - return fmt.Errorf("failed to format response: %w", err) - } - - fmt.Println(string(output)) - return nil + return utils.PrintJSON(response) } diff --git a/cmd/reddit.go b/cmd/reddit.go index 2341322..1096a23 100644 --- a/cmd/reddit.go +++ b/cmd/reddit.go @@ -1,12 +1,12 @@ package cmd import ( - "encoding/json" "fmt" "git.db.org.ai/dborg/internal/client" "git.db.org.ai/dborg/internal/config" "git.db.org.ai/dborg/internal/models" + "git.db.org.ai/dborg/internal/utils" "github.com/spf13/cobra" ) @@ -103,13 +103,7 @@ func runRedditSubredditPosts(cmd *cobra.Command, args []string) error { return fmt.Errorf("API error: %s", response.Error) } - output, err := json.MarshalIndent(response, "", " ") - if err != nil { - return fmt.Errorf("failed to format response: %w", err) - } - - fmt.Println(string(output)) - return nil + return utils.PrintJSON(response) } func runRedditSubredditComments(cmd *cobra.Command, args []string) error { @@ -134,13 +128,7 @@ func runRedditSubredditComments(cmd *cobra.Command, args []string) error { return fmt.Errorf("API error: %s", response.Error) } - output, err := json.MarshalIndent(response, "", " ") - if err != nil { - return fmt.Errorf("failed to format response: %w", err) - } - - fmt.Println(string(output)) - return nil + return utils.PrintJSON(response) } func runRedditUserPosts(cmd *cobra.Command, args []string) error { @@ -165,13 +153,7 @@ func runRedditUserPosts(cmd *cobra.Command, args []string) error { return fmt.Errorf("API error: %s", response.Error) } - output, err := json.MarshalIndent(response, "", " ") - if err != nil { - return fmt.Errorf("failed to format response: %w", err) - } - - fmt.Println(string(output)) - return nil + return utils.PrintJSON(response) } func runRedditUserComments(cmd *cobra.Command, args []string) error { @@ -196,13 +178,7 @@ func runRedditUserComments(cmd *cobra.Command, args []string) error { return fmt.Errorf("API error: %s", response.Error) } - output, err := json.MarshalIndent(response, "", " ") - if err != nil { - return fmt.Errorf("failed to format response: %w", err) - } - - fmt.Println(string(output)) - return nil + return utils.PrintJSON(response) } func runRedditUserAbout(cmd *cobra.Command, args []string) error { @@ -227,11 +203,5 @@ func runRedditUserAbout(cmd *cobra.Command, args []string) error { return fmt.Errorf("API error: %s", response.Error) } - output, err := json.MarshalIndent(response, "", " ") - if err != nil { - return fmt.Errorf("failed to format response: %w", err) - } - - fmt.Println(string(output)) - return nil + return utils.PrintJSON(response) } diff --git a/cmd/skiptrace.go b/cmd/skiptrace.go index 6b0ad0b..a9ab862 100644 --- a/cmd/skiptrace.go +++ b/cmd/skiptrace.go @@ -1,11 +1,11 @@ package cmd import ( - "encoding/json" "fmt" "git.db.org.ai/dborg/internal/client" "git.db.org.ai/dborg/internal/config" "git.db.org.ai/dborg/internal/models" + "git.db.org.ai/dborg/internal/utils" "strconv" "github.com/spf13/cobra" @@ -92,11 +92,7 @@ func runSkiptracePeople(cmd *cobra.Command, args []string) error { } if response.Data != nil && len(response.Data) > 0 { - output, err := json.MarshalIndent(response.Data, "", " ") - if err != nil { - return fmt.Errorf("failed to format response: %w", err) - } - fmt.Println(string(output)) + return utils.PrintJSON(response.Data) } return nil @@ -124,11 +120,9 @@ func runSkiptraceReport(cmd *cobra.Command, args []string) error { } if response.Data != nil && len(response.Data) > 0 { - output, err := json.MarshalIndent(response.Data, "", " ") - if err != nil { - return fmt.Errorf("failed to format response: %w", err) + if err := utils.PrintJSON(response.Data); err != nil { + return err } - fmt.Println(string(output)) } if response.Message != "" { @@ -154,11 +148,9 @@ func runSkiptracePhone(cmd *cobra.Command, args []string) error { } if response.Data != nil && len(response.Data) > 0 { - output, err := json.MarshalIndent(response.Data, "", " ") - if err != nil { - return fmt.Errorf("failed to format response: %w", err) + if err := utils.PrintJSON(response.Data); err != nil { + return err } - fmt.Println(string(output)) } if response.Message != "" { @@ -184,11 +176,9 @@ func runSkiptraceEmail(cmd *cobra.Command, args []string) error { } if response.Data != nil && len(response.Data) > 0 { - output, err := json.MarshalIndent(response.Data, "", " ") - if err != nil { - return fmt.Errorf("failed to format response: %w", err) + if err := utils.PrintJSON(response.Data); err != nil { + return err } - fmt.Println(string(output)) } if response.Message != "" { diff --git a/cmd/sl.go b/cmd/sl.go index 82adfdf..cabd4ce 100644 --- a/cmd/sl.go +++ b/cmd/sl.go @@ -1,11 +1,11 @@ package cmd import ( - "encoding/json" "fmt" "git.db.org.ai/dborg/internal/client" "git.db.org.ai/dborg/internal/config" "git.db.org.ai/dborg/internal/models" + "git.db.org.ai/dborg/internal/utils" "github.com/spf13/cobra" ) @@ -63,11 +63,5 @@ func runSLSearch(cmd *cobra.Command, args []string) error { return nil } - output, err := json.MarshalIndent(response.Results, "", " ") - if err != nil { - return fmt.Errorf("failed to format response: %w", err) - } - - fmt.Println(string(output)) - return nil + return utils.PrintJSON(response.Results) } diff --git a/cmd/x.go b/cmd/x.go index b933e3d..c88243b 100644 --- a/cmd/x.go +++ b/cmd/x.go @@ -5,6 +5,7 @@ import ( "fmt" "git.db.org.ai/dborg/internal/client" "git.db.org.ai/dborg/internal/config" + "git.db.org.ai/dborg/internal/utils" "github.com/spf13/cobra" ) @@ -56,19 +57,11 @@ func runXHistorySearch(cmd *cobra.Command, args []string) error { } if len(response.PreviousUsernames) > 0 { - output, err := json.MarshalIndent(response.PreviousUsernames, "", " ") - if err != nil { - return fmt.Errorf("failed to format response: %w", err) - } - fmt.Println(string(output)) + return utils.PrintJSON(response.PreviousUsernames) } else if response.Response != "" { fmt.Println(response.Response) } else if response.Data != nil { - output, err := json.MarshalIndent(response.Data, "", " ") - if err != nil { - return fmt.Errorf("failed to format response: %w", err) - } - fmt.Println(string(output)) + return utils.PrintJSON(response.Data) } else { fmt.Println("No username history found") } -- cgit v1.2.3