redisctl 0.10.1

Unified CLI for Redis Cloud and Enterprise
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
//! Cloud task command implementations

#![allow(dead_code)]

use crate::cli::{CloudTaskCommands, OutputFormat};
use crate::connection::ConnectionManager;
use crate::error::{RedisCtlError, Result as CliResult};
use crate::output::print_output;
use anyhow::Context;
use colored::Colorize;
use indicatif::{ProgressBar, ProgressStyle};
use redis_cloud::CloudClient;
use serde_json::Value;
use std::time::Duration;
use tokio::time::{Instant, sleep};

/// Handle cloud task commands
pub async fn handle_task_command(
    conn_mgr: &ConnectionManager,
    profile_name: Option<&str>,
    command: &CloudTaskCommands,
    output_format: OutputFormat,
    query: Option<&str>,
) -> CliResult<()> {
    match command {
        CloudTaskCommands::List => list_tasks(conn_mgr, profile_name, output_format, query).await,
        CloudTaskCommands::Get { id } => {
            get_task(conn_mgr, profile_name, id, output_format, query).await
        }
        CloudTaskCommands::Wait {
            id,
            timeout,
            interval,
        } => {
            wait_for_task(
                conn_mgr,
                profile_name,
                id,
                *timeout,
                *interval,
                output_format,
            )
            .await
        }
        CloudTaskCommands::Poll {
            id,
            interval,
            max_polls,
        } => {
            poll_task(
                conn_mgr,
                profile_name,
                id,
                *interval,
                *max_polls,
                output_format,
            )
            .await
        }
    }
}

/// List all tasks for this account
async fn list_tasks(
    conn_mgr: &ConnectionManager,
    profile_name: Option<&str>,
    output_format: OutputFormat,
    query: Option<&str>,
) -> CliResult<()> {
    let client = conn_mgr.create_cloud_client(profile_name).await?;
    let tasks = client
        .get_raw("/tasks")
        .await
        .with_context(|| "Failed to fetch tasks")
        .map_err(|e| RedisCtlError::ApiError {
            message: e.to_string(),
        })?;

    // Apply JMESPath query if provided
    let data = if let Some(q) = query {
        super::utils::apply_jmespath(&tasks, q)?
    } else {
        tasks
    };

    match output_format {
        OutputFormat::Auto | OutputFormat::Table => {
            print_tasks_table(&data)?;
        }
        OutputFormat::Json => {
            print_output(data, crate::output::OutputFormat::Json, None).map_err(|e| {
                RedisCtlError::OutputError {
                    message: e.to_string(),
                }
            })?;
        }
        OutputFormat::Yaml => {
            print_output(data, crate::output::OutputFormat::Yaml, None).map_err(|e| {
                RedisCtlError::OutputError {
                    message: e.to_string(),
                }
            })?;
        }
    }

    Ok(())
}

/// Print tasks in table format
fn print_tasks_table(tasks: &Value) -> CliResult<()> {
    use super::utils::output_with_pager;
    use tabled::{Table, Tabled, settings::Style};

    #[derive(Tabled)]
    struct TaskRow {
        #[tabled(rename = "Task ID")]
        task_id: String,
        #[tabled(rename = "Status")]
        status: String,
        #[tabled(rename = "Command")]
        command: String,
        #[tabled(rename = "Progress")]
        progress: String,
        #[tabled(rename = "Description")]
        description: String,
    }

    let tasks_array = match tasks.as_array() {
        Some(arr) => arr,
        None => {
            println!("No tasks found");
            return Ok(());
        }
    };

    if tasks_array.is_empty() {
        println!("No tasks found");
        return Ok(());
    }

    let rows: Vec<TaskRow> = tasks_array
        .iter()
        .map(|task| {
            let status = task
                .get("status")
                .or_else(|| task.get("state"))
                .and_then(|s| s.as_str())
                .unwrap_or("unknown");

            TaskRow {
                task_id: task
                    .get("taskId")
                    .or_else(|| task.get("id"))
                    .and_then(|v| v.as_str())
                    .unwrap_or("-")
                    .to_string(),
                status: format_task_state(status),
                command: task
                    .get("commandType")
                    .and_then(|v| v.as_str())
                    .unwrap_or("-")
                    .to_string(),
                progress: task
                    .get("progress")
                    .and_then(|p| p.as_u64())
                    .map(|p| format!("{}%", p))
                    .unwrap_or_else(|| "-".to_string()),
                description: task
                    .get("description")
                    .and_then(|v| v.as_str())
                    .unwrap_or("-")
                    .chars()
                    .take(40)
                    .collect::<String>(),
            }
        })
        .collect();

    let mut table = Table::new(&rows);
    table.with(Style::blank());
    output_with_pager(&table.to_string());

    Ok(())
}

/// Get task status and details
async fn get_task(
    conn_mgr: &ConnectionManager,
    profile_name: Option<&str>,
    task_id: &str,
    output_format: OutputFormat,
    query: Option<&str>,
) -> CliResult<()> {
    let client = conn_mgr.create_cloud_client(profile_name).await?;
    let task = fetch_task(&client, task_id).await?;

    // Apply JMESPath query if provided
    let data = if let Some(q) = query {
        super::utils::apply_jmespath(&task, q)?
    } else {
        task
    };

    match output_format {
        OutputFormat::Auto | OutputFormat::Table => {
            print_task_details(&data)?;
        }
        OutputFormat::Json => {
            print_output(data, crate::output::OutputFormat::Json, None).map_err(|e| {
                RedisCtlError::OutputError {
                    message: e.to_string(),
                }
            })?;
        }
        OutputFormat::Yaml => {
            print_output(data, crate::output::OutputFormat::Yaml, None).map_err(|e| {
                RedisCtlError::OutputError {
                    message: e.to_string(),
                }
            })?;
        }
    }

    Ok(())
}

/// Wait for a task to complete
async fn wait_for_task(
    conn_mgr: &ConnectionManager,
    profile_name: Option<&str>,
    task_id: &str,
    timeout_secs: u64,
    interval_secs: u64,
    output_format: OutputFormat,
) -> CliResult<()> {
    let client = conn_mgr.create_cloud_client(profile_name).await?;
    let start = Instant::now();
    let timeout = Duration::from_secs(timeout_secs);
    let interval = Duration::from_secs(interval_secs);

    // Create progress bar
    let pb = ProgressBar::new_spinner();
    pb.set_style(
        ProgressStyle::default_spinner()
            .template("{spinner:.green} {msg} [{elapsed_precise}]")
            .unwrap(),
    );
    pb.set_message(format!("Waiting for task {}", task_id));

    loop {
        let task = fetch_task(&client, task_id).await?;
        let state = get_task_state(&task);

        pb.set_message(format!("Task {}: {}", task_id, format_task_state(&state)));

        if is_terminal_state(&state) {
            pb.finish_with_message(format!("Task {}: {}", task_id, format_task_state(&state)));

            match output_format {
                OutputFormat::Auto | OutputFormat::Table => {
                    print_task_details(&task)?;
                }
                OutputFormat::Json => {
                    print_output(task, crate::output::OutputFormat::Json, None)?;
                }
                OutputFormat::Yaml => {
                    print_output(task, crate::output::OutputFormat::Yaml, None)?;
                }
            }

            return Ok(());
        }

        if start.elapsed() > timeout {
            pb.finish_with_message(format!("Timeout waiting for task {}", task_id));
            return Err(RedisCtlError::Timeout {
                message: format!(
                    "Task {} did not complete within {} seconds",
                    task_id, timeout_secs
                ),
            });
        }

        sleep(interval).await;
    }
}

/// Poll task status with live updates
async fn poll_task(
    conn_mgr: &ConnectionManager,
    profile_name: Option<&str>,
    task_id: &str,
    interval_secs: u64,
    max_polls: u64,
    output_format: OutputFormat,
) -> CliResult<()> {
    let client = conn_mgr.create_cloud_client(profile_name).await?;
    let interval = Duration::from_secs(interval_secs);
    let mut poll_count = 0u64;

    println!(
        "Polling task {} every {} seconds...",
        task_id, interval_secs
    );
    println!("Press Ctrl+C to stop\n");

    loop {
        let task = fetch_task(&client, task_id).await?;
        let state = get_task_state(&task);
        let timestamp = chrono::Local::now().format("%H:%M:%S");

        // Clear screen for table output in auto mode
        if matches!(output_format, OutputFormat::Auto | OutputFormat::Table) {
            // Move cursor up to overwrite previous output
            if poll_count > 0 {
                print!("\x1B[2K"); // Clear line
                print!("\x1B[1A"); // Move up one line
                print!("\x1B[2K"); // Clear line
                print!("\r"); // Return to start of line
            }

            println!(
                "[{}] Task {}: {}",
                timestamp,
                task_id,
                format_task_state(&state)
            );

            if let Some(progress) = task.get("progress").and_then(|p| p.as_u64()) {
                print_progress_bar(progress);
            }
        } else {
            // For JSON/YAML, print the full output each time
            match output_format {
                OutputFormat::Json => {
                    print_output(task.clone(), crate::output::OutputFormat::Json, None)?;
                }
                OutputFormat::Yaml => {
                    print_output(task.clone(), crate::output::OutputFormat::Yaml, None)?;
                }
                _ => {} // Auto/Table already handled above
            }
        }

        if is_terminal_state(&state) {
            println!("\nTask completed with state: {}", format_task_state(&state));
            break;
        }

        poll_count += 1;
        if max_polls > 0 && poll_count >= max_polls {
            println!("\nReached maximum poll count ({})", max_polls);
            break;
        }

        sleep(interval).await;
    }

    Ok(())
}

/// Fetch task details from API
async fn fetch_task(client: &CloudClient, task_id: &str) -> CliResult<Value> {
    client
        .get_raw(&format!("/tasks/{}", task_id))
        .await
        .with_context(|| format!("Failed to fetch task {}", task_id))
        .map_err(|e| RedisCtlError::ApiError {
            message: e.to_string(),
        })
}

/// Extract task state from response
fn get_task_state(task: &Value) -> String {
    task.get("state")
        .or_else(|| task.get("status"))
        .and_then(|s| s.as_str())
        .unwrap_or("unknown")
        .to_string()
}

/// Check if task is in terminal state
fn is_terminal_state(state: &str) -> bool {
    matches!(
        state.to_lowercase().as_str(),
        "completed" | "failed" | "error" | "success" | "cancelled" | "aborted"
    )
}

/// Format task state with color
fn format_task_state(state: &str) -> String {
    match state.to_lowercase().as_str() {
        "processing" | "running" | "in_progress" => state.yellow().to_string(),
        "completed" | "success" => state.green().to_string(),
        "failed" | "error" => state.red().to_string(),
        "cancelled" | "aborted" => state.dimmed().to_string(),
        _ => state.to_string(),
    }
}

/// Print task details in table format
fn print_task_details(task: &Value) -> CliResult<()> {
    use super::utils::DetailRow;
    use tabled::{Table, settings::Style};

    let mut rows = Vec::new();

    // Task ID
    if let Some(id) = task.get("taskId").or_else(|| task.get("id")) {
        rows.push(DetailRow {
            field: "Task ID".to_string(),
            value: id.to_string().trim_matches('"').to_string(),
        });
    }

    // State/Status
    let state = get_task_state(task);
    rows.push(DetailRow {
        field: "State".to_string(),
        value: format_task_state(&state),
    });

    // Progress
    if let Some(progress) = task.get("progress").and_then(|p| p.as_u64()) {
        rows.push(DetailRow {
            field: "Progress".to_string(),
            value: format!("{}%", progress),
        });
    }

    // Description
    if let Some(desc) = task.get("description").and_then(|d| d.as_str()) {
        rows.push(DetailRow {
            field: "Description".to_string(),
            value: desc.to_string(),
        });
    }

    // Resource info
    if let Some(resource) = task.get("resourceId").and_then(|r| r.as_str()) {
        rows.push(DetailRow {
            field: "Resource ID".to_string(),
            value: resource.to_string(),
        });
    }

    if let Some(resource_type) = task.get("resourceType").and_then(|r| r.as_str()) {
        rows.push(DetailRow {
            field: "Resource Type".to_string(),
            value: resource_type.to_string(),
        });
    }

    // Timing
    if let Some(created) = task.get("createdTimestamp").and_then(|t| t.as_str()) {
        rows.push(DetailRow {
            field: "Created".to_string(),
            value: super::utils::format_date(created.to_string()),
        });
    }

    if let Some(updated) = task.get("lastUpdatedTimestamp").and_then(|t| t.as_str()) {
        rows.push(DetailRow {
            field: "Last Updated".to_string(),
            value: super::utils::format_date(updated.to_string()),
        });
    }

    // Error details if failed
    if matches!(state.to_lowercase().as_str(), "failed" | "error") {
        if let Some(error) = task.get("error").and_then(|e| e.as_str()) {
            rows.push(DetailRow {
                field: "Error".to_string(),
                value: error.red().to_string(),
            });
        }
        if let Some(error_msg) = task.get("errorMessage").and_then(|e| e.as_str()) {
            rows.push(DetailRow {
                field: "Error Message".to_string(),
                value: error_msg.red().to_string(),
            });
        }
    }

    if rows.is_empty() {
        println!("No task information available");
        return Ok(());
    }

    let mut table = Table::new(&rows);
    table.with(Style::blank());

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

/// Print a simple progress bar
fn print_progress_bar(progress: u64) {
    let width = 30;
    let filled = (progress as usize * width) / 100;
    let empty = width - filled;

    print!("Progress: [");
    print!("{}", "=".repeat(filled).green());
    print!("{}", "-".repeat(empty).dimmed());
    println!("] {}%", progress);
}