oauth-db-cli 0.1.0

Command-line tool for managing OAuth-DB platform
Documentation
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
use crate::cli::{AppCommands, Cli};
use crate::client::ApiClient;
use crate::config::Config;
use crate::error::{CliError, Result};
use crate::output::{create_table_with_headers, format_output, print_success, print_warning, OutputFormat};
use chrono::{DateTime, Utc};
use dialoguer::Confirm;
use serde::{Deserialize, Serialize};

pub async fn execute(command: &AppCommands, cli: &Cli) -> Result<()> {
    let output_format = cli.output.as_ref().map(|s| OutputFormat::from_str(s)).unwrap_or(OutputFormat::Table);

    match command {
        AppCommands::Create { name, default_db, allowed_dbs, max_users, storage_quota, max_connections, qps_limit } => {
            create_app(name.clone(), default_db.clone(), allowed_dbs.clone(), *max_users, *storage_quota, *max_connections, *qps_limit, &output_format).await
        }
        AppCommands::List { status, page, page_size } => {
            list_apps(status.clone(), *page, *page_size, &output_format).await
        }
        AppCommands::Show { app_id } => {
            show_app(app_id.clone(), &output_format).await
        }
        AppCommands::Update { app_id, name, default_db, allowed_dbs, max_users, storage_quota, max_connections, qps_limit } => {
            update_app(app_id.clone(), name.clone(), default_db.clone(), allowed_dbs.clone(), *max_users, *storage_quota, *max_connections, *qps_limit, &output_format).await
        }
        AppCommands::Enable { app_id } => {
            enable_app(app_id.clone(), &output_format).await
        }
        AppCommands::Disable { app_id } => {
            disable_app(app_id.clone(), &output_format).await
        }
        AppCommands::Delete { app_id, force } => {
            delete_app(app_id.clone(), *force, &output_format).await
        }
        AppCommands::ResetToken { app_id } => {
            reset_token(app_id.clone(), &output_format).await
        }
        AppCommands::Stats { app_id } => {
            show_stats(app_id.clone(), &output_format).await
        }
    }
}

#[derive(Debug, Serialize, Deserialize)]
pub struct CreateAppRequest {
    pub app_name: String,
    pub default_database: String,
    pub allowed_databases: Vec<String>,
    pub max_users: u32,
    pub default_storage_quota_mb: u32,
    pub default_max_connections: u32,
    pub default_qps: u32,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct CreateAppResponse {
    pub app_id: String,
    pub app_name: String,
    pub app_token: String,
    pub default_database: String,
    pub allowed_databases: Vec<String>,
    pub max_users: u32,
    pub status: String,
    pub created_at: String,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct AppListResponse {
    pub items: Vec<AppListItem>,
    pub total: u32,
    pub page: u32,
    pub page_size: u32,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct AppListItem {
    pub app_id: String,
    pub app_name: String,
    pub status: String,
    pub user_count: u32,
    pub created_at: String,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct AppDetail {
    pub app_id: String,
    pub app_name: String,
    pub default_database: String,
    pub allowed_databases: Vec<String>,
    pub status: String,
    pub max_users: u32,
    pub default_storage_quota_mb: u32,
    pub default_max_connections: u32,
    pub default_qps: u32,
    pub user_count: u32,
    pub total_storage_mb: f64,
    pub created_at: String,
    pub updated_at: String,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct UpdateAppRequest {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub app_name: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub default_database: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub allowed_databases: Option<Vec<String>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub max_users: Option<u32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub default_storage_quota_mb: Option<u32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub default_max_connections: Option<u32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub default_qps: Option<u32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub status: Option<String>,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct UpdateAppResponse {
    pub app_id: String,
    pub updated_fields: Vec<String>,
    pub updated_at: String,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct DeleteAppResponse {
    pub app_id: String,
    pub deleted: bool,
    pub affected_users: u32,
    pub workspace_cleanup: String,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct ResetTokenResponse {
    pub app_id: String,
    pub app_token: String,
    pub rotated_at: String,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct AppStats {
    pub app_id: String,
    pub user_count: u32,
    pub active_user_count: u32,
    pub total_storage_mb: f64,
    pub total_connections: u32,
    pub recent_users: Vec<RecentUser>,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct RecentUser {
    pub user_uid: String,
    pub label: Option<String>,
    pub last_accessed_at: String,
}

pub async fn create_app(
    name: String,
    default_db: String,
    allowed_dbs: String,
    max_users: u32,
    storage_quota: u32,
    max_connections: u32,
    qps_limit: u32,
    output_format: &OutputFormat,
) -> Result<()> {
    let config = Config::load()?;
    let account = config.get_default_account().ok_or(CliError::NotLoggedIn)?;

    let client = ApiClient::new(account.server.clone(), Some(account.token.clone()))?;

    let allowed_databases: Vec<String> = allowed_dbs
        .split(',')
        .map(|s| s.trim().to_string())
        .filter(|s| !s.is_empty())
        .collect();

    let request = CreateAppRequest {
        app_name: name,
        default_database: default_db,
        allowed_databases,
        max_users,
        default_storage_quota_mb: storage_quota,
        default_max_connections: max_connections,
        default_qps: qps_limit,
    };

    let request_json = serde_json::to_value(&request)
        .map_err(|e| CliError::SerializationError(format!("Failed to serialize request: {}", e)))?;
    let response: CreateAppResponse = client.post("/api/v1/apps", &request_json).await?;

    match output_format {
        OutputFormat::Json | OutputFormat::Yaml => {
            println!("{}", format_output(&response, *output_format)?);
        }
        OutputFormat::Table => {
            print_success("App created successfully");
            println!();
            println!("  App ID: {}", response.app_id);
            println!("  App Name: {}", response.app_name);
            println!("  App Token: {}", response.app_token);
            println!();
            print_warning("⚠️  Save the app token - it won't be shown again!");
        }
    }

    Ok(())
}

pub async fn list_apps(
    status: Option<String>,
    page: u32,
    page_size: u32,
    output_format: &OutputFormat,
) -> Result<()> {
    let config = Config::load()?;
    let account = config.get_default_account().ok_or(CliError::NotLoggedIn)?;

    let client = ApiClient::new(account.server.clone(), Some(account.token.clone()))?;

    let mut query = vec![
        ("page", page.to_string()),
        ("page_size", page_size.to_string()),
    ];

    if let Some(s) = status {
        query.push(("status", s));
    }

    let query_refs: Vec<(&str, String)> = query.iter().map(|(k, v)| (*k, v.clone())).collect();
    let response: AppListResponse = client.get("/api/v1/apps", &query_refs).await?;

    match output_format {
        OutputFormat::Json | OutputFormat::Yaml => {
            println!("{}", format_output(&response, *output_format)?);
        }
        OutputFormat::Table => {
            if response.items.is_empty() {
                println!("No applications found.");
                return Ok(());
            }

            let mut table = create_table_with_headers(vec!["APP ID", "NAME", "STATUS", "USERS", "CREATED AT"]);

            for item in &response.items {
                let created_at = parse_and_format_date(&item.created_at);
                table.add_row(vec![
                    &item.app_id,
                    &item.app_name,
                    &item.status,
                    &item.user_count.to_string(),
                    &created_at,
                ]);
            }

            println!("{}", table);
            println!(
                "\nShowing {} of {} apps (page {}/{})",
                response.items.len(),
                response.total,
                response.page,
                (response.total + response.page_size - 1) / response.page_size
            );
        }
    }

    Ok(())
}

pub async fn show_app(app_id: String, output_format: &OutputFormat) -> Result<()> {
    let config = Config::load()?;
    let account = config.get_default_account().ok_or(CliError::NotLoggedIn)?;

    let client = ApiClient::new(account.server.clone(), Some(account.token.clone()))?;

    let url = format!("/api/v1/apps/{}", app_id);
    let response: AppDetail = client.get(&url, &[]).await?;

    match output_format {
        OutputFormat::Json | OutputFormat::Yaml => {
            println!("{}", format_output(&response, *output_format)?);
        }
        OutputFormat::Table => {
            println!("Application Details");
            println!("==================");
            println!();
            println!("  App ID: {}", response.app_id);
            println!("  Name: {}", response.app_name);
            println!("  Status: {}", response.status);
            println!();
            println!("Database Configuration:");
            println!("  Default Database: {}", response.default_database);
            println!("  Allowed Databases: {}", response.allowed_databases.join(", "));
            println!();
            println!("Quotas:");
            println!("  Max Users: {}", response.max_users);
            println!("  Storage Quota (per user): {} MB", response.default_storage_quota_mb);
            println!("  Max Connections (per user): {}", response.default_max_connections);
            println!("  QPS Limit (per user): {}", response.default_qps);
            println!();
            println!("Usage:");
            println!("  Current Users: {}", response.user_count);
            println!("  Total Storage: {:.2} MB", response.total_storage_mb);
            println!();
            println!("  Created: {}", parse_and_format_date(&response.created_at));
            println!("  Updated: {}", parse_and_format_date(&response.updated_at));
        }
    }

    Ok(())
}

pub async fn update_app(
    app_id: String,
    name: Option<String>,
    default_db: Option<String>,
    allowed_dbs: Option<String>,
    max_users: Option<u32>,
    storage_quota: Option<u32>,
    max_connections: Option<u32>,
    qps_limit: Option<u32>,
    output_format: &OutputFormat,
) -> Result<()> {
    let config = Config::load()?;
    let account = config.get_default_account().ok_or(CliError::NotLoggedIn)?;

    let client = ApiClient::new(account.server.clone(), Some(account.token.clone()))?;

    let allowed_databases = allowed_dbs.map(|dbs| {
        dbs.split(',')
            .map(|s| s.trim().to_string())
            .filter(|s| !s.is_empty())
            .collect()
    });

    let request = UpdateAppRequest {
        app_name: name,
        default_database: default_db,
        allowed_databases,
        max_users,
        default_storage_quota_mb: storage_quota,
        default_max_connections: max_connections,
        default_qps: qps_limit,
        status: None,
    };

    let url = format!("/api/v1/apps/{}", app_id);
    let request_json = serde_json::to_value(&request)
        .map_err(|e| CliError::SerializationError(format!("Failed to serialize request: {}", e)))?;
    let response: UpdateAppResponse = client.patch(&url, &request_json).await?;

    match output_format {
        OutputFormat::Json | OutputFormat::Yaml => {
            println!("{}", format_output(&response, *output_format)?);
        }
        OutputFormat::Table => {
            print_success("App updated successfully");
            println!();
            println!("  App ID: {}", response.app_id);
            println!("  Updated Fields: {}", response.updated_fields.join(", "));
            println!("  Updated At: {}", parse_and_format_date(&response.updated_at));
        }
    }

    Ok(())
}

pub async fn enable_app(app_id: String, output_format: &OutputFormat) -> Result<()> {
    change_app_status(app_id, "active", output_format).await
}

pub async fn disable_app(app_id: String, output_format: &OutputFormat) -> Result<()> {
    change_app_status(app_id, "disabled", output_format).await
}

async fn change_app_status(
    app_id: String,
    status: &str,
    output_format: &OutputFormat,
) -> Result<()> {
    let config = Config::load()?;
    let account = config.get_default_account().ok_or(CliError::NotLoggedIn)?;

    let client = ApiClient::new(account.server.clone(), Some(account.token.clone()))?;

    let request = UpdateAppRequest {
        app_name: None,
        default_database: None,
        allowed_databases: None,
        max_users: None,
        default_storage_quota_mb: None,
        default_max_connections: None,
        default_qps: None,
        status: Some(status.to_string()),
    };

    let url = format!("/api/v1/apps/{}", app_id);
    let request_json = serde_json::to_value(&request)
        .map_err(|e| CliError::SerializationError(format!("Failed to serialize request: {}", e)))?;
    let response: UpdateAppResponse = client.patch(&url, &request_json).await?;

    match output_format {
        OutputFormat::Json | OutputFormat::Yaml => {
            println!("{}", format_output(&response, *output_format)?);
        }
        OutputFormat::Table => {
            let action = if status == "active" { "enabled" } else { "disabled" };
            print_success(&format!("App {} successfully", action));
        }
    }

    Ok(())
}

pub async fn delete_app(app_id: String, force: bool, output_format: &OutputFormat) -> Result<()> {
    let config = Config::load()?;
    let account = config.get_default_account().ok_or(CliError::NotLoggedIn)?;

    let client = ApiClient::new(account.server.clone(), Some(account.token.clone()))?;

    // Get app details first for confirmation
    let url = format!("/api/v1/apps/{}", app_id);
    let app: AppDetail = client.get(&url, &[]).await?;

    if !force {
        println!();
        print_warning("⚠️  WARNING: This will permanently delete the app and all its users.");
        println!("   App ID: {}", app.app_id);
        println!("   App Name: {}", app.app_name);
        println!("   Users: {}", app.user_count);
        println!();

        let confirmed = Confirm::new()
            .with_prompt("Are you sure?")
            .default(false)
            .interact()
            .map_err(|e| CliError::InvalidInput(format!("Failed to read input: {}", e)))?;

        if !confirmed {
            println!("Cancelled.");
            return Ok(());
        }
    }

    let response: DeleteAppResponse = client.delete(&url).await?;

    match output_format {
        OutputFormat::Json | OutputFormat::Yaml => {
            println!("{}", format_output(&response, *output_format)?);
        }
        OutputFormat::Table => {
            print_success("App deleted successfully");
            println!();
            println!("  Affected Users: {}", response.affected_users);
            println!("  Workspace Cleanup: {}", response.workspace_cleanup);
        }
    }

    Ok(())
}

pub async fn reset_token(app_id: String, output_format: &OutputFormat) -> Result<()> {
    let config = Config::load()?;
    let account = config.get_default_account().ok_or(CliError::NotLoggedIn)?;

    let client = ApiClient::new(account.server.clone(), Some(account.token.clone()))?;

    // Confirm action
    let confirmed = Confirm::new()
        .with_prompt("This will invalidate the old token. Continue?")
        .default(false)
        .interact()
        .map_err(|e| CliError::InvalidInput(format!("Failed to read input: {}", e)))?;

    if !confirmed {
        println!("Cancelled.");
        return Ok(());
    }

    let url = format!("/api/v1/apps/{}/rotate-token", app_id);
    let response: ResetTokenResponse = client.post(&url, &serde_json::json!({})).await?;

    match output_format {
        OutputFormat::Json | OutputFormat::Yaml => {
            println!("{}", format_output(&response, *output_format)?);
        }
        OutputFormat::Table => {
            print_success("App token reset successfully");
            println!();
            println!("  App ID: {}", response.app_id);
            println!("  New Token: {}", response.app_token);
            println!();
            print_warning("⚠️  Save the new token - it won't be shown again!");
        }
    }

    Ok(())
}

pub async fn show_stats(app_id: String, output_format: &OutputFormat) -> Result<()> {
    let config = Config::load()?;
    let account = config.get_default_account().ok_or(CliError::NotLoggedIn)?;

    let client = ApiClient::new(account.server.clone(), Some(account.token.clone()))?;

    let url = format!("/api/v1/apps/{}/stats", app_id);
    let response: AppStats = client.get(&url, &[]).await?;

    match output_format {
        OutputFormat::Json | OutputFormat::Yaml => {
            println!("{}", format_output(&response, *output_format)?);
        }
        OutputFormat::Table => {
            println!("Application Statistics");
            println!("=====================");
            println!();
            println!("  App ID: {}", response.app_id);
            println!("  Total Users: {}", response.user_count);
            println!("  Active Users: {}", response.active_user_count);
            println!("  Total Storage: {:.2} MB", response.total_storage_mb);
            println!("  Active Connections: {}", response.total_connections);
            println!();

            if !response.recent_users.is_empty() {
                println!("Recent Users:");
                let mut table = create_table_with_headers(vec!["USER UID", "LABEL", "LAST ACCESSED"]);

                for user in &response.recent_users {
                    let label = user.label.as_deref().unwrap_or("-");
                    let last_accessed = parse_and_format_date(&user.last_accessed_at);
                    table.add_row(vec![&user.user_uid, label, &last_accessed]);
                }

                println!("{}", table);
            }
        }
    }

    Ok(())
}

fn parse_and_format_date(date_str: &str) -> String {
    DateTime::parse_from_rfc3339(date_str)
        .map(|dt| dt.with_timezone(&Utc).format("%Y-%m-%d %H:%M:%S UTC").to_string())
        .unwrap_or_else(|_| date_str.to_string())
}