2022-01-25 16:13:43 +00:00
|
|
|
use clap::{AppSettings, Parser, Subcommand, App, IntoApp};
|
|
|
|
use clap_complete::{generate, Shell, Generator};
|
|
|
|
use log::error;
|
|
|
|
|
|
|
|
use std::io::stdout;
|
2022-01-22 21:02:51 +00:00
|
|
|
|
|
|
|
use imgurs::api::configuration::ImgurHandle;
|
|
|
|
|
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(
|
|
|
|
setting(AppSettings::ArgRequiredElseHelp),
|
2022-01-25 16:13:43 +00:00
|
|
|
about = "Upload image to Imgur"
|
2022-01-22 21:02:51 +00:00
|
|
|
)]
|
2022-01-22 21:14:26 +00:00
|
|
|
Upload { path: String },
|
2022-01-22 21:02:51 +00:00
|
|
|
|
|
|
|
#[clap(
|
|
|
|
setting(AppSettings::ArgRequiredElseHelp),
|
2022-01-25 16:13:43 +00:00
|
|
|
about = "Delete image from Imgur"
|
2022-01-22 21:02:51 +00:00
|
|
|
)]
|
2022-01-22 21:14:26 +00:00
|
|
|
Delete { delete_hash: String },
|
2022-01-23 12:05:32 +00:00
|
|
|
|
|
|
|
#[clap(setting(AppSettings::ArgRequiredElseHelp), about = "Print image info")]
|
|
|
|
Info { id: String },
|
2022-01-25 16:13:43 +00:00
|
|
|
|
|
|
|
#[clap(setting(AppSettings::ArgRequiredElseHelp), about = "Print shell completions (bash, zsh, fish, powershell)")]
|
|
|
|
Completions {
|
|
|
|
shell: String,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
fn print_completions<G: Generator>(gen: G, app: &mut App) {
|
|
|
|
generate(gen, app, app.get_name().to_string(), &mut stdout())
|
2022-01-22 21:02:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn parse(client: ImgurHandle) {
|
|
|
|
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::into_app();
|
|
|
|
|
|
|
|
if shell == "bash" {
|
|
|
|
print_completions(Shell::Bash, &mut app);
|
|
|
|
} else if shell == "zsh" {
|
|
|
|
print_completions(Shell::Zsh, &mut app);
|
|
|
|
} else if shell == "fish" {
|
|
|
|
print_completions(Shell::Fish, &mut app);
|
|
|
|
} else if shell == "powershell" {
|
|
|
|
print_completions(Shell::PowerShell, &mut app);
|
|
|
|
} else {
|
|
|
|
error!("Completions to shell `{shell}`, not found!")
|
|
|
|
}
|
|
|
|
}
|
2022-01-22 21:02:51 +00:00
|
|
|
}
|
|
|
|
}
|