From 4486b6659640102dd542fea007f4c33ac02511ff Mon Sep 17 00:00:00 2001 From: s Date: Tue, 4 Nov 2025 11:06:35 -0500 Subject: feat: add version checking and auto-update functionality --- cmd/reddit.go | 237 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ cmd/root.go | 4 + cmd/version.go | 21 +++++ 3 files changed, 262 insertions(+) create mode 100644 cmd/reddit.go create mode 100644 cmd/version.go (limited to 'cmd') diff --git a/cmd/reddit.go b/cmd/reddit.go new file mode 100644 index 0000000..2341322 --- /dev/null +++ b/cmd/reddit.go @@ -0,0 +1,237 @@ +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 redditCmd = &cobra.Command{ + Use: "reddit", + Short: "Reddit data retrieval tools", + Long: `Retrieve posts, comments, and user information from Reddit`, +} + +var redditSubredditCmd = &cobra.Command{ + Use: "subreddit", + Short: "Get subreddit data", + Long: `Retrieve posts or comments from a subreddit`, +} + +var redditUserCmd = &cobra.Command{ + Use: "user", + Short: "Get Reddit user data", + Long: `Retrieve posts, comments, or profile information for a Reddit user`, +} + +var redditSubredditPostsCmd = &cobra.Command{ + Use: "posts [subreddit]", + Short: "Get subreddit posts", + Long: `Get up to 1000 recent posts from a subreddit`, + Args: cobra.ExactArgs(1), + RunE: runRedditSubredditPosts, +} + +var redditSubredditCommentsCmd = &cobra.Command{ + Use: "comments [subreddit]", + Short: "Get subreddit comments", + Long: `Get up to 1000 recent comments from a subreddit`, + Args: cobra.ExactArgs(1), + RunE: runRedditSubredditComments, +} + +var redditUserPostsCmd = &cobra.Command{ + Use: "posts [username]", + Short: "Get user posts", + Long: `Get up to 1000 recent posts from a Reddit user`, + Args: cobra.ExactArgs(1), + RunE: runRedditUserPosts, +} + +var redditUserCommentsCmd = &cobra.Command{ + Use: "comments [username]", + Short: "Get user comments", + Long: `Get up to 1000 recent comments from a Reddit user`, + Args: cobra.ExactArgs(1), + RunE: runRedditUserComments, +} + +var redditUserAboutCmd = &cobra.Command{ + Use: "about [username]", + Short: "Get user profile", + Long: `Get profile information for a Reddit user`, + Args: cobra.ExactArgs(1), + RunE: runRedditUserAbout, +} + +func init() { + rootCmd.AddCommand(redditCmd) + redditCmd.AddCommand(redditSubredditCmd) + redditCmd.AddCommand(redditUserCmd) + + redditSubredditCmd.AddCommand(redditSubredditPostsCmd) + redditSubredditCmd.AddCommand(redditSubredditCommentsCmd) + + redditUserCmd.AddCommand(redditUserPostsCmd) + redditUserCmd.AddCommand(redditUserCommentsCmd) + redditUserCmd.AddCommand(redditUserAboutCmd) +} + +func runRedditSubredditPosts(cmd *cobra.Command, args []string) error { + apiKey, _ := cmd.Flags().GetString("api-key") + cfg := config.New().WithAPIKey(apiKey) + + c, err := client.New(cfg) + if err != nil { + return err + } + + params := &models.RedditSubredditParams{ + Subreddit: args[0], + } + + response, err := c.GetSubredditPosts(params) + if err != nil { + return err + } + + if response.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 +} + +func runRedditSubredditComments(cmd *cobra.Command, args []string) error { + apiKey, _ := cmd.Flags().GetString("api-key") + cfg := config.New().WithAPIKey(apiKey) + + c, err := client.New(cfg) + if err != nil { + return err + } + + params := &models.RedditSubredditParams{ + Subreddit: args[0], + } + + response, err := c.GetSubredditComments(params) + if err != nil { + return err + } + + if response.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 +} + +func runRedditUserPosts(cmd *cobra.Command, args []string) error { + apiKey, _ := cmd.Flags().GetString("api-key") + cfg := config.New().WithAPIKey(apiKey) + + c, err := client.New(cfg) + if err != nil { + return err + } + + params := &models.RedditUserParams{ + Username: args[0], + } + + response, err := c.GetUserPosts(params) + if err != nil { + return err + } + + if response.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 +} + +func runRedditUserComments(cmd *cobra.Command, args []string) error { + apiKey, _ := cmd.Flags().GetString("api-key") + cfg := config.New().WithAPIKey(apiKey) + + c, err := client.New(cfg) + if err != nil { + return err + } + + params := &models.RedditUserParams{ + Username: args[0], + } + + response, err := c.GetUserComments(params) + if err != nil { + return err + } + + if response.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 +} + +func runRedditUserAbout(cmd *cobra.Command, args []string) error { + apiKey, _ := cmd.Flags().GetString("api-key") + cfg := config.New().WithAPIKey(apiKey) + + c, err := client.New(cfg) + if err != nil { + return err + } + + params := &models.RedditUserParams{ + Username: args[0], + } + + response, err := c.GetUserAbout(params) + if err != nil { + return err + } + + if response.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 +} diff --git a/cmd/root.go b/cmd/root.go index b8ab9e4..cc74436 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -4,6 +4,7 @@ import ( "fmt" "os" + "git.db.org.ai/dborg/internal/utils" "github.com/spf13/cobra" ) @@ -11,6 +12,9 @@ var rootCmd = &cobra.Command{ Use: "dborg", Short: "DB.org.ai CLI client", Long: `Query db.org.ai API`, + PersistentPreRunE: func(cmd *cobra.Command, args []string) error { + return utils.CheckForUpdates(cmd) + }, } func Execute() { diff --git a/cmd/version.go b/cmd/version.go new file mode 100644 index 0000000..bdb3331 --- /dev/null +++ b/cmd/version.go @@ -0,0 +1,21 @@ +package cmd + +import ( + "fmt" + + "git.db.org.ai/dborg/internal/utils" + "github.com/spf13/cobra" +) + +var versionCmd = &cobra.Command{ + Use: "version", + Short: "Print the version number of dborg", + Long: `Display the current version of the dborg CLI`, + Run: func(cmd *cobra.Command, args []string) { + fmt.Printf("dborg version %s\n", utils.Version) + }, +} + +func init() { + rootCmd.AddCommand(versionCmd) +} -- cgit v1.2.3