From 344a6f6415c3c1b593677adec3b8844e0839971b Mon Sep 17 00:00:00 2001 From: s Date: Thu, 13 Nov 2025 14:43:15 -0500 Subject: created pretty printing for all commands --- cmd/admin.go | 58 +++++++++++++++++++++++++++++------------------- cmd/dns.go | 12 +++++++++- cmd/npd.go | 11 +++++++--- cmd/osint.go | 29 +++++++++++++++--------- cmd/reddit.go | 37 ++++++++++++++++++++++++++----- cmd/root.go | 9 ++++++++ cmd/skiptrace.go | 48 ++++++++++++++++++---------------------- cmd/sl.go | 11 +++++++--- cmd/x.go | 67 ++++++++++++++++++++++++++++++++++++++++++++------------ 9 files changed, 196 insertions(+), 86 deletions(-) (limited to 'cmd') diff --git a/cmd/admin.go b/cmd/admin.go index 82e2c29..3c20068 100644 --- a/cmd/admin.go +++ b/cmd/admin.go @@ -4,8 +4,8 @@ import ( "fmt" "git.db.org.ai/dborg/internal/client" "git.db.org.ai/dborg/internal/config" + "git.db.org.ai/dborg/internal/formatter" "git.db.org.ai/dborg/internal/models" - "git.db.org.ai/dborg/internal/utils" "strconv" "github.com/spf13/cobra" @@ -93,7 +93,13 @@ func runAdminList(cmd *cobra.Command, args []string) error { return fmt.Errorf("API error: %s", response.Error) } - return utils.PrintJSON(response.Accounts) + output, err := formatter.FormatAccountList(response.Accounts, IsJSONOutput()) + if err != nil { + return err + } + + fmt.Print(output) + return nil } func runAdminCreate(cmd *cobra.Command, args []string) error { @@ -122,17 +128,12 @@ func runAdminCreate(cmd *cobra.Command, args []string) error { return fmt.Errorf("API error: %s", response.Error) } - if response.Account != nil { - fmt.Printf("Account created successfully!\n") - fmt.Printf("Name: %s\n", response.Account.Name) - fmt.Printf("API Key: %s\n", response.Account.APIKey) - fmt.Printf("Credits: %d\n", response.Account.Credits) - fmt.Printf("Unlimited: %v\n", response.Account.Unlimited) - fmt.Printf("Premium: %v\n", response.Account.IsPremium) - fmt.Printf("Disabled: %v\n", response.Account.Disabled) - } else { - fmt.Println(response.Message) + output, err := formatter.FormatAccountCreated(response.Account, response.Message, IsJSONOutput()) + if err != nil { + return err } + + fmt.Print(output) return nil } @@ -151,7 +152,12 @@ func runAdminDelete(cmd *cobra.Command, args []string) error { return fmt.Errorf("API error: %s", response.Error) } - fmt.Println(response.Message) + output, err := formatter.FormatAccountDeleted(response.Message, IsJSONOutput()) + if err != nil { + return err + } + + fmt.Print(output) return nil } @@ -175,7 +181,12 @@ func runAdminCredits(cmd *cobra.Command, args []string) error { return fmt.Errorf("API error: %s", response.Error) } - fmt.Println(response.Message) + output, err := formatter.FormatCreditsUpdated(response.Message, nil, IsJSONOutput()) + if err != nil { + return err + } + + fmt.Print(output) return nil } @@ -199,14 +210,12 @@ func runAdminSetCredits(cmd *cobra.Command, args []string) error { return fmt.Errorf("API error: %s", response.Error) } - if response.Account != nil { - fmt.Printf("Credits set successfully!\n") - fmt.Printf("Account: %s\n", response.Account.Name) - fmt.Printf("API Key: %s\n", response.Account.APIKey) - fmt.Printf("Credits: %d\n", response.Account.Credits) - } else if response.Message != "" { - fmt.Println(response.Message) + output, err := formatter.FormatCreditsUpdated(response.Message, response.Account, IsJSONOutput()) + if err != nil { + return err } + + fmt.Print(output) return nil } @@ -226,6 +235,11 @@ func runAdminDisable(cmd *cobra.Command, args []string) error { return fmt.Errorf("API error: %s", response.Error) } - fmt.Println(response.Message) + output, err := formatter.FormatAccountToggled(response.Message, IsJSONOutput()) + if err != nil { + return err + } + + fmt.Print(output) return nil } diff --git a/cmd/dns.go b/cmd/dns.go index d6776ee..130f394 100644 --- a/cmd/dns.go +++ b/cmd/dns.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/formatter" "git.db.org.ai/dborg/internal/models" "github.com/spf13/cobra" ) @@ -42,7 +43,16 @@ func runDNSTLDCheck(cmd *cobra.Command, args []string) error { fmt.Printf("Checking TLDs for term: %s\n\n", term) err = c.CheckDNSTLDStream(params, func(result json.RawMessage) error { - fmt.Println(string(result)) + var domainResult models.DomainResult + if err := json.Unmarshal(result, &domainResult); err != nil { + return fmt.Errorf("failed to parse result: %w", err) + } + + output, err := formatter.FormatDNSResults(&domainResult, IsJSONOutput()) + if err != nil { + return err + } + fmt.Print(output) return nil }) diff --git a/cmd/npd.go b/cmd/npd.go index 9868eae..c8b0b28 100644 --- a/cmd/npd.go +++ b/cmd/npd.go @@ -2,11 +2,11 @@ package cmd import ( "fmt" + "git.db.org.ai/dborg/internal/client" "git.db.org.ai/dborg/internal/config" + "git.db.org.ai/dborg/internal/formatter" "git.db.org.ai/dborg/internal/models" - "git.db.org.ai/dborg/internal/utils" - "github.com/spf13/cobra" ) @@ -84,5 +84,10 @@ func runNPDSearch(cmd *cobra.Command, args []string) error { return fmt.Errorf("API error: %s", response.Error) } - return utils.PrintJSON(response.Results.Hits) + output, err := formatter.FormatNPDResults(response, IsJSONOutput()) + if err != nil { + return err + } + fmt.Print(output) + return nil } diff --git a/cmd/osint.go b/cmd/osint.go index 3c3d0de..e29ef54 100644 --- a/cmd/osint.go +++ b/cmd/osint.go @@ -5,8 +5,8 @@ import ( "fmt" "git.db.org.ai/dborg/internal/client" "git.db.org.ai/dborg/internal/config" + "git.db.org.ai/dborg/internal/formatter" "git.db.org.ai/dborg/internal/models" - "git.db.org.ai/dborg/internal/utils" "github.com/spf13/cobra" ) @@ -149,8 +149,17 @@ func runUsernameCheck(cmd *cobra.Command, args []string) error { params.MaxTasks, _ = cmd.Flags().GetInt("max_tasks") err = c.CheckUsernameStream(params, func(result json.RawMessage) error { - fmt.Println(string(result)) - return nil + if IsJSONOutput() { + fmt.Println(string(result)) + return nil + } + + var siteResult models.SiteResult + if err := json.Unmarshal(result, &siteResult); err != nil { + return err + } + + return formatter.FormatUsernameSiteResult(&siteResult) }) if err != nil { @@ -180,7 +189,7 @@ func runBSSIDLookup(cmd *cobra.Command, args []string) error { return err } - return utils.PrintJSON(response) + return formatter.FormatBSSIDResults(*response, IsJSONOutput()) } func runBreachForumSearch(cmd *cobra.Command, args []string) error { @@ -201,7 +210,7 @@ func runBreachForumSearch(cmd *cobra.Command, args []string) error { return err } - return utils.PrintJSON(response) + return formatter.FormatBreachForumResults(response, IsJSONOutput()) } func runFilesSearch(cmd *cobra.Command, args []string) error { @@ -226,7 +235,7 @@ func runFilesSearch(cmd *cobra.Command, args []string) error { return err } - return utils.PrintJSON(response) + return formatter.FormatFilesResults(*response, IsJSONOutput()) } func runBucketsSearch(cmd *cobra.Command, args []string) error { @@ -246,7 +255,7 @@ func runBucketsSearch(cmd *cobra.Command, args []string) error { return err } - return utils.PrintJSON(response) + return formatter.FormatBucketsResults(response, IsJSONOutput()) } func runBucketFilesSearch(cmd *cobra.Command, args []string) error { @@ -269,7 +278,7 @@ func runBucketFilesSearch(cmd *cobra.Command, args []string) error { return err } - return utils.PrintJSON(response) + return formatter.FormatBucketFilesResults(response, IsJSONOutput()) } func runShortlinksSearch(cmd *cobra.Command, args []string) error { @@ -294,7 +303,7 @@ func runShortlinksSearch(cmd *cobra.Command, args []string) error { return err } - return utils.PrintJSON(response) + return formatter.FormatShortlinksResults(response, IsJSONOutput()) } func runGeoSearch(cmd *cobra.Command, args []string) error { @@ -316,7 +325,7 @@ func runGeoSearch(cmd *cobra.Command, args []string) error { return err } - return utils.PrintJSON(response) + return formatter.FormatGeoResults(*response, IsJSONOutput()) } func runCrawl(cmd *cobra.Command, args []string) error { diff --git a/cmd/reddit.go b/cmd/reddit.go index 194fdab..5969f7c 100644 --- a/cmd/reddit.go +++ b/cmd/reddit.go @@ -5,8 +5,8 @@ import ( "git.db.org.ai/dborg/internal/client" "git.db.org.ai/dborg/internal/config" + "git.db.org.ai/dborg/internal/formatter" "git.db.org.ai/dborg/internal/models" - "git.db.org.ai/dborg/internal/utils" "github.com/spf13/cobra" ) @@ -102,7 +102,12 @@ func runRedditSubredditPosts(cmd *cobra.Command, args []string) error { return fmt.Errorf("API error: %s", response.Error) } - return utils.PrintJSON(response) + output, err := formatter.FormatRedditResults(response, IsJSONOutput()) + if err != nil { + return err + } + fmt.Print(output) + return nil } func runRedditSubredditComments(cmd *cobra.Command, args []string) error { @@ -126,7 +131,12 @@ func runRedditSubredditComments(cmd *cobra.Command, args []string) error { return fmt.Errorf("API error: %s", response.Error) } - return utils.PrintJSON(response) + output, err := formatter.FormatRedditResults(response, IsJSONOutput()) + if err != nil { + return err + } + fmt.Print(output) + return nil } func runRedditUserPosts(cmd *cobra.Command, args []string) error { @@ -150,7 +160,12 @@ func runRedditUserPosts(cmd *cobra.Command, args []string) error { return fmt.Errorf("API error: %s", response.Error) } - return utils.PrintJSON(response) + output, err := formatter.FormatRedditResults(response, IsJSONOutput()) + if err != nil { + return err + } + fmt.Print(output) + return nil } func runRedditUserComments(cmd *cobra.Command, args []string) error { @@ -174,7 +189,12 @@ func runRedditUserComments(cmd *cobra.Command, args []string) error { return fmt.Errorf("API error: %s", response.Error) } - return utils.PrintJSON(response) + output, err := formatter.FormatRedditResults(response, IsJSONOutput()) + if err != nil { + return err + } + fmt.Print(output) + return nil } func runRedditUserAbout(cmd *cobra.Command, args []string) error { @@ -198,5 +218,10 @@ func runRedditUserAbout(cmd *cobra.Command, args []string) error { return fmt.Errorf("API error: %s", response.Error) } - return utils.PrintJSON(response) + output, err := formatter.FormatRedditResults(response, IsJSONOutput()) + if err != nil { + return err + } + fmt.Print(output) + return nil } diff --git a/cmd/root.go b/cmd/root.go index 890d336..c4239ba 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -8,6 +8,10 @@ import ( "github.com/spf13/cobra" ) +var ( + jsonOutput bool +) + var rootCmd = &cobra.Command{ Use: "dborg", Short: "DB.org.ai CLI client", @@ -28,4 +32,9 @@ func Execute() { } func init() { + rootCmd.PersistentFlags().BoolVarP(&jsonOutput, "json", "j", false, "Output raw JSON instead of formatted text") +} + +func IsJSONOutput() bool { + return jsonOutput } diff --git a/cmd/skiptrace.go b/cmd/skiptrace.go index 9ce1b4b..307204c 100644 --- a/cmd/skiptrace.go +++ b/cmd/skiptrace.go @@ -4,8 +4,8 @@ import ( "fmt" "git.db.org.ai/dborg/internal/client" "git.db.org.ai/dborg/internal/config" + "git.db.org.ai/dborg/internal/formatter" "git.db.org.ai/dborg/internal/models" - "git.db.org.ai/dborg/internal/utils" "strconv" "github.com/spf13/cobra" @@ -90,10 +90,16 @@ func runSkiptracePeople(cmd *cobra.Command, args []string) error { return err } - if response.Data != nil && len(response.Data) > 0 { - return utils.PrintJSON(response.Data) + if response.Error != "" { + return fmt.Errorf("API error: %s", response.Error) } + output, err := formatter.FormatSkiptraceResults(response, IsJSONOutput()) + if err != nil { + return err + } + + fmt.Print(output) return nil } @@ -118,16 +124,12 @@ func runSkiptraceReport(cmd *cobra.Command, args []string) error { return fmt.Errorf("API error: %s", response.Error) } - if response.Data != nil && len(response.Data) > 0 { - if err := utils.PrintJSON(response.Data); err != nil { - return err - } - } - - if response.Message != "" { - fmt.Println(response.Message) + output, err := formatter.FormatSkiptraceResults(response, IsJSONOutput()) + if err != nil { + return err } + fmt.Print(output) return nil } @@ -146,16 +148,12 @@ func runSkiptracePhone(cmd *cobra.Command, args []string) error { return fmt.Errorf("API error: %s", response.Error) } - if response.Data != nil && len(response.Data) > 0 { - if err := utils.PrintJSON(response.Data); err != nil { - return err - } - } - - if response.Message != "" { - fmt.Println(response.Message) + output, err := formatter.FormatSkiptraceResults(response, IsJSONOutput()) + if err != nil { + return err } + fmt.Print(output) return nil } @@ -174,15 +172,11 @@ func runSkiptraceEmail(cmd *cobra.Command, args []string) error { return fmt.Errorf("API error: %s", response.Error) } - if response.Data != nil && len(response.Data) > 0 { - if err := utils.PrintJSON(response.Data); err != nil { - return err - } - } - - if response.Message != "" { - fmt.Println(response.Message) + output, err := formatter.FormatSkiptraceResults(response, IsJSONOutput()) + if err != nil { + return err } + fmt.Print(output) return nil } diff --git a/cmd/sl.go b/cmd/sl.go index 1efa9e2..c30725a 100644 --- a/cmd/sl.go +++ b/cmd/sl.go @@ -2,11 +2,11 @@ package cmd import ( "fmt" + "git.db.org.ai/dborg/internal/client" "git.db.org.ai/dborg/internal/config" + "git.db.org.ai/dborg/internal/formatter" "git.db.org.ai/dborg/internal/models" - "git.db.org.ai/dborg/internal/utils" - "github.com/spf13/cobra" ) @@ -66,5 +66,10 @@ func runSLSearch(cmd *cobra.Command, args []string) error { return nil } - return utils.PrintJSON(response.Results) + output, err := formatter.FormatSLResults(response, IsJSONOutput()) + if err != nil { + return err + } + fmt.Print(output) + return nil } diff --git a/cmd/x.go b/cmd/x.go index 0820b27..ba18aa8 100644 --- a/cmd/x.go +++ b/cmd/x.go @@ -5,7 +5,8 @@ import ( "fmt" "git.db.org.ai/dborg/internal/client" "git.db.org.ai/dborg/internal/config" - "git.db.org.ai/dborg/internal/utils" + "git.db.org.ai/dborg/internal/formatter" + "git.db.org.ai/dborg/internal/models" "github.com/spf13/cobra" ) @@ -94,16 +95,12 @@ func runXHistorySearch(cmd *cobra.Command, args []string) error { return fmt.Errorf("API error: %s", response.Error) } - if len(response.PreviousUsernames) > 0 { - return utils.PrintJSON(response.PreviousUsernames) - } else if response.Response != "" { - fmt.Println(response.Response) - } else if response.Data != nil { - return utils.PrintJSON(response.Data) - } else { - fmt.Println("No username history found") + output, err := formatter.FormatXHistory(response, IsJSONOutput()) + if err != nil { + return err } + fmt.Print(output) return nil } @@ -116,7 +113,17 @@ func runXTweetsSearch(cmd *cobra.Command, args []string) error { } err = c.FetchTweetsStream(args[0], func(result json.RawMessage) error { - fmt.Println(string(result)) + var streamResp models.TweetsStreamResponse + if err := json.Unmarshal(result, &streamResp); err != nil { + return err + } + + output, err := formatter.FormatXTweets(&streamResp, IsJSONOutput()) + if err != nil { + return err + } + + fmt.Print(output) return nil }) @@ -140,7 +147,13 @@ func runXFirstFollowers(cmd *cobra.Command, args []string) error { return err } - return utils.PrintJSON(response) + output, err := formatter.FormatXFirstFollowers(response, IsJSONOutput()) + if err != nil { + return err + } + + fmt.Print(output) + return nil } func runXNotableFollowers(cmd *cobra.Command, args []string) error { @@ -156,7 +169,13 @@ func runXNotableFollowers(cmd *cobra.Command, args []string) error { return err } - return utils.PrintJSON(response) + output, err := formatter.FormatXNotableFollowers(response, IsJSONOutput()) + if err != nil { + return err + } + + fmt.Print(output) + return nil } func runXReplies(cmd *cobra.Command, args []string) error { @@ -169,7 +188,17 @@ func runXReplies(cmd *cobra.Command, args []string) error { } err = c.FetchRepliesStream(args[0], limit, func(result json.RawMessage) error { - fmt.Println(string(result)) + var streamResp models.TweetsStreamResponse + if err := json.Unmarshal(result, &streamResp); err != nil { + return err + } + + output, err := formatter.FormatXReplies(&streamResp, IsJSONOutput()) + if err != nil { + return err + } + + fmt.Print(output) return nil }) @@ -190,7 +219,17 @@ func runXSearch(cmd *cobra.Command, args []string) error { } err = c.SearchTweetsStream(args[0], limit, func(result json.RawMessage) error { - fmt.Println(string(result)) + var streamResp models.TweetsStreamResponse + if err := json.Unmarshal(result, &streamResp); err != nil { + return err + } + + output, err := formatter.FormatXSearch(&streamResp, IsJSONOutput()) + if err != nil { + return err + } + + fmt.Print(output) return nil }) -- cgit v1.2.3