diff options
-rw-r--r-- | Cargo.lock | 1 | ||||
-rw-r--r-- | Cargo.toml | 1 | ||||
-rw-r--r-- | src/logging.rs | 25 | ||||
-rw-r--r-- | src/main.rs | 63 |
4 files changed, 57 insertions, 33 deletions
diff --git a/Cargo.lock b/Cargo.lock index f56227c..efa98b6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -123,6 +123,7 @@ version = "0.3.3" dependencies = [ "chrono 0.4.7 (registry+https://github.com/rust-lang/crates.io-index)", "clap 2.33.0 (registry+https://github.com/rust-lang/crates.io-index)", + "lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)", "rusqlite 0.20.0 (registry+https://github.com/rust-lang/crates.io-index)", "simplelog 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/Cargo.toml b/Cargo.toml index 728c3c8..0b72d94 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,6 +8,7 @@ edition = "2018" [dependencies] chrono = "^0.4" +lazy_static = "^1.4" log = "^0.4" rusqlite = "^0.20" simplelog = "^0.7" diff --git a/src/logging.rs b/src/logging.rs index 6340def..7b8bd2e 100644 --- a/src/logging.rs +++ b/src/logging.rs @@ -3,19 +3,28 @@ use std::fs::File; use chrono::offset::Utc; use simplelog::*; - -pub const FILE: &str = "/tmp/clinte.log"; +use users; + +lazy_static! { + static ref FILE: String = format!( + "/tmp/clinte_{}.log", + users::get_current_username() + .unwrap() + .into_string() + .unwrap() + ); +} pub fn init() { // If the log file exists on startup, // move and timestamp it so we get a // fresh log file. - if fs::metadata(FILE).is_ok() { - let mut newpath = FILE.to_string(); + if fs::metadata(FILE.clone()).is_ok() { + let mut new_file = FILE.clone(); let time = Utc::now().to_rfc3339(); - newpath.push_str("."); - newpath.push_str(&time); - fs::rename(FILE, newpath).unwrap(); + new_file.push_str("."); + new_file.push_str(&time); + fs::rename(FILE.clone(), new_file).unwrap(); } CombinedLogger::init(vec![ @@ -23,7 +32,7 @@ pub fn init() { WriteLogger::new( LevelFilter::Info, Config::default(), - File::create(FILE).unwrap(), + File::create(FILE.clone()).unwrap(), ), ]) .expect("Unable to initialize logging"); diff --git a/src/main.rs b/src/main.rs index 65652d9..b7d6f7a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,6 @@ +#[macro_use] +extern crate lazy_static; + use clap; use log::info; use std::io; @@ -14,16 +17,8 @@ fn main() { .author("Ben Morrison (gbmor)") .about("Command-line community notices system") .subcommand(clap::SubCommand::with_name("post").about("Post a new notice")) - .subcommand( - clap::SubCommand::with_name("update") - .about("Update a notice you've posted") - .arg(clap::Arg::with_name("id").help("Numeric ID of the post")), - ) - .subcommand( - clap::SubCommand::with_name("delete") - .about("Delete a notice you've posted") - .arg(clap::Arg::with_name("id").help("Numeric ID of the post")), - ) + .subcommand(clap::SubCommand::with_name("update").about("Update a notice you've posted")) + .subcommand(clap::SubCommand::with_name("delete").about("Delete a notice you've posted")) .get_matches(); let start = time::Instant::now(); @@ -41,11 +36,21 @@ fn main() { info!("New post..."); post(&db); } else if arg_matches.subcommand_matches("update").is_some() { + let id: u32 = if let Some(val) = arg_matches.subcommand_matches("update") { + val.value_of("id").unwrap().parse().unwrap() + } else { + 0 + }; info!("Updating post ..."); - update(&db); + update(&db, id); } else if arg_matches.subcommand_matches("delete").is_some() { + let id: u32 = if let Some(val) = arg_matches.subcommand_matches("update") { + val.value_of("id").unwrap().parse().unwrap() + } else { + 0 + }; info!("Deleting post"); - delete(&db); + delete(&db, id); } posts::display(&db); @@ -96,17 +101,21 @@ fn post(db: &db::Conn) { println!(); } -fn update(db: &db::Conn) { +fn update(db: &db::Conn, id: u32) { let cur_user = users::get_current_username() .unwrap() .into_string() .unwrap(); - println!(); - println!("ID number of your post to edit?"); - let mut id_num_in = String::new(); - io::stdin().read_line(&mut id_num_in).unwrap(); - let id_num_in: u32 = id_num_in.trim().parse().unwrap(); + let id_num_in = if id == 0 { + println!(); + println!("ID number of your post to edit?"); + let mut id_num_in = String::new(); + io::stdin().read_line(&mut id_num_in).unwrap(); + id_num_in.trim().parse().unwrap() + } else { + id + }; let mut get_stmt = db .conn @@ -145,18 +154,21 @@ fn update(db: &db::Conn) { posts::update(&new_title, &new_body, id_num_in, &db).unwrap(); } -fn delete(db: &db::Conn) { +fn delete(db: &db::Conn, id: u32) { let cur_user = users::get_current_username() .unwrap() .into_string() .unwrap(); - println!(); - println!("ID of the post to delete?"); - let mut id_num_in = String::new(); - io::stdin().read_line(&mut id_num_in).unwrap(); - let id_num_in: u32 = id_num_in.trim().parse().unwrap(); - println!(); + let id_num_in: u32 = if id == 0 { + println!(); + println!("ID of the post to delete?"); + let mut id_num_in = String::new(); + io::stdin().read_line(&mut id_num_in).unwrap(); + id_num_in.trim().parse().unwrap() + } else { + id + }; let del_stmt = format!("DELETE FROM posts WHERE id = {}", id_num_in); let get_stmt = format!("SELECT * FROM posts WHERE id = {}", id_num_in); @@ -169,6 +181,7 @@ fn delete(db: &db::Conn) { .unwrap(); if cur_user != user_in_post { + println!(); println!("Users don't match. Can't delete!"); println!(); return; |