oximedia-cli 0.1.4

Command-line interface for OxiMedia
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
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
//! Cloud storage CLI commands.
//!
//! Provides commands for uploading, downloading, cloud transcoding,
//! job status, and cost estimation across S3, Azure, and GCS providers.

use anyhow::{Context, Result};
use clap::Subcommand;
use colored::Colorize;
use std::path::PathBuf;

// ---------------------------------------------------------------------------
// Command definitions
// ---------------------------------------------------------------------------

/// Cloud command subcommands.
#[derive(Subcommand, Debug)]
pub enum CloudCommand {
    /// Upload a file to cloud storage
    Upload {
        /// Local input file
        #[arg(short, long)]
        input: PathBuf,

        /// Cloud provider: s3, azure, gcs
        #[arg(long)]
        provider: String,

        /// Bucket or container name
        #[arg(long)]
        bucket: String,

        /// Remote object key (defaults to filename)
        #[arg(long)]
        key: Option<String>,

        /// Cloud region
        #[arg(long)]
        region: Option<String>,

        /// Use multipart upload for large files
        #[arg(long)]
        multipart: bool,

        /// Bandwidth limit in KB/s
        #[arg(long)]
        bandwidth_limit: Option<u32>,
    },

    /// Download a file from cloud storage
    Download {
        /// Cloud provider: s3, azure, gcs
        #[arg(long)]
        provider: String,

        /// Bucket or container name
        #[arg(long)]
        bucket: String,

        /// Remote object key
        #[arg(long)]
        key: String,

        /// Local output file
        #[arg(short, long)]
        output: PathBuf,

        /// Cloud region
        #[arg(long)]
        region: Option<String>,
    },

    /// Submit a cloud transcoding job
    Transcode {
        /// Cloud provider: s3, azure, gcs
        #[arg(long)]
        provider: String,

        /// Bucket or container name
        #[arg(long)]
        bucket: String,

        /// Input object key
        #[arg(long)]
        input_key: String,

        /// Output object key
        #[arg(long)]
        output_key: String,

        /// Transcoding preset (e.g., av1-1080p, vp9-4k)
        #[arg(long)]
        preset: Option<String>,

        /// Cloud region
        #[arg(long)]
        region: Option<String>,
    },

    /// Check a cloud job status
    Status {
        /// Cloud provider: s3, azure, gcs
        #[arg(long)]
        provider: String,

        /// Job identifier
        #[arg(long)]
        job_id: String,

        /// Cloud region
        #[arg(long)]
        region: Option<String>,
    },

    /// Estimate cloud costs
    Cost {
        /// Cloud provider: s3, azure, gcs
        #[arg(long)]
        provider: String,

        /// Storage amount in GB
        #[arg(long)]
        storage_gb: f64,

        /// Egress (data transfer out) in GB
        #[arg(long)]
        egress_gb: Option<f64>,

        /// Transcoding minutes
        #[arg(long)]
        transcode_minutes: Option<f64>,

        /// Cloud region
        #[arg(long)]
        region: Option<String>,
    },
}

// ---------------------------------------------------------------------------
// Cost data
// ---------------------------------------------------------------------------

/// Per-provider pricing constants (approximate USD/month, standard tier).
struct PricingTier {
    storage_per_gb: f64,
    egress_per_gb: f64,
    transcode_per_min: f64,
    name: &'static str,
}

fn pricing_for(provider: &str, region: &str) -> Result<PricingTier> {
    // Simplified pricing model; real implementation would query provider APIs
    let _region = region; // region could vary pricing; here we use defaults
    match provider.to_lowercase().as_str() {
        "s3" | "aws" => Ok(PricingTier {
            storage_per_gb: 0.023,
            egress_per_gb: 0.09,
            transcode_per_min: 0.024,
            name: "AWS S3",
        }),
        "azure" => Ok(PricingTier {
            storage_per_gb: 0.018,
            egress_per_gb: 0.087,
            transcode_per_min: 0.022,
            name: "Azure Blob",
        }),
        "gcs" | "google" => Ok(PricingTier {
            storage_per_gb: 0.020,
            egress_per_gb: 0.12,
            transcode_per_min: 0.025,
            name: "Google Cloud Storage",
        }),
        other => Err(anyhow::anyhow!(
            "Unknown cloud provider '{}'. Supported: s3, azure, gcs",
            other
        )),
    }
}

fn validate_provider(provider: &str) -> Result<()> {
    match provider.to_lowercase().as_str() {
        "s3" | "aws" | "azure" | "gcs" | "google" => Ok(()),
        other => Err(anyhow::anyhow!(
            "Unknown cloud provider '{}'. Supported: s3, azure, gcs",
            other
        )),
    }
}

fn format_provider(provider: &str) -> &str {
    match provider.to_lowercase().as_str() {
        "s3" | "aws" => "AWS S3",
        "azure" => "Azure Blob Storage",
        "gcs" | "google" => "Google Cloud Storage",
        _ => provider,
    }
}

// ---------------------------------------------------------------------------
// Command handler
// ---------------------------------------------------------------------------

/// Handle cloud command dispatch.
pub async fn handle_cloud_command(command: CloudCommand, json_output: bool) -> Result<()> {
    match command {
        CloudCommand::Upload {
            input,
            provider,
            bucket,
            key,
            region,
            multipart,
            bandwidth_limit,
        } => {
            run_upload(
                &input,
                &provider,
                &bucket,
                &key,
                &region,
                multipart,
                bandwidth_limit,
                json_output,
            )
            .await
        }
        CloudCommand::Download {
            provider,
            bucket,
            key,
            output,
            region,
        } => run_download(&provider, &bucket, &key, &output, &region, json_output).await,
        CloudCommand::Transcode {
            provider,
            bucket,
            input_key,
            output_key,
            preset,
            region,
        } => {
            run_transcode(
                &provider,
                &bucket,
                &input_key,
                &output_key,
                &preset,
                &region,
                json_output,
            )
            .await
        }
        CloudCommand::Status {
            provider,
            job_id,
            region,
        } => run_status(&provider, &job_id, &region, json_output).await,
        CloudCommand::Cost {
            provider,
            storage_gb,
            egress_gb,
            transcode_minutes,
            region,
        } => {
            run_cost(
                &provider,
                storage_gb,
                egress_gb,
                transcode_minutes,
                &region,
                json_output,
            )
            .await
        }
    }
}

// ---------------------------------------------------------------------------
// Upload
// ---------------------------------------------------------------------------

async fn run_upload(
    input: &PathBuf,
    provider: &str,
    bucket: &str,
    key: &Option<String>,
    region: &Option<String>,
    multipart: bool,
    bandwidth_limit: Option<u32>,
    json_output: bool,
) -> Result<()> {
    validate_provider(provider)?;

    if !input.exists() {
        return Err(anyhow::anyhow!("Input file not found: {}", input.display()));
    }

    let meta = std::fs::metadata(input).context("Failed to read file metadata")?;
    let remote_key = key.clone().unwrap_or_else(|| {
        input
            .file_name()
            .unwrap_or_default()
            .to_string_lossy()
            .to_string()
    });
    let region_str = region.as_deref().unwrap_or("us-east-1");

    if json_output {
        let result = serde_json::json!({
            "command": "upload",
            "provider": format_provider(provider),
            "bucket": bucket,
            "key": remote_key,
            "region": region_str,
            "size_bytes": meta.len(),
            "multipart": multipart,
            "bandwidth_limit_kbps": bandwidth_limit,
            "status": "ready",
            "message": "Upload configured; cloud credentials required for execution",
        });
        let s = serde_json::to_string_pretty(&result).context("Failed to serialize")?;
        println!("{s}");
    } else {
        println!("{}", "Cloud Upload".green().bold());
        println!("{}", "=".repeat(60));
        println!("{:22} {}", "Provider:", format_provider(provider));
        println!("{:22} {}", "Bucket:", bucket);
        println!("{:22} {}", "Remote key:", remote_key);
        println!("{:22} {}", "Region:", region_str);
        println!("{:22} {}", "Local file:", input.display());
        println!(
            "{:22} {:.2} MB",
            "File size:",
            meta.len() as f64 / (1024.0 * 1024.0)
        );
        println!(
            "{:22} {}",
            "Multipart:",
            if multipart { "enabled" } else { "disabled" }
        );
        if let Some(limit) = bandwidth_limit {
            println!("{:22} {} KB/s", "Bandwidth limit:", limit);
        }
        println!();
        println!(
            "{}",
            "Note: Cloud credentials must be configured for actual upload.".yellow()
        );
        println!(
            "{}",
            "Set environment variables (AWS_ACCESS_KEY_ID, etc.) or use a credentials file."
                .dimmed()
        );
    }

    Ok(())
}

// ---------------------------------------------------------------------------
// Download
// ---------------------------------------------------------------------------

async fn run_download(
    provider: &str,
    bucket: &str,
    key: &str,
    output: &PathBuf,
    region: &Option<String>,
    json_output: bool,
) -> Result<()> {
    validate_provider(provider)?;
    let region_str = region.as_deref().unwrap_or("us-east-1");

    if json_output {
        let result = serde_json::json!({
            "command": "download",
            "provider": format_provider(provider),
            "bucket": bucket,
            "key": key,
            "region": region_str,
            "output": output.display().to_string(),
            "status": "ready",
            "message": "Download configured; cloud credentials required for execution",
        });
        let s = serde_json::to_string_pretty(&result).context("Failed to serialize")?;
        println!("{s}");
    } else {
        println!("{}", "Cloud Download".green().bold());
        println!("{}", "=".repeat(60));
        println!("{:22} {}", "Provider:", format_provider(provider));
        println!("{:22} {}", "Bucket:", bucket);
        println!("{:22} {}", "Remote key:", key);
        println!("{:22} {}", "Region:", region_str);
        println!("{:22} {}", "Output:", output.display());
        println!();
        println!(
            "{}",
            "Note: Cloud credentials must be configured for actual download.".yellow()
        );
    }

    Ok(())
}

// ---------------------------------------------------------------------------
// Transcode
// ---------------------------------------------------------------------------

async fn run_transcode(
    provider: &str,
    bucket: &str,
    input_key: &str,
    output_key: &str,
    preset: &Option<String>,
    region: &Option<String>,
    json_output: bool,
) -> Result<()> {
    validate_provider(provider)?;
    let region_str = region.as_deref().unwrap_or("us-east-1");
    let preset_str = preset.as_deref().unwrap_or("av1-1080p");

    // Generate a synthetic job ID
    let job_id = format!(
        "job-{:08x}",
        std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap_or_default()
            .as_secs()
    );

    if json_output {
        let result = serde_json::json!({
            "command": "transcode",
            "provider": format_provider(provider),
            "bucket": bucket,
            "input_key": input_key,
            "output_key": output_key,
            "preset": preset_str,
            "region": region_str,
            "job_id": job_id,
            "status": "submitted",
            "message": "Transcode job submitted (simulation); cloud API required for real execution",
        });
        let s = serde_json::to_string_pretty(&result).context("Failed to serialize")?;
        println!("{s}");
    } else {
        println!("{}", "Cloud Transcode".green().bold());
        println!("{}", "=".repeat(60));
        println!("{:22} {}", "Provider:", format_provider(provider));
        println!("{:22} {}", "Bucket:", bucket);
        println!("{:22} {}", "Input key:", input_key);
        println!("{:22} {}", "Output key:", output_key);
        println!("{:22} {}", "Preset:", preset_str);
        println!("{:22} {}", "Region:", region_str);
        println!("{:22} {}", "Job ID:", job_id.cyan());
        println!();
        println!(
            "{}",
            "Note: Cloud API credentials are required for actual transcoding.".yellow()
        );
    }

    Ok(())
}

// ---------------------------------------------------------------------------
// Status
// ---------------------------------------------------------------------------

async fn run_status(
    provider: &str,
    job_id: &str,
    region: &Option<String>,
    json_output: bool,
) -> Result<()> {
    validate_provider(provider)?;
    let region_str = region.as_deref().unwrap_or("us-east-1");

    if json_output {
        let result = serde_json::json!({
            "command": "status",
            "provider": format_provider(provider),
            "job_id": job_id,
            "region": region_str,
            "status": "unknown",
            "message": "Job status lookup requires cloud API credentials",
        });
        let s = serde_json::to_string_pretty(&result).context("Failed to serialize")?;
        println!("{s}");
    } else {
        println!("{}", "Cloud Job Status".green().bold());
        println!("{}", "=".repeat(60));
        println!("{:22} {}", "Provider:", format_provider(provider));
        println!("{:22} {}", "Job ID:", job_id);
        println!("{:22} {}", "Region:", region_str);
        println!();
        println!(
            "{}",
            "Note: Cloud API credentials are required for status lookup.".yellow()
        );
    }

    Ok(())
}

// ---------------------------------------------------------------------------
// Cost estimation
// ---------------------------------------------------------------------------

async fn run_cost(
    provider: &str,
    storage_gb: f64,
    egress_gb: Option<f64>,
    transcode_minutes: Option<f64>,
    region: &Option<String>,
    json_output: bool,
) -> Result<()> {
    let region_str = region.as_deref().unwrap_or("us-east-1");
    let pricing = pricing_for(provider, region_str)?;

    let egress = egress_gb.unwrap_or(0.0);
    let transcode = transcode_minutes.unwrap_or(0.0);

    let storage_cost = storage_gb * pricing.storage_per_gb;
    let egress_cost = egress * pricing.egress_per_gb;
    let transcode_cost = transcode * pricing.transcode_per_min;
    let total_cost = storage_cost + egress_cost + transcode_cost;

    if json_output {
        let result = serde_json::json!({
            "command": "cost",
            "provider": pricing.name,
            "region": region_str,
            "storage_gb": storage_gb,
            "egress_gb": egress,
            "transcode_minutes": transcode,
            "storage_cost_usd": format!("{:.4}", storage_cost),
            "egress_cost_usd": format!("{:.4}", egress_cost),
            "transcode_cost_usd": format!("{:.4}", transcode_cost),
            "total_cost_usd": format!("{:.4}", total_cost),
            "currency": "USD",
            "note": "Estimates based on standard tier pricing",
        });
        let s = serde_json::to_string_pretty(&result).context("Failed to serialize")?;
        println!("{s}");
    } else {
        println!("{}", "Cloud Cost Estimate".green().bold());
        println!("{}", "=".repeat(60));
        println!("{:22} {}", "Provider:", pricing.name);
        println!("{:22} {}", "Region:", region_str);
        println!();
        println!("{}", "Usage".cyan().bold());
        println!("{}", "-".repeat(60));
        println!("{:22} {:.2} GB", "Storage:", storage_gb);
        println!("{:22} {:.2} GB", "Egress:", egress);
        println!("{:22} {:.1} min", "Transcode:", transcode);
        println!();
        println!("{}", "Cost Breakdown (USD/month)".cyan().bold());
        println!("{}", "-".repeat(60));
        println!(
            "{:22} ${:.4}  (${:.4}/GB)",
            "Storage:", storage_cost, pricing.storage_per_gb
        );
        println!(
            "{:22} ${:.4}  (${:.4}/GB)",
            "Egress:", egress_cost, pricing.egress_per_gb
        );
        println!(
            "{:22} ${:.4}  (${:.4}/min)",
            "Transcode:", transcode_cost, pricing.transcode_per_min
        );
        println!("{}", "-".repeat(60));
        println!(
            "{:22} {}",
            "TOTAL:",
            format!("${:.4}", total_cost).green().bold()
        );
        println!();
        println!(
            "{}",
            "Note: Estimates based on standard tier pricing. Actual costs may vary.".dimmed()
        );
    }

    Ok(())
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_validate_provider_known() {
        assert!(validate_provider("s3").is_ok());
        assert!(validate_provider("azure").is_ok());
        assert!(validate_provider("gcs").is_ok());
        assert!(validate_provider("aws").is_ok());
        assert!(validate_provider("google").is_ok());
    }

    #[test]
    fn test_validate_provider_unknown() {
        assert!(validate_provider("dropbox").is_err());
        assert!(validate_provider("").is_err());
    }

    #[test]
    fn test_pricing_s3() {
        let p = pricing_for("s3", "us-east-1");
        assert!(p.is_ok());
        let p = p.expect("should succeed");
        assert!(p.storage_per_gb > 0.0);
        assert!(p.egress_per_gb > 0.0);
        assert!(p.transcode_per_min > 0.0);
    }

    #[test]
    fn test_pricing_unknown() {
        let p = pricing_for("dropbox", "us-east-1");
        assert!(p.is_err());
    }

    #[test]
    fn test_cost_calculation() {
        let p = pricing_for("s3", "us-east-1").expect("should succeed");
        let storage_cost = 100.0 * p.storage_per_gb;
        let egress_cost = 50.0 * p.egress_per_gb;
        let transcode_cost = 120.0 * p.transcode_per_min;
        let total = storage_cost + egress_cost + transcode_cost;
        assert!(total > 0.0);
        // Sanity: 100GB S3 + 50GB egress + 120min transcode should be > $5
        assert!(total > 5.0);
    }
}