From 8383a241fc3cf5b022c9c53f8f19690edf04177b Mon Sep 17 00:00:00 2001 From: s Date: Mon, 10 Nov 2025 15:22:32 -0500 Subject: refactor: restructure client modules and add config file support - Split large osint.go client into focused modules (bssid.go, breachforum.go, buckets.go, etc.) - Add config file support with init command for API key management - Remove api-key flag in favor of config file + env var fallback - Update API paths to remove /osint prefix - Add crawl endpoint streaming support - Improve error handling with 402 payment required status --- cmd/admin.go | 3 +-- cmd/init.go | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ cmd/npd.go | 3 +-- cmd/osint.go | 24 ++++------------------- cmd/reddit.go | 15 +++++--------- cmd/root.go | 1 - cmd/skiptrace.go | 3 +-- cmd/sl.go | 3 +-- cmd/x.go | 15 +++++--------- 9 files changed, 77 insertions(+), 49 deletions(-) create mode 100644 cmd/init.go (limited to 'cmd') diff --git a/cmd/admin.go b/cmd/admin.go index e0f0653..82e2c29 100644 --- a/cmd/admin.go +++ b/cmd/admin.go @@ -74,8 +74,7 @@ func init() { } func getAdminClient(cmd *cobra.Command) (*client.Client, error) { - apiKey, _ := cmd.Flags().GetString("api-key") - cfg := config.New().WithAPIKey(apiKey) + cfg := config.New() return client.New(cfg) } diff --git a/cmd/init.go b/cmd/init.go new file mode 100644 index 0000000..954dd31 --- /dev/null +++ b/cmd/init.go @@ -0,0 +1,59 @@ +package cmd + +import ( + "bufio" + "fmt" + "os" + "strings" + + "git.db.org.ai/dborg/internal/config" + "github.com/spf13/cobra" +) + +var initCmd = &cobra.Command{ + Use: "init", + Short: "Initialize dborg CLI configuration", + Long: `Initialize the dborg CLI by setting up your API key and other configuration`, + RunE: runInit, +} + +func init() { + rootCmd.AddCommand(initCmd) +} + +func runInit(cmd *cobra.Command, args []string) error { + reader := bufio.NewReader(os.Stdin) + + fmt.Println("Welcome to dborg CLI setup!") + fmt.Println("----------------------------") + fmt.Print("\nEnter your DB.org.ai API key: ") + + apiKey, err := reader.ReadString('\n') + if err != nil { + return fmt.Errorf("failed to read API key: %w", err) + } + + apiKey = strings.TrimSpace(apiKey) + if apiKey == "" { + return fmt.Errorf("API key cannot be empty") + } + + configPath, err := config.GetConfigPath() + if err != nil { + return fmt.Errorf("failed to get config path: %w", err) + } + + cfg := &config.FileConfig{ + APIKey: apiKey, + } + + if err := config.SaveConfig(cfg); err != nil { + return fmt.Errorf("failed to save config: %w", err) + } + + fmt.Printf("\n✓ Configuration saved to: %s\n", configPath) + fmt.Println("\nYou can now use dborg commands without specifying an API key.") + fmt.Println("Example: dborg osint username john_doe") + + return nil +} diff --git a/cmd/npd.go b/cmd/npd.go index a6bd7b2..9868eae 100644 --- a/cmd/npd.go +++ b/cmd/npd.go @@ -44,8 +44,7 @@ func init() { } func runNPDSearch(cmd *cobra.Command, args []string) error { - apiKey, _ := cmd.Flags().GetString("api-key") - cfg := config.New().WithAPIKey(apiKey) + cfg := config.New() c, err := client.New(cfg) if err != nil { diff --git a/cmd/osint.go b/cmd/osint.go index 33b5416..9f2cfef 100644 --- a/cmd/osint.go +++ b/cmd/osint.go @@ -228,11 +228,7 @@ func runOsintFilesSearch(cmd *cobra.Command, args []string) error { } func runOsintBucketsSearch(cmd *cobra.Command, args []string) error { - apiKey, _ := cmd.Flags().GetString("api-key") - if apiKey == "" { - return fmt.Errorf("API key required for buckets endpoint") - } - cfg := config.New().WithAPIKey(apiKey) + cfg := config.New() c, err := client.New(cfg) if err != nil { @@ -252,11 +248,7 @@ func runOsintBucketsSearch(cmd *cobra.Command, args []string) error { } func runOsintBucketFilesSearch(cmd *cobra.Command, args []string) error { - apiKey, _ := cmd.Flags().GetString("api-key") - if apiKey == "" { - return fmt.Errorf("API key required for bucket files endpoint") - } - cfg := config.New().WithAPIKey(apiKey) + cfg := config.New() c, err := client.New(cfg) if err != nil { @@ -279,11 +271,7 @@ func runOsintBucketFilesSearch(cmd *cobra.Command, args []string) error { } func runOsintShortlinksSearch(cmd *cobra.Command, args []string) error { - apiKey, _ := cmd.Flags().GetString("api-key") - if apiKey == "" { - return fmt.Errorf("API key required for shortlinks endpoint") - } - cfg := config.New().WithAPIKey(apiKey) + cfg := config.New() c, err := client.New(cfg) if err != nil { @@ -308,11 +296,7 @@ func runOsintShortlinksSearch(cmd *cobra.Command, args []string) error { } func runOsintGeoSearch(cmd *cobra.Command, args []string) error { - apiKey, _ := cmd.Flags().GetString("api-key") - if apiKey == "" { - return fmt.Errorf("API key required for geo endpoint (costs 1 credit)") - } - cfg := config.New().WithAPIKey(apiKey) + cfg := config.New() c, err := client.New(cfg) if err != nil { diff --git a/cmd/reddit.go b/cmd/reddit.go index 1096a23..194fdab 100644 --- a/cmd/reddit.go +++ b/cmd/reddit.go @@ -82,8 +82,7 @@ func init() { } func runRedditSubredditPosts(cmd *cobra.Command, args []string) error { - apiKey, _ := cmd.Flags().GetString("api-key") - cfg := config.New().WithAPIKey(apiKey) + cfg := config.New() c, err := client.New(cfg) if err != nil { @@ -107,8 +106,7 @@ func runRedditSubredditPosts(cmd *cobra.Command, args []string) error { } func runRedditSubredditComments(cmd *cobra.Command, args []string) error { - apiKey, _ := cmd.Flags().GetString("api-key") - cfg := config.New().WithAPIKey(apiKey) + cfg := config.New() c, err := client.New(cfg) if err != nil { @@ -132,8 +130,7 @@ func runRedditSubredditComments(cmd *cobra.Command, args []string) error { } func runRedditUserPosts(cmd *cobra.Command, args []string) error { - apiKey, _ := cmd.Flags().GetString("api-key") - cfg := config.New().WithAPIKey(apiKey) + cfg := config.New() c, err := client.New(cfg) if err != nil { @@ -157,8 +154,7 @@ func runRedditUserPosts(cmd *cobra.Command, args []string) error { } func runRedditUserComments(cmd *cobra.Command, args []string) error { - apiKey, _ := cmd.Flags().GetString("api-key") - cfg := config.New().WithAPIKey(apiKey) + cfg := config.New() c, err := client.New(cfg) if err != nil { @@ -182,8 +178,7 @@ func runRedditUserComments(cmd *cobra.Command, args []string) error { } func runRedditUserAbout(cmd *cobra.Command, args []string) error { - apiKey, _ := cmd.Flags().GetString("api-key") - cfg := config.New().WithAPIKey(apiKey) + cfg := config.New() c, err := client.New(cfg) if err != nil { diff --git a/cmd/root.go b/cmd/root.go index e5939ca..890d336 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -28,5 +28,4 @@ func Execute() { } func init() { - rootCmd.PersistentFlags().StringP("api-key", "k", "", "API key for authentication (or set DBORG_API_KEY env var)") } diff --git a/cmd/skiptrace.go b/cmd/skiptrace.go index a9ab862..9ce1b4b 100644 --- a/cmd/skiptrace.go +++ b/cmd/skiptrace.go @@ -68,8 +68,7 @@ func init() { } func getSkiptraceClient(cmd *cobra.Command) (*client.Client, error) { - apiKey, _ := cmd.Flags().GetString("api-key") - cfg := config.New().WithAPIKey(apiKey) + cfg := config.New() return client.New(cfg) } diff --git a/cmd/sl.go b/cmd/sl.go index 6a6af63..1efa9e2 100644 --- a/cmd/sl.go +++ b/cmd/sl.go @@ -30,8 +30,7 @@ func init() { } func runSLSearch(cmd *cobra.Command, args []string) error { - apiKey, _ := cmd.Flags().GetString("api-key") - cfg := config.New().WithAPIKey(apiKey) + cfg := config.New() c, err := client.New(cfg) if err != nil { diff --git a/cmd/x.go b/cmd/x.go index 3164b23..0820b27 100644 --- a/cmd/x.go +++ b/cmd/x.go @@ -78,8 +78,7 @@ func init() { } func runXHistorySearch(cmd *cobra.Command, args []string) error { - apiKey, _ := cmd.Flags().GetString("api-key") - cfg := config.New().WithAPIKey(apiKey) + cfg := config.New() c, err := client.New(cfg) if err != nil { @@ -129,8 +128,7 @@ func runXTweetsSearch(cmd *cobra.Command, args []string) error { } func runXFirstFollowers(cmd *cobra.Command, args []string) error { - apiKey, _ := cmd.Flags().GetString("api-key") - cfg := config.New().WithAPIKey(apiKey) + cfg := config.New() c, err := client.New(cfg) if err != nil { @@ -146,8 +144,7 @@ func runXFirstFollowers(cmd *cobra.Command, args []string) error { } func runXNotableFollowers(cmd *cobra.Command, args []string) error { - apiKey, _ := cmd.Flags().GetString("api-key") - cfg := config.New().WithAPIKey(apiKey) + cfg := config.New() c, err := client.New(cfg) if err != nil { @@ -163,9 +160,8 @@ func runXNotableFollowers(cmd *cobra.Command, args []string) error { } func runXReplies(cmd *cobra.Command, args []string) error { - apiKey, _ := cmd.Flags().GetString("api-key") limit, _ := cmd.Flags().GetInt("limit") - cfg := config.New().WithAPIKey(apiKey) + cfg := config.New() c, err := client.New(cfg) if err != nil { @@ -185,9 +181,8 @@ func runXReplies(cmd *cobra.Command, args []string) error { } func runXSearch(cmd *cobra.Command, args []string) error { - apiKey, _ := cmd.Flags().GetString("api-key") limit, _ := cmd.Flags().GetInt("limit") - cfg := config.New().WithAPIKey(apiKey) + cfg := config.New() c, err := client.New(cfg) if err != nil { -- cgit v1.2.3