use crate::output::{OutputFormat, print_output};
use anyhow::Result;
use clap::{Args, Subcommand};
use grr_drive::{DriveClient, FileListOptions};
use std::path::Path;
#[derive(Subcommand, Debug)]
pub enum DriveCommands {
List(ListArgs),
Search(SearchArgs),
Get(GetArgs),
Download(DownloadArgs),
Upload(UploadArgs),
Rename(RenameArgs),
Delete(DeleteArgs),
Quota(QuotaArgs),
}
#[derive(Args, Debug)]
pub struct ListArgs {
#[arg(short, long)]
pub query: Option<String>,
#[arg(short, long, default_value = "25")]
pub max: usize,
#[arg(short, long, value_enum, default_value = "json")]
pub format: OutputFormat,
}
#[derive(Args, Debug)]
pub struct SearchArgs {
#[arg(short, long)]
pub query: String,
#[arg(short, long, default_value = "25")]
pub max: usize,
#[arg(short, long, value_enum, default_value = "json")]
pub format: OutputFormat,
}
#[derive(Args, Debug)]
pub struct GetArgs {
pub file_id: String,
#[arg(short, long, value_enum, default_value = "json")]
pub format: OutputFormat,
}
#[derive(Args, Debug)]
pub struct DownloadArgs {
pub file_id: String,
#[arg(short, long)]
pub output: String,
}
#[derive(Args, Debug)]
pub struct UploadArgs {
pub file: String,
#[arg(long)]
pub name: Option<String>,
#[arg(long)]
pub parent: Option<String>,
#[arg(short, long, value_enum, default_value = "json")]
pub format: OutputFormat,
}
#[derive(Args, Debug)]
pub struct RenameArgs {
pub file_id: String,
pub new_name: String,
#[arg(short, long, value_enum, default_value = "json")]
pub format: OutputFormat,
}
#[derive(Args, Debug)]
pub struct DeleteArgs {
pub file_id: String,
}
#[derive(Args, Debug)]
pub struct QuotaArgs {
#[arg(short, long, value_enum, default_value = "json")]
pub format: OutputFormat,
}
pub async fn handle_drive_cmd(client: &DriveClient, cmd: DriveCommands) -> Result<()> {
match cmd {
DriveCommands::List(args) => {
let opts = FileListOptions {
q: Some(args.query.unwrap_or_else(|| "trashed = false".into())),
max_results: args.max,
};
let files = client.list_files(opts).await?;
print_output(&files, args.format)?;
}
DriveCommands::Search(args) => {
let opts = FileListOptions {
q: Some(args.query),
max_results: args.max,
};
let files = client.list_files(opts).await?;
print_output(&files, args.format)?;
}
DriveCommands::Get(args) => {
let file = client.get_file(&args.file_id).await?;
print_output(&file, args.format)?;
}
DriveCommands::Download(args) => {
let written = client
.download_file(&args.file_id, Path::new(&args.output))
.await?;
println!("Downloaded {written} bytes to {}", args.output);
}
DriveCommands::Upload(args) => {
let file = client
.upload_file(
Path::new(&args.file),
args.name.as_deref(),
args.parent.as_deref(),
)
.await?;
print_output(&file, args.format)?;
}
DriveCommands::Rename(args) => {
let file = client.rename_file(&args.file_id, &args.new_name).await?;
print_output(&file, args.format)?;
}
DriveCommands::Delete(args) => {
client.delete_file(&args.file_id).await?;
println!("File {} deleted", args.file_id);
}
DriveCommands::Quota(args) => {
let about = client.about().await?;
print_output(&about, args.format)?;
}
}
Ok(())
}