summaryrefslogtreecommitdiffstats
path: root/src/main.rs
blob: 763194cd6bca004412d9b852acfb3d4f61863014 (plain)
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
use std::collections::HashMap;

use clap::Parser;

use sshr::{Config, History, KnownHosts, Session, SessionSource, Tmux};

fn main() -> anyhow::Result<()> {
    let config = Config::parse();

    tracing_subscriber::fmt::fmt()
        .with_max_level(&config.verbosity)
        .without_time()
        .init();

    if config.list {
        list_sessions(config)
    } else {
        switch(config.target)
    }
}

fn list_sessions(config: Config) -> anyhow::Result<()> {
    let mut sessions = HashMap::new();

    sessions = KnownHosts::default().update(sessions)?;
    sessions = Tmux::new(config.socket_name).update(sessions)?;

    if let Some(history_file) = config.history_file.or_else(History::default_path) {
        if history_file.exists() {
            sessions = History::new(history_file).update(sessions)?;
        }
    }

    let mut sessions: Vec<Session> = sessions.into_values().collect();

    sessions.sort();

    for session in sessions {
        println!("{session}");
    }

    Ok(())
}

fn switch(_target: Option<String>) -> anyhow::Result<()> {
    todo!()
}