summary refs log tree commit diff stats
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
parent39894be3eac7d3410d460c8a7b52a6eae2caf841 (diff)
downloadclinte-eca1e0ad03f3bf83f8548509291905363096f0af.tar.gz
logging reflects executing username v0.3.4
-rw-r--r--Cargo.lock1
-rw-r--r--Cargo.toml1
-rw-r--r--src/logging.rs25
-rw-r--r--src/main.rs63
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;
917'>^
67bb838c ^
a808a661 ^
bba8d293 ^
2a64495f ^
5e449699 ^
fc486c60 ^
7b04e507 ^
0268e3c3 ^
6f43de0a ^




fca1fc4f ^
f70ee6b2 ^
0db4c9b2 ^
b2d63ef5 ^
291ca616 ^
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62