1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
|
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 FormatAccountUpdated(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
}
var sb strings.Builder
sb.WriteString(fmt.Sprintf("\n%s\n\n", Bold(Green("✓ Account updated successfully!"))))
if account != nil && account.Name != "" {
sb.WriteString(fmt.Sprintf("%s %s\n", Cyan("Name:"), Bold(account.Name)))
} else {
sb.WriteString(fmt.Sprintf("%s %s\n", Cyan("Message:"), message))
}
if account != nil {
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))))
}
premiumStr := Gray("No")
if account.IsPremium {
premiumStr = Magenta("Yes")
}
sb.WriteString(fmt.Sprintf("%s %s\n", Cyan("Premium:"), premiumStr))
statusStr := Green("Active")
if account.Disabled {
statusStr = Red("Disabled")
}
sb.WriteString(fmt.Sprintf("%s %s\n", Cyan("Status:"), statusStr))
}
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
}
|