imgurs/src/cli/parse.rs

79 lines
2.1 KiB
Rust
Raw Normal View History

use imgurs::api::ImgurClient;
use clap::{Command, IntoApp, Parser, Subcommand};
2022-01-25 16:14:23 +00:00
use clap_complete::{generate, Generator, Shell};
2022-01-25 16:13:43 +00:00
use std::io::stdout;
2022-01-22 21:02:51 +00:00
2022-01-23 12:05:32 +00:00
use crate::cli::{credits::*, delete_image::*, info_image::*, upload_image::*};
2022-01-22 21:02:51 +00:00
const VERSION: Option<&str> = option_env!("CARGO_PKG_VERSION");
2022-01-23 12:05:32 +00:00
const NAME: Option<&str> = option_env!("CARGO_PKG_NAME");
2022-01-22 21:02:51 +00:00
#[derive(Parser, Debug)]
#[clap(
2022-01-23 12:05:32 +00:00
name = NAME.unwrap_or("unknown"),
2022-01-22 21:02:51 +00:00
about = "Imgur API CLI", long_about = None,
version = VERSION.unwrap_or("unknown")
)]
struct Cli {
#[clap(subcommand)]
command: Commands,
}
#[derive(Subcommand, Debug)]
enum Commands {
2022-01-25 16:13:43 +00:00
#[clap(about = "Print API Rate Limit")]
2022-01-22 21:02:51 +00:00
Credits,
#[clap(about = "Upload image to Imgur")]
2022-01-22 21:14:26 +00:00
Upload { path: String },
2022-01-22 21:02:51 +00:00
#[clap(about = "Delete image from Imgur")]
2022-01-22 21:14:26 +00:00
Delete { delete_hash: String },
2022-01-23 12:05:32 +00:00
#[clap(about = "Print image info")]
2022-01-23 12:05:32 +00:00
Info { id: String },
2022-01-25 16:13:43 +00:00
#[clap(about = "Print shell completions (bash, zsh, fish, powershell)")]
2022-01-25 16:14:23 +00:00
Completions { shell: String },
2022-01-25 16:13:43 +00:00
}
fn print_completions<G: Generator>(gen: G, app: &mut Command) {
2022-01-25 16:13:43 +00:00
generate(gen, app, app.get_name().to_string(), &mut stdout())
2022-01-22 21:02:51 +00:00
}
pub async fn parse(client: ImgurClient) {
2022-01-22 21:02:51 +00:00
let args = Cli::parse();
match &args.command {
Commands::Credits => {
2022-01-23 12:05:32 +00:00
credits(client).await;
2022-01-22 21:02:51 +00:00
}
Commands::Upload { path } => {
2022-01-23 12:05:32 +00:00
upload_image(client, path).await;
2022-01-22 21:02:51 +00:00
}
Commands::Delete { delete_hash } => {
2022-01-23 12:05:32 +00:00
delete_image(client, delete_hash.to_string()).await;
}
Commands::Info { id } => {
image_info(client, id).await;
2022-01-22 21:02:51 +00:00
}
2022-01-25 16:13:43 +00:00
Commands::Completions { shell } => {
let mut app = Cli::command();
2022-01-25 16:13:43 +00:00
match shell.as_str() {
"bash" => print_completions(Shell::Bash, &mut app),
"zsh" => print_completions(Shell::Zsh, &mut app),
"fish" => print_completions(Shell::Fish, &mut app),
"powershell" => print_completions(Shell::PowerShell, &mut app),
2022-02-27 10:39:02 +00:00
_ => panic!("Completions to shell `{shell}`, not found!"),
2022-01-25 16:13:43 +00:00
}
}
2022-01-22 21:02:51 +00:00
}
}