summaryrefslogtreecommitdiffstats
path: root/cmd/admin.go
diff options
context:
space:
mode:
authors <[email protected]>2025-11-03 21:17:12 -0500
committers <[email protected]>2025-11-03 21:17:12 -0500
commitf7fcfa623e670dc533bb378912829c73a3593e63 (patch)
tree910119ff7293b407affa9ff34706d627d77a3a04 /cmd/admin.go
downloaddborg-f7fcfa623e670dc533bb378912829c73a3593e63.tar.gz
dborg-f7fcfa623e670dc533bb378912829c73a3593e63.zip
hi
Diffstat (limited to 'cmd/admin.go')
-rw-r--r--cmd/admin.go238
1 files changed, 238 insertions, 0 deletions
diff --git a/cmd/admin.go b/cmd/admin.go
new file mode 100644
index 0000000..3c43e20
--- /dev/null
+++ b/cmd/admin.go
@@ -0,0 +1,238 @@
+package cmd
+
+import (
+ "dborg/internal/client"
+ "dborg/internal/config"
+ "dborg/internal/models"
+ "encoding/json"
+ "fmt"
+ "strconv"
+
+ "github.com/spf13/cobra"
+)
+
+var adminCmd = &cobra.Command{
+ Use: "admin",
+ Short: "Admin operations",
+ Long: `Administrative operations for managing accounts`,
+}
+
+var adminListCmd = &cobra.Command{
+ Use: "list",
+ Short: "List all accounts",
+ RunE: runAdminList,
+}
+
+var adminCreateCmd = &cobra.Command{
+ Use: "create [name]",
+ Short: "Create new account",
+ Args: cobra.ExactArgs(1),
+ RunE: runAdminCreate,
+}
+
+var adminDeleteCmd = &cobra.Command{
+ Use: "delete [api_key]",
+ Short: "Delete account",
+ Args: cobra.ExactArgs(1),
+ RunE: runAdminDelete,
+}
+
+var adminCreditsCmd = &cobra.Command{
+ Use: "credits [api_key] [amount]",
+ Short: "Add credits to account",
+ Args: cobra.ExactArgs(2),
+ RunE: runAdminCredits,
+}
+
+var adminSetCreditsCmd = &cobra.Command{
+ Use: "set-credits [api_key] [amount]",
+ Short: "Set account credits to specific amount",
+ Args: cobra.ExactArgs(2),
+ RunE: runAdminSetCredits,
+}
+
+var adminDisableCmd = &cobra.Command{
+ Use: "disable [api_key]",
+ Short: "Disable/enable account",
+ Args: cobra.ExactArgs(1),
+ RunE: runAdminDisable,
+}
+
+func init() {
+ rootCmd.AddCommand(adminCmd)
+ adminCmd.AddCommand(adminListCmd)
+ adminCmd.AddCommand(adminCreateCmd)
+ adminCmd.AddCommand(adminDeleteCmd)
+ adminCmd.AddCommand(adminCreditsCmd)
+ adminCmd.AddCommand(adminSetCreditsCmd)
+ adminCmd.AddCommand(adminDisableCmd)
+
+ adminCreateCmd.Flags().IntP("credits", "c", 0, "Initial credits")
+ adminCreateCmd.Flags().BoolP("unlimited", "u", false, "Unlimited credits")
+ adminCreateCmd.Flags().BoolP("premium", "p", false, "Premium account (enables skiptrace access)")
+ adminDisableCmd.Flags().BoolP("enable", "e", false, "Enable account instead of disable")
+}
+
+func getAdminClient(cmd *cobra.Command) (*client.Client, error) {
+ apiKey, _ := cmd.Flags().GetString("api-key")
+ cfg := config.New().WithAPIKey(apiKey)
+ return client.New(cfg)
+}
+
+func runAdminList(cmd *cobra.Command, args []string) error {
+ c, err := getAdminClient(cmd)
+ if err != nil {
+ return err
+ }
+
+ response, err := c.ListAccounts()
+ if err != nil {
+ return err
+ }
+
+ if response.Error != "" {
+ return fmt.Errorf("API error: %s", response.Error)
+ }
+
+ output, err := json.MarshalIndent(response.Accounts, "", " ")
+ if err != nil {
+ return fmt.Errorf("failed to format response: %w", err)
+ }
+
+ fmt.Println(string(output))
+ return nil
+}
+
+func runAdminCreate(cmd *cobra.Command, args []string) error {
+ c, err := getAdminClient(cmd)
+ if err != nil {
+ return err
+ }
+
+ credits, _ := cmd.Flags().GetInt("credits")
+ unlimited, _ := cmd.Flags().GetBool("unlimited")
+ premium, _ := cmd.Flags().GetBool("premium")
+
+ req := &models.AccountCreateRequest{
+ Name: args[0],
+ Credits: credits,
+ Unlimited: unlimited,
+ IsPremium: premium,
+ }
+
+ response, err := c.CreateAccount(req)
+ if err != nil {
+ return err
+ }
+
+ if response.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)
+ }
+ return nil
+}
+
+func runAdminDelete(cmd *cobra.Command, args []string) error {
+ c, err := getAdminClient(cmd)
+ if err != nil {
+ return err
+ }
+
+ response, err := c.DeleteAccount(args[0])
+ if err != nil {
+ return err
+ }
+
+ if response.Error != "" {
+ return fmt.Errorf("API error: %s", response.Error)
+ }
+
+ fmt.Println(response.Message)
+ return nil
+}
+
+func runAdminCredits(cmd *cobra.Command, args []string) error {
+ c, err := getAdminClient(cmd)
+ if err != nil {
+ return err
+ }
+
+ credits, err := strconv.Atoi(args[1])
+ if err != nil {
+ return fmt.Errorf("invalid credits amount: %s", args[1])
+ }
+
+ response, err := c.UpdateCredits(args[0], credits)
+ if err != nil {
+ return err
+ }
+
+ if response.Error != "" {
+ return fmt.Errorf("API error: %s", response.Error)
+ }
+
+ fmt.Println(response.Message)
+ return nil
+}
+
+func runAdminSetCredits(cmd *cobra.Command, args []string) error {
+ c, err := getAdminClient(cmd)
+ if err != nil {
+ return err
+ }
+
+ credits, err := strconv.Atoi(args[1])
+ if err != nil {
+ return fmt.Errorf("invalid credits amount: %s", args[1])
+ }
+
+ response, err := c.SetCredits(args[0], credits)
+ if err != nil {
+ return err
+ }
+
+ if response.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)
+ }
+ return nil
+}
+
+func runAdminDisable(cmd *cobra.Command, args []string) error {
+ c, err := getAdminClient(cmd)
+ if err != nil {
+ return err
+ }
+
+ enable, _ := cmd.Flags().GetBool("enable")
+ response, err := c.ToggleAccount(args[0], enable)
+ if err != nil {
+ return err
+ }
+
+ if response.Error != "" {
+ return fmt.Errorf("API error: %s", response.Error)
+ }
+
+ fmt.Println(response.Message)
+ return nil
+}