Files
tmuxido/src/main.rs
T
cinco 0f27bedc94 feat: periodic update check on startup
On startup, if `update_check_interval_hours` have elapsed since the last
check, fetches the latest release tag from the Gitea API and prints a
notice when a newer version is available. Silent on network failure or
no update found.

- New `update_check` module with injected fetcher for full testability
- Cache at ~/.cache/tmuxido/update_check.json tracks timestamp + version
- `fetch_latest_tag` and `version_compare` promoted to `pub(crate)`
- 6 unit tests covering disabled, interval not elapsed, fetch triggered,
  equal versions, update available, and current-newer edge case
2026-03-01 04:07:38 -03:00

115 lines
2.8 KiB
Rust

use anyhow::{Context, Result};
use clap::Parser;
use std::io::Write;
use std::path::PathBuf;
use std::process::{Command, Stdio};
use tmuxido::config::Config;
use tmuxido::deps::ensure_dependencies;
use tmuxido::self_update;
use tmuxido::update_check;
use tmuxido::{get_projects, launch_tmux_session, show_cache_status};
#[derive(Parser, Debug)]
#[command(
name = "tmuxido",
about = "Quickly find and open projects in tmux",
version
)]
struct Args {
/// Project path to open directly (skips selection)
project_path: Option<PathBuf>,
/// Force refresh the project cache
#[arg(short, long)]
refresh: bool,
/// Show cache status and exit
#[arg(long)]
cache_status: bool,
/// Update tmuxido to the latest version
#[arg(long)]
update: bool,
}
fn main() -> Result<()> {
let args = Args::parse();
// Handle self-update before anything else
if args.update {
return self_update::self_update();
}
// Check that fzf and tmux are installed; offer to install if missing
ensure_dependencies()?;
// Ensure config exists
Config::ensure_config_exists()?;
// Load config
let config = Config::load()?;
// Periodic update check (silent on failure or no update)
update_check::check_and_notify(&config);
// Handle cache status command
if args.cache_status {
show_cache_status(&config)?;
return Ok(());
}
let selected = if let Some(path) = args.project_path {
path
} else {
// Get projects (from cache or scan)
let projects = get_projects(&config, args.refresh)?;
if projects.is_empty() {
eprintln!("No projects found in configured paths");
std::process::exit(1);
}
// Use fzf to select a project
select_project_with_fzf(&projects)?
};
if !selected.exists() {
eprintln!("Selected path does not exist: {}", selected.display());
std::process::exit(1);
}
// Launch tmux session
launch_tmux_session(&selected, &config)?;
Ok(())
}
fn select_project_with_fzf(projects: &[PathBuf]) -> Result<PathBuf> {
let mut child = Command::new("fzf")
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.spawn()
.context("Failed to spawn fzf. Make sure fzf is installed.")?;
{
let stdin = child.stdin.as_mut().context("Failed to open stdin")?;
for project in projects {
writeln!(stdin, "{}", project.display())?;
}
}
let output = child.wait_with_output()?;
if !output.status.success() {
std::process::exit(0);
}
let selected = String::from_utf8(output.stdout)?.trim().to_string();
if selected.is_empty() {
std::process::exit(0);
}
Ok(PathBuf::from(selected))
}