summaryrefslogtreecommitdiffstats
path: root/src/main.rs
blob: 9eaa97818b21ed3cc326b27cadf764c409fcbd73 (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
use anyhow::Context;
use clap::Parser;

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

fn main() -> anyhow::Result<()> {
    let mut 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() {
        if config.enabled.tmux() {
            let local_sessions = Tmux::local_session().context("Failed to get local sessions")?;
            sessions.extend(local_sessions);
        } else {
            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")?;
        if config.tmux.exclude_attached {
            if let Ok(session) = config.tmux.attached() {
                config.stdio.exclude.push(session.name)
            }
        }
        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);
    }

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