~gbmor/clinte

cf3b6628ce4deb429fad407829b425a7377e2b5d — Ben Morrison 1 year, 2 months ago 16fbf51
hiding some log messages behind -v flag
4 files changed, 49 insertions(+), 20 deletions(-)

A src/conf.rs
M src/db.rs
M src/ed.rs
M src/main.rs
A src/conf.rs => src/conf.rs +23 -0
@@ 0,0 1,23 @@
use clap::{Arg, ArgMatches};

lazy_static! {
    pub static ref ARGS: ArgMatches<'static> = get_config();
    pub static ref DEBUG: bool = ARGS.is_present("verbose");
}

fn get_config() -> clap::ArgMatches<'static> {
    clap::App::new("clinte")
        .version(clap::crate_version!())
        .author("Ben Morrison <ben@gbmor.dev>")
        .about("Command-line community notices system")
        .arg(
            Arg::with_name("verbose")
                .short("v")
                .long("verbose")
                .help("Verbose logging"),
        )
        .subcommand(clap::SubCommand::with_name("post").about("Post a new notice"))
        .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()
}

M src/db.rs => src/db.rs +13 -5
@@ 1,5 1,7 @@
use std::time;

use crate::conf;

const DB_PATH: &str = "/usr/local/clinte/clinte.db";

#[derive(Debug)]


@@ 18,7 20,11 @@ pub struct Conn {
impl Conn {
    pub fn init(path: &str) -> rusqlite::Connection {
        let start = time::Instant::now();
        log::info!("Connecting to database");

        if *conf::DEBUG {
            log::info!("Connecting to database");
        }

        let conn = rusqlite::Connection::open_with_flags(
            path,
            rusqlite::OpenFlags::SQLITE_OPEN_FULL_MUTEX


@@ 38,10 44,12 @@ impl Conn {
        )
        .expect("Could not initialize DB");

        log::info!(
            "Database connection established in {}ms",
            start.elapsed().as_millis()
        );
        if *conf::DEBUG {
            log::info!(
                "Database connection established in {}ms",
                start.elapsed().as_millis()
            );
        }

        conn
    }

M src/ed.rs => src/ed.rs +5 -1
@@ 4,6 4,7 @@ use std::process;

use chrono::prelude::*;

use crate::conf;
use crate::error;
use crate::user;



@@ 13,7 14,10 @@ fn create_tmp_file<'a>() -> Result<String, &'a str> {
    match fs::write(&file_name, "") {
        Ok(_) => Ok(file_name),
        Err(err) => {
            log::warn!("{:?}", err);
            log::warn!("Couldn't create tempfile");
            if *conf::DEBUG {
                log::warn!("--> {:?}", err);
            }
            Err("Unable to create temp file")
        }
    }

M src/main.rs => src/main.rs +8 -14
@@ 3,8 3,7 @@ use std::time;
#[macro_use]
extern crate lazy_static;

use clap;

mod conf;
mod db;
mod ed;
mod error;


@@ 13,18 12,11 @@ mod posts;
mod user;

fn main() -> error::Result<()> {
    let arg_matches = clap::App::new("clinte")
        .version(clap::crate_version!())
        .author("Ben Morrison <ben@gbmor.dev>")
        .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"))
        .subcommand(clap::SubCommand::with_name("delete").about("Delete a notice you've posted"))
        .get_matches();

    let arg_matches = &*conf::ARGS;
    let start = time::Instant::now();
    let file = format!("/tmp/clinte_{}.log", *user::NAME);
    logging::init(&file)?;
    let logfile = format!("/tmp/clinte_{}.log", *user::NAME);
    logging::init(&logfile)?;

    log::info!("clinte starting up!");
    println!("clinte v{}", clap::crate_version!());
    println!("a community notices system");


@@ 32,7 24,9 @@ fn main() -> error::Result<()> {

    let db = db::Conn::new();

    log::info!("Startup completed in {:?}ms", start.elapsed().as_millis());
    if *conf::DEBUG {
        log::info!("Startup completed in {:?}ms", start.elapsed().as_millis());
    }

    if arg_matches.subcommand_matches("post").is_some() {
        log::info!("New post...");