summary refs log tree commit diff stats
path: root/src/main.rs
blob: 8b6cb9a46f9419e7fc65c0b70e7f55702478fbd6 (plain) (blame)
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
use std::time;

#[macro_use]
extern crate lazy_static;

mod conf;
mod db;
mod ed;
mod error;
mod logging;
mod posts;
mod user;

fn main() {
    let arg_matches = &*conf::ARGS;
    let start = time::Instant::now();
    logging::checked_init();

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

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

    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...");
        if let Err(e) = posts::create(&db) {
            log::error!("Error creating new post");
            if *conf::DEBUG {
                log::error!("--> {}", e);
            }
            std::process::exit(1);
        }
    } else if arg_matches.subcommand_matches("update").is_some() {
        let id: u32 = if let Some(val) = arg_matches.subcommand_matches("update_handler") {
            match val.value_of("id").unwrap_or_else(|| "0").parse() {
                Ok(n) => n,
                Err(e) => {
                    log::error!("Couldn't parse ID");
                    if *conf::DEBUG {
                        log::error!("--> {}", e);
                    }
                    std::process::exit(1);
                }
            }
        } else {
            0
        };
        log::info!("Updating post ...");
        if let Err(e) = posts::update_handler(&db, id) {
            log::error!("Error updating post {}", id);
            if *conf::DEBUG {
                log::error!("--> {}", e);
            }
            std::process::exit(1);
        }
    } else if arg_matches.subcommand_matches("delete").is_some() {
        let id: u32 = if let Some(val) = arg_matches.subcommand_matches("update_handler") {
            match val.value_of("id").unwrap_or_else(|| "0").parse() {
                Ok(n) => n,
                Err(_) => {
                    log::error!("Couldn't parse ID");
                    std::process::exit(1);
                }
            }
        } else {
            0
        };
        log::info!("Deleting post");
        if let Err(e) = posts::delete_handler(&db, id) {
            log::error!("Error deleting post {}", id);
            if *conf::DEBUG {
                log::error!("--> {}", e);
            }
            std::process::exit(1);
        }
    }

    if let Err(e) = posts::display(&db) {
        log::error!("Error displaying posts");
        if *conf::DEBUG {
            log::error!("--> {}", e);
        }
        std::process::exit(1);
    }
}