summaryrefslogtreecommitdiffstats
path: root/src/main.rs
blob: 523ee828b6eb45df33c99e99495a05b75a7fd9e7 (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
48
49
50
51
52
53
use anyhow::Context;
use clap::Parser;

use sshr::{Config, HostName, Hosts, KnownHosts, Sessions, Stdout};

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

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

    let mut sessions = Sessions::new(config.sessions);

    if config.enabled.localhost() {
        let hostname = HostName::get().context("Failed to get hostname of localhost")?;
        sessions.extend(hostname);
    }

    if config.enabled.tmux() {
        let tmux_sessions = config.tmux.list().context("Failed to list tmux sessions")?;
        sessions.extend(tmux_sessions);
    }

    if config.enabled.ssh() {
        let known_hosts = KnownHosts::open().context("Failed to read KnownHost file")?;
        sessions.extend(known_hosts);
    }

    if config.enabled.hosts() {
        let hosts = Hosts::open().context("Failed to read /etc/hosts")?;
        sessions.extend(hosts);
    }

    if config.enabled.history() {
        match config.history.read() {
            Ok(h) => sessions.extend(h),
            Err(err) if err.kind() == std::io::ErrorKind::NotFound => {
                tracing::warn!("Skipping non-existant history file")
            }
            Err(err) => return Err(err).context("Failed to read history file"),
        }
    }

    if config.history.update {
        sessions.write_sessions(config.history)?;
    }

    sessions
        .write_sessions(Stdout::new(config.stdio))
        .context("Failed to write to stdout")
}