diff options
Diffstat (limited to 'cmd/x.go')
| -rw-r--r-- | cmd/x.go | 67 |
1 files changed, 53 insertions, 14 deletions
@@ -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 }) |
