summaryrefslogtreecommitdiffstats
path: root/src/bin/client/main.rs
blob: d983e58f9571b9d5584a5db9c00229b69003f46b (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
#[macro_use]
mod macros;
mod connection;
mod repl;

use repl::ClientState;
use zagan_bet::client::paths::{database_path, default_data_dir, parse_args, USAGE};

fn main() {
    let args = match parse_args(std::env::args().skip(1)) {
        Ok(args) => args,
        Err(message) => {
            eprintln!("{message}");
            std::process::exit(2);
        }
    };

    if args.help {
        println!("{USAGE}");
        return;
    }

    // Every game board is drawn in place, so refuse a terminal that cannot fit
    // them before touching the network or the terminal mode.
    let size = crossterm::terminal::size();
    let (columns, rows) = match &size {
        Ok((columns, rows)) => (*columns, *rows),
        Err(_) => (0, 0),
    };
    if let Some(message) = zagan_bet::client::ui::terminal_too_small(columns, rows) {
        eprintln!("{message}");
        if size.is_err() {
            eprintln!("Could not read the terminal size (is this a real terminal?).");
        }
        std::process::exit(1);
    }

    if args.unsafe_mode {
        println!("WARNING: Running in UNSAFE mode - certificate verification disabled!");
    }

    connection::set_unsafe_mode(args.unsafe_mode);

    let data_dir = args.data_dir.unwrap_or_else(default_data_dir);
    if let Err(error) = std::fs::create_dir_all(&data_dir) {
        eprintln!("Cannot create {}: {error}", data_dir.display());
        std::process::exit(1);
    }
    let db_path = database_path(&data_dir);

    let mut state = match ClientState::new(&db_path) {
        Ok(state) => state,
        Err(error) => {
            eprintln!("{error}");
            std::process::exit(1);
        }
    };
    if let Some(host) = state.addr.clone() {
        state.connect_to(&host);
    }

    println!("Welcome to Zagan-Bet Client!");
    println!("Type 'help' for available commands.");
    println!("Data: {}", db_path.display());

    state.run();
}