summaryrefslogtreecommitdiffstats
path: root/src/main.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs32
1 files changed, 21 insertions, 11 deletions
diff --git a/src/main.rs b/src/main.rs
index 763194c..b546377 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -2,20 +2,21 @@ use std::collections::HashMap;
use clap::Parser;
-use sshr::{Config, History, KnownHosts, Session, SessionSource, Tmux};
+use sshr::{Commands, Config, History, KnownHosts, Session, SessionSource, Tmux};
fn main() -> anyhow::Result<()> {
- let config = Config::parse();
+ let mut 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)
+ config.history_file = config.history_file.or_else(History::default_path);
+
+ match config.command.to_owned() {
+ Commands::List => list_sessions(config),
+ Commands::Switch { name } => switch(config, name),
}
}
@@ -25,9 +26,9 @@ fn list_sessions(config: Config) -> anyhow::Result<()> {
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)?;
+ if let Some(history) = config.history_file.map(History::new) {
+ if history.exists() {
+ sessions = history.update(sessions)?;
}
}
@@ -42,6 +43,15 @@ fn list_sessions(config: Config) -> anyhow::Result<()> {
Ok(())
}
-fn switch(_target: Option<String>) -> anyhow::Result<()> {
- todo!()
+fn switch(config: Config, name: String) -> anyhow::Result<()> {
+ let tmux = Tmux::new(config.socket_name);
+
+ if let Some(history) = config.history_file.map(History::new) {
+ if tmux.switch(&name)?.success() {
+ let session = tmux.show(&name)?;
+ history.update_session(session)?;
+ }
+ }
+
+ Ok(())
}