summary refs log tree commit diff stats
path: root/src
diff options
context:
space:
mode:
authorBen Morrison <ben@gbmor.dev>2019-09-04 10:27:48 -0400
committerBen Morrison <ben@gbmor.dev>2019-09-04 10:27:48 -0400
commiteca1e0ad03f3bf83f8548509291905363096f0af (patch)
tree3388dd9f05ed8afa8f2a48816e6640f73c3990bf /src
parent39894be3eac7d3410d460c8a7b52a6eae2caf841 (diff)
downloadclinte-eca1e0ad03f3bf83f8548509291905363096f0af.tar.gz
logging reflects executing username v0.3.4
Diffstat (limited to 'src')
-rw-r--r--src/logging.rs25
-rw-r--r--src/main.rs63
2 files changed, 55 insertions, 33 deletions
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;