diff options
Diffstat (limited to 'internal/formatter/admin.go')
| -rw-r--r-- | internal/formatter/admin.go | 187 |
1 files changed, 187 insertions, 0 deletions
diff --git a/internal/formatter/admin.go b/internal/formatter/admin.go new file mode 100644 index 0000000..914eaaa --- /dev/null +++ b/internal/formatter/admin.go @@ -0,0 +1,187 @@ +package formatter + +import ( + "fmt" + "strings" + "time" + + "git.db.org.ai/dborg/internal/models" +) + +func FormatAccountList(accounts []models.Account, asJSON bool) (string, error) { + if asJSON { + err := PrintColorizedJSON(accounts) + if err != nil { + return "", fmt.Errorf("failed to marshal JSON: %w", err) + } + return "", nil + } + + if len(accounts) == 0 { + return fmt.Sprintf("%s\n", Gray("No accounts found")), nil + } + + var sb strings.Builder + sb.WriteString(fmt.Sprintf("\n%s\n", Bold(Cyan("Account List")))) + sb.WriteString(fmt.Sprintf("%s\n\n", Gray(strings.Repeat("─", 80)))) + + table := NewTable([]string{"Name", "API Key", "Credits", "Status", "Premium", "Created"}) + + for _, acc := range accounts { + creditsStr := fmt.Sprintf("%d", acc.Credits) + if acc.Unlimited { + creditsStr = Green("Unlimited") + } else if acc.Credits > 1000 { + creditsStr = Green(creditsStr) + } else if acc.Credits > 100 { + creditsStr = Yellow(creditsStr) + } else { + creditsStr = Red(creditsStr) + } + + statusStr := Green("Active") + if acc.Disabled { + statusStr = Red("Disabled") + } + + premiumStr := Gray("No") + if acc.IsPremium { + premiumStr = Magenta("Yes") + } + + createdStr := Gray("-") + if acc.CreatedAt != nil { + if createdAt, ok := acc.CreatedAt.(string); ok && createdAt != "" { + if t, err := time.Parse(time.RFC3339, createdAt); err == nil { + createdStr = t.Format("2006-01-02") + } else { + createdStr = createdAt + } + } + } + + table.AddRow( + Bold(acc.Name), + Dim(acc.APIKey), + creditsStr, + statusStr, + premiumStr, + createdStr, + ) + } + + sb.WriteString(table.Render()) + sb.WriteString(fmt.Sprintf("\n%s %d accounts\n", Blue("Total:"), len(accounts))) + + return sb.String(), nil +} + +func FormatAccountCreated(account *models.Account, message string, asJSON bool) (string, error) { + if asJSON { + response := map[string]interface{}{ + "account": account, + "message": message, + } + err := PrintColorizedJSON(response) + if err != nil { + return "", fmt.Errorf("failed to marshal JSON: %w", err) + } + return "", nil + } + + if account == nil { + return fmt.Sprintf("%s\n", message), nil + } + + var sb strings.Builder + sb.WriteString(fmt.Sprintf("\n%s\n\n", Bold(Green("✓ Account created successfully!")))) + + sb.WriteString(fmt.Sprintf("%s %s\n", Cyan("Name:"), Bold(account.Name))) + sb.WriteString(fmt.Sprintf("%s %s\n", Cyan("API Key:"), Bold(Yellow(account.APIKey)))) + + if account.Unlimited { + sb.WriteString(fmt.Sprintf("%s %s\n", Cyan("Credits:"), Green("Unlimited"))) + } else { + sb.WriteString(fmt.Sprintf("%s %s\n", Cyan("Credits:"), FormatCredits(int64(account.Credits)))) + } + + if account.IsPremium { + sb.WriteString(fmt.Sprintf("%s %s\n", Cyan("Premium:"), Magenta("Yes"))) + } else { + sb.WriteString(fmt.Sprintf("%s %s\n", Cyan("Premium:"), Gray("No"))) + } + + if account.Disabled { + sb.WriteString(fmt.Sprintf("%s %s\n", Cyan("Status:"), Red("Disabled"))) + } else { + sb.WriteString(fmt.Sprintf("%s %s\n", Cyan("Status:"), Green("Active"))) + } + + sb.WriteString(fmt.Sprintf("\n%s\n", Dim("Save the API key securely - it cannot be retrieved later!"))) + + return sb.String(), nil +} + +func FormatAccountDeleted(message string, asJSON bool) (string, error) { + if asJSON { + response := map[string]string{"message": message} + err := PrintColorizedJSON(response) + if err != nil { + return "", fmt.Errorf("failed to marshal JSON: %w", err) + } + return "", nil + } + + return fmt.Sprintf("%s %s\n", Green("✓"), message), nil +} + +func FormatCreditsUpdated(message string, account *models.Account, asJSON bool) (string, error) { + if asJSON { + response := map[string]interface{}{ + "message": message, + "account": account, + } + err := PrintColorizedJSON(response) + if err != nil { + return "", fmt.Errorf("failed to marshal JSON: %w", err) + } + return "", nil + } + + var sb strings.Builder + + if account != nil { + sb.WriteString(fmt.Sprintf("\n%s\n\n", Bold(Green("✓ Credits updated successfully!")))) + sb.WriteString(fmt.Sprintf("%s %s\n", Cyan("Account:"), Bold(account.Name))) + sb.WriteString(fmt.Sprintf("%s %s\n", Cyan("API Key:"), Dim(account.APIKey))) + + if account.Unlimited { + sb.WriteString(fmt.Sprintf("%s %s\n", Cyan("Credits:"), Green("Unlimited"))) + } else { + sb.WriteString(fmt.Sprintf("%s %s\n", Cyan("Credits:"), FormatCredits(int64(account.Credits)))) + } + } else { + sb.WriteString(fmt.Sprintf("%s %s\n", Green("✓"), message)) + } + + return sb.String(), nil +} + +func FormatAccountToggled(message string, asJSON bool) (string, error) { + if asJSON { + response := map[string]string{"message": message} + err := PrintColorizedJSON(response) + if err != nil { + return "", fmt.Errorf("failed to marshal JSON: %w", err) + } + return "", nil + } + + if strings.Contains(strings.ToLower(message), "enabled") { + return fmt.Sprintf("%s %s\n", Green("✓"), message), nil + } else if strings.Contains(strings.ToLower(message), "disabled") { + return fmt.Sprintf("%s %s\n", Yellow("⚠"), message), nil + } + + return fmt.Sprintf("%s %s\n", Blue("ℹ"), message), nil +} |
