diff options
Diffstat (limited to 'cmd/reddit.go')
| -rw-r--r-- | cmd/reddit.go | 54 |
1 files changed, 20 insertions, 34 deletions
diff --git a/cmd/reddit.go b/cmd/reddit.go index 5969f7c..bb31336 100644 --- a/cmd/reddit.go +++ b/cmd/reddit.go @@ -1,10 +1,6 @@ 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" "github.com/spf13/cobra" @@ -82,9 +78,7 @@ func init() { } func runRedditSubredditPosts(cmd *cobra.Command, args []string) error { - cfg := config.New() - - c, err := client.New(cfg) + c, err := newClient() if err != nil { return err } @@ -98,22 +92,20 @@ func runRedditSubredditPosts(cmd *cobra.Command, args []string) error { return err } - if response.Error != "" { - return fmt.Errorf("API error: %s", response.Error) + if err := checkError(response.Error); err != nil { + return err } output, err := formatter.FormatRedditResults(response, IsJSONOutput()) if err != nil { return err } - fmt.Print(output) + printOutput(output) return nil } func runRedditSubredditComments(cmd *cobra.Command, args []string) error { - cfg := config.New() - - c, err := client.New(cfg) + c, err := newClient() if err != nil { return err } @@ -127,22 +119,20 @@ func runRedditSubredditComments(cmd *cobra.Command, args []string) error { return err } - if response.Error != "" { - return fmt.Errorf("API error: %s", response.Error) + if err := checkError(response.Error); err != nil { + return err } output, err := formatter.FormatRedditResults(response, IsJSONOutput()) if err != nil { return err } - fmt.Print(output) + printOutput(output) return nil } func runRedditUserPosts(cmd *cobra.Command, args []string) error { - cfg := config.New() - - c, err := client.New(cfg) + c, err := newClient() if err != nil { return err } @@ -156,22 +146,20 @@ func runRedditUserPosts(cmd *cobra.Command, args []string) error { return err } - if response.Error != "" { - return fmt.Errorf("API error: %s", response.Error) + if err := checkError(response.Error); err != nil { + return err } output, err := formatter.FormatRedditResults(response, IsJSONOutput()) if err != nil { return err } - fmt.Print(output) + printOutput(output) return nil } func runRedditUserComments(cmd *cobra.Command, args []string) error { - cfg := config.New() - - c, err := client.New(cfg) + c, err := newClient() if err != nil { return err } @@ -185,22 +173,20 @@ func runRedditUserComments(cmd *cobra.Command, args []string) error { return err } - if response.Error != "" { - return fmt.Errorf("API error: %s", response.Error) + if err := checkError(response.Error); err != nil { + return err } output, err := formatter.FormatRedditResults(response, IsJSONOutput()) if err != nil { return err } - fmt.Print(output) + printOutput(output) return nil } func runRedditUserAbout(cmd *cobra.Command, args []string) error { - cfg := config.New() - - c, err := client.New(cfg) + c, err := newClient() if err != nil { return err } @@ -214,14 +200,14 @@ func runRedditUserAbout(cmd *cobra.Command, args []string) error { return err } - if response.Error != "" { - return fmt.Errorf("API error: %s", response.Error) + if err := checkError(response.Error); err != nil { + return err } output, err := formatter.FormatRedditResults(response, IsJSONOutput()) if err != nil { return err } - fmt.Print(output) + printOutput(output) return nil } |
