summaryrefslogtreecommitdiffstats
path: root/src/main.rs
blob: eb62d524ac067b67080531e4ce878ca3da748b25 (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
54
55
56
57
58
59
60
61
62
use std::io::ErrorKind;

use clap::Parser;

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

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

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

    config.history_file = config.history_file.or_else(History::default_path);

    let history = match config.history_file {
        Some(path) => History::open(path),
        None => Err(std::io::Error::from(ErrorKind::NotFound)),
    };

    let tmux = Tmux::new(config.socket_name);

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

    match tmux.host() {
        Ok(p) => sessions.add(p),
        Err(err) => tracing::warn!(?err, "Failed to get tmux host"),
    };

    match tmux.list(None) {
        Ok(p) => sessions.extend(p),
        Err(err) => tracing::warn!(?err, "Failed to list tmux sessions"),
    };

    match history {
        Ok(p) => {
            sessions.extend(p.entries);

            if config.update {
                sessions.write_into(p.file)?;
            }
        }
        Err(err) => tracing::warn!(?err, "Failed to open history file"),
    };

    match KnownHosts::open() {
        Ok(p) => sessions.extend(p),
        Err(err) => tracing::warn!(?err, "Failed to read KnownHost file"),
    };

    match Hosts::open() {
        Ok(p) => sessions.extend(p),
        Err(err) => tracing::warn!(?err, "Failed to read /etc/hosts"),
    };

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

    Ok(())
}