add shell completions

This commit is contained in:
MedzikUser 2022-01-25 17:13:43 +01:00
parent 37f51f97dd
commit a83788d0c6
No known key found for this signature in database
GPG Key ID: A5FAC1E185C112DB
10 changed files with 92 additions and 1835 deletions

5
.gitignore vendored
View File

@ -1 +1,6 @@
# Generated by Cargo
# will have compiled files and executables
/debug
/target /target
Cargo.lock

View File

@ -8,13 +8,18 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
<!-- next-header --> <!-- next-header -->
## [Unreleased] ## [Unreleased]
### CLI ### Added
#### CLI
- create default config, if not exits - create default config, if not exits
- when the image uploaded, send a notification (can be turn off in config) - when the image uploaded, send a notification (can be turn off in config)
- added completions
### Library #### Library
- change OpenSSL to RusTLS - change OpenSSL to RusTLS
### Fixed
* api rate limit (error decoding response body: invalid value: integer \`200\`, expected i8 at line 1 column 140)
## [0.1.0] - 2022-01-23 ## [0.1.0] - 2022-01-23
### CLI ### CLI
- commands - commands

1769
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -16,10 +16,11 @@ serde_json = "1.0.75"
chrono = "0.4.19" chrono = "0.4.19"
base64 = "0.13.0" base64 = "0.13.0"
notify-rust = "4.5.5" notify-rust = "4.5.5"
clap_complete = "3.0.5"
[dependencies.clap] [dependencies.clap]
version = "3.0.7" version = "3.0.7"
features = ["derive", "cargo", "unicode"] features = ["derive", "cargo", "std"]
[dependencies.log] [dependencies.log]
version = "0.4.14" version = "0.4.14"

View File

@ -1,28 +1,26 @@
use imgurs::api::rate_limit::*; use imgurs::api::{rate_limit::rate_limit, ImgurHandle};
use imgurs::api::ImgurHandle;
use log::{error, info}; use log::{error, info};
use chrono::prelude::DateTime; use chrono::{prelude::DateTime, Utc};
use chrono::Utc; use std::{
use std::time::{Duration, UNIX_EPOCH}; process::exit,
time::{Duration, UNIX_EPOCH},
};
pub async fn credits(client: ImgurHandle) { pub async fn credits(client: ImgurHandle) {
match rate_limit(client).await { let i = rate_limit(client).await.unwrap_or_else(|e| {
Ok(i) => { error!("{e}");
let d = UNIX_EPOCH + Duration::from_secs(i.data.user_reset.try_into().unwrap()); exit(1);
let datetime = DateTime::<Utc>::from(d); });
let timestamp_str = datetime.format("%Y-%m-%d %H:%M:%S").to_string();
info!("User Limit {}", i.data.user_limit); let d = UNIX_EPOCH + Duration::from_secs(i.data.user_reset.try_into().unwrap());
info!("User Remaining {}", i.data.user_remaining); let datetime = DateTime::<Utc>::from(d);
info!("User Reset {} (UTC)", timestamp_str); let timestamp_str = datetime.format("%Y-%m-%d %H:%M:%S").to_string();
info!("Client Limit {}", i.data.client_limit);
info!("Client Remaining {}", i.data.client_remaining);
}
Err(e) => { info!("User Limit {}", i.data.user_limit);
error!("{}", e); info!("User Remaining {}", i.data.user_remaining);
} info!("User Reset {} (UTC)", timestamp_str);
} info!("Client Limit {}", i.data.client_limit);
info!("Client Remaining {}", i.data.client_remaining);
} }

View File

@ -1,16 +1,13 @@
use imgurs::api; use imgurs::api::{configuration::ImgurHandle, delete_image::delete_image as del_img};
use imgurs::api::configuration::ImgurHandle;
use log::{error, info}; use log::{error, info};
use std::process::exit;
pub async fn delete_image(client: ImgurHandle, delete_hash: String) { pub async fn delete_image(client: ImgurHandle, delete_hash: String) {
match api::delete_image::delete_image(client, delete_hash).await { let i = del_img(client, delete_hash).await.unwrap_or_else(|e| {
Ok(i) => { error!("{e}");
info!("{i}") exit(1);
} });
Err(e) => { info!("{i}");
error!("{}", e);
}
}
} }

View File

@ -2,22 +2,14 @@ use imgurs::api::{configuration::ImgurHandle, get_image::get_image};
use super::print_image_info; use super::print_image_info;
use base64;
use log::error; use log::error;
use std::fs; use std::process::exit;
use std::path::Path;
pub async fn image_info(client: ImgurHandle, path: &str) { pub async fn image_info(client: ImgurHandle, id: &str) {
let mut image: String = path.to_string(); let i = get_image(client, id).await.unwrap_or_else(|e| {
error!("{e}");
exit(1);
});
if Path::new(path).exists() { print_image_info(i, false);
let bytes = fs::read(path).map_err(|err| err.to_string()).unwrap();
image = base64::encode(bytes);
}
match get_image(client, &image).await {
Ok(i) => print_image_info(i, false),
Err(e) => error!("{e}"),
}
} }

View File

@ -4,15 +4,13 @@ pub mod info_image;
pub mod parse; pub mod parse;
pub mod upload_image; pub mod upload_image;
use chrono::prelude::DateTime; use chrono::{prelude::DateTime, Utc};
use chrono::Utc;
use log::{error, info}; use log::{error, info};
use std::time::{Duration, UNIX_EPOCH}; use std::{time::{Duration, UNIX_EPOCH}, process::exit};
use imgurs::api::ImageInfo; use imgurs::api::ImageInfo;
use notify_rust::Notification; use notify_rust::Notification;
use std::process::exit;
use crate::config::toml::parse; use crate::config::toml::parse;

View File

@ -1,4 +1,8 @@
use clap::{AppSettings, Parser, Subcommand}; use clap::{AppSettings, Parser, Subcommand, App, IntoApp};
use clap_complete::{generate, Shell, Generator};
use log::error;
use std::io::stdout;
use imgurs::api::configuration::ImgurHandle; use imgurs::api::configuration::ImgurHandle;
@ -20,23 +24,32 @@ struct Cli {
#[derive(Subcommand, Debug)] #[derive(Subcommand, Debug)]
enum Commands { enum Commands {
#[clap(about = "Get API ratelimit")] #[clap(about = "Print API Rate Limit")]
Credits, Credits,
#[clap( #[clap(
setting(AppSettings::ArgRequiredElseHelp), setting(AppSettings::ArgRequiredElseHelp),
about = "Upload image to imgur" about = "Upload image to Imgur"
)] )]
Upload { path: String }, Upload { path: String },
#[clap( #[clap(
setting(AppSettings::ArgRequiredElseHelp), setting(AppSettings::ArgRequiredElseHelp),
about = "Delete image from imgur" about = "Delete image from Imgur"
)] )]
Delete { delete_hash: String }, Delete { delete_hash: String },
#[clap(setting(AppSettings::ArgRequiredElseHelp), about = "Print image info")] #[clap(setting(AppSettings::ArgRequiredElseHelp), about = "Print image info")]
Info { id: String }, Info { id: String },
#[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())
} }
pub async fn parse(client: ImgurHandle) { pub async fn parse(client: ImgurHandle) {
@ -58,5 +71,21 @@ pub async fn parse(client: ImgurHandle) {
Commands::Info { id } => { Commands::Info { id } => {
image_info(client, id).await; image_info(client, id).await;
} }
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!")
}
}
} }
} }

View File

@ -1,22 +1,23 @@
use super::print_image_info; use super::print_image_info;
use imgurs::api::configuration::ImgurHandle; use imgurs::api::{configuration::ImgurHandle, upload_image::upload_image as upload_img};
use base64; use base64::encode as base64_encode;
use log::error; use log::error;
use std::fs; use std::{fs::read as fs_read, process::exit, path::Path};
use std::path::Path;
pub async fn upload_image(client: ImgurHandle, path: &str) { pub async fn upload_image(client: ImgurHandle, path: &str) {
let mut image: String = path.to_string(); let mut image: String = path.to_string();
if Path::new(path).exists() { if Path::new(path).exists() {
let bytes = fs::read(path).map_err(|err| err.to_string()).unwrap(); let bytes = fs_read(path).map_err(|err| err.to_string()).unwrap();
image = base64::encode(bytes); image = base64_encode(bytes);
} }
match imgurs::api::upload_image::upload_image(client, &image).await { let i = upload_img(client, &image).await.unwrap_or_else(|e| {
Ok(i) => print_image_info(i, true), error!("{e}");
Err(e) => error!("{e}"), exit(1);
} });
print_image_info(i, true);
} }