diff --git a/CHANGELOG.md b/CHANGELOG.md index 33b5844..34a35b5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,7 +13,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). - when the image uploaded, send a notification (can be turn off in config) ### Library -- change openssl to rustls +- change OpenSSL to RusTLS ## [0.1.0] - 2022-01-23 ### CLI diff --git a/config.toml b/config.toml index eff69ff..cf97ee9 100644 --- a/config.toml +++ b/config.toml @@ -2,4 +2,4 @@ id = '3e3ce0d7ac14d56' [notification] -enable = true +enabled = true diff --git a/src/api/image_type.rs b/src/api/image_type.rs index 5f46c13..15c3ebc 100644 --- a/src/api/image_type.rs +++ b/src/api/image_type.rs @@ -3,6 +3,8 @@ use serde_derive::{Deserialize, Serialize}; #[derive(Debug, Serialize, Deserialize)] pub struct ImageInfo { pub data: ImageInfoData, + pub success: bool, + pub status: i32, } #[derive(Debug, Serialize, Deserialize)] diff --git a/src/cli/mod.rs b/src/cli/mod.rs index 559fff6..b80c746 100644 --- a/src/cli/mod.rs +++ b/src/cli/mod.rs @@ -50,9 +50,9 @@ pub fn print_image_info(i: ImageInfo, notify: bool) { info!("Bandwidth {}", i.data.bandwidth); info!("Link {}", i.data.link); - let config = parse().unwrap(); + let config = parse(); - if notify && config.notification.enable { + if notify && config.notification.enabled { Notification::new() .summary("Imgurs") .body(&format!("Uploaded {}", i.data.link)) diff --git a/src/config/mod.rs b/src/config/mod.rs index 47cc331..544585c 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -1 +1,19 @@ +use serde_derive::Deserialize; + pub mod toml; + +#[derive(Debug, Deserialize)] +pub struct Config { + pub imgur: ConfigImgur, + pub notification: ConfigNotification, +} + +#[derive(Debug, Deserialize)] +pub struct ConfigImgur { + pub id: String, +} + +#[derive(Debug, Deserialize)] +pub struct ConfigNotification { + pub enabled: bool, +} diff --git a/src/config/toml.rs b/src/config/toml.rs index 7f3a364..96a2dfc 100644 --- a/src/config/toml.rs +++ b/src/config/toml.rs @@ -1,26 +1,45 @@ use dirs::config_dir; -use serde_derive::Deserialize; -use std::fs::read_to_string; use toml::from_str; -#[derive(Debug, Deserialize)] -pub struct Config { - pub imgur: ConfigImgur, - pub notification: ConfigNotification, +use super::Config; +use log::{error, warn}; + +use std::{ + fs::{create_dir_all, read_to_string, File}, + io::Write as _, + path::Path, + process::exit, +}; + +pub fn parse() -> Config { + toml().unwrap_or_else(|error| { + warn!("Parse toml config: {}! Creating config file...", error); + + let default_config = include_str!(concat!("../../config.toml")); + + let sys_config_dir = config_dir().unwrap(); + let config_dir = format!("{}/imgurs/config.toml", sys_config_dir.to_string_lossy()); + let config_path = Path::new(&config_dir); + + let prefix = config_path.parent().unwrap(); + create_dir_all(prefix).unwrap(); + + let mut file_ref = File::create(config_path).expect("create failed"); + + file_ref + .write_all(default_config.as_bytes()) + .expect("failed write default config to file"); + + toml().unwrap_or_else(|error| { + error!("parse toml config: {}", error); + exit(2); + }) + }) } -#[derive(Debug, Deserialize)] -pub struct ConfigImgur { - pub id: String, -} - -#[derive(Debug, Deserialize)] -pub struct ConfigNotification { - pub enable: bool, -} - -pub fn parse() -> Result { +fn toml() -> Result { let config_dir = config_dir().unwrap(); + let file_dir: String = String::from(config_dir.to_string_lossy()) + "/imgurs/config.toml"; let toml_str = read_to_string(file_dir).map_err(|err| err.to_string())?; diff --git a/src/main.rs b/src/main.rs index f1c6e31..09c5607 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,13 +3,7 @@ mod config; use cli::parse::parse; -use dirs::config_dir; -use log::warn; use simple_logger::SimpleLogger; -use std::{fs::create_dir_all, path::Path}; - -use std::fs::File; -use std::io::Write as _; use imgurs::api::ImgurHandle; @@ -17,26 +11,7 @@ use imgurs::api::ImgurHandle; async fn main() { SimpleLogger::new().init().unwrap(); - let config = config::toml::parse().unwrap_or_else(|error| { - warn!("Parse toml config: {}! Creating config file...", error); - - let default_config = include_str!(concat!("../config.toml")); - - let sys_config_dir = config_dir().unwrap(); - let config_dir = format!("{}/imgurs/config.toml", sys_config_dir.to_string_lossy()); - let config_path = Path::new(&config_dir); - - let prefix = config_path.parent().unwrap(); - create_dir_all(prefix).unwrap(); - - let mut file_ref = File::create(config_path).expect("create failed"); - - file_ref - .write_all(default_config.as_bytes()) - .expect("failed write default config to file"); - - config::toml::parse().unwrap() - }); + let config = config::toml::parse(); let client = ImgurHandle::new((&config.imgur.id).to_string());