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
Cargo.lock

View File

@ -8,13 +8,18 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
<!-- next-header -->
## [Unreleased]
### CLI
### Added
#### CLI
- create default config, if not exits
- when the image uploaded, send a notification (can be turn off in config)
- added completions
### Library
#### Library
- 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
### CLI
- 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"
base64 = "0.13.0"
notify-rust = "4.5.5"
clap_complete = "3.0.5"
[dependencies.clap]
version = "3.0.7"
features = ["derive", "cargo", "unicode"]
features = ["derive", "cargo", "std"]
[dependencies.log]
version = "0.4.14"

View File

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

View File

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

View File

@ -4,15 +4,13 @@ pub mod info_image;
pub mod parse;
pub mod upload_image;
use chrono::prelude::DateTime;
use chrono::Utc;
use chrono::{prelude::DateTime, Utc};
use log::{error, info};
use std::time::{Duration, UNIX_EPOCH};
use std::{time::{Duration, UNIX_EPOCH}, process::exit};
use imgurs::api::ImageInfo;
use notify_rust::Notification;
use std::process::exit;
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;
@ -20,23 +24,32 @@ struct Cli {
#[derive(Subcommand, Debug)]
enum Commands {
#[clap(about = "Get API ratelimit")]
#[clap(about = "Print API Rate Limit")]
Credits,
#[clap(
setting(AppSettings::ArgRequiredElseHelp),
about = "Upload image to imgur"
about = "Upload image to Imgur"
)]
Upload { path: String },
#[clap(
setting(AppSettings::ArgRequiredElseHelp),
about = "Delete image from imgur"
about = "Delete image from Imgur"
)]
Delete { delete_hash: String },
#[clap(setting(AppSettings::ArgRequiredElseHelp), about = "Print image info")]
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) {
@ -58,5 +71,21 @@ pub async fn parse(client: ImgurHandle) {
Commands::Info { id } => {
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 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 std::fs;
use std::path::Path;
use std::{fs::read as fs_read, process::exit, path::Path};
pub async fn upload_image(client: ImgurHandle, path: &str) {
let mut image: String = path.to_string();
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 {
Ok(i) => print_image_info(i, true),
Err(e) => error!("{e}"),
}
let i = upload_img(client, &image).await.unwrap_or_else(|e| {
error!("{e}");
exit(1);
});
print_image_info(i, true);
}