use crate::{
app::{Action, App, BusyState, MsgKind, Phase},
logging,
manager::GoManager,
version::GoVersion,
};
use ratatui::text::{Line, Span};
use std::sync::Arc;
use tokio::sync::mpsc;
pub(crate) async fn handle_actions(
action_rx: &mut mpsc::UnboundedReceiver<Action>,
app: &mut App,
manager: &Arc<GoManager>,
action_tx: &mpsc::UnboundedSender<Action>,
) -> anyhow::Result<()> {
while let Ok(action) = action_rx.try_recv() {
match action {
Action::Refresh => spawn_refresh(app, manager, action_tx),
Action::RefreshDone(result) => handle_refresh_done(app, result),
Action::Install(v) => start_install(app, manager, action_tx, v),
Action::InstallProgress(p) => app.update_install_progress(p),
Action::InstallDone(v) => handle_install_done(app, manager, action_tx, &v),
Action::InstallFailed(err) => {
if let Some(rest) = err.strip_prefix("[UPDATE]") {
let (target, message) = rest.split_once("|||").unwrap_or(("unknown", rest));
handle_update_failed(app, target, message);
} else {
handle_install_failed(app, &err);
}
}
Action::Use(v) => handle_use(app, manager, &v),
Action::Delete(v) => handle_delete(app, manager, action_tx, &v),
Action::FixPath => handle_fix_path(app, manager),
Action::Update => spawn_update(app, manager, action_tx),
Action::UpdateStarted(ver) => {
if let Some(BusyState::Updating { version, .. }) = &mut app.state.busy {
*version = ver;
}
}
Action::UpdateProgress(p) => app.update_update_progress(p),
Action::Uninstall(purge) => handle_uninstall(app, manager, purge),
Action::UninstallBinaryOnly => handle_uninstall(app, manager, false),
Action::UpdateDone(msg) => {
app.state.busy = None;
app.state.cancel_install = None;
app.set_status(msg, MsgKind::Success);
}
}
}
Ok(())
}
fn spawn_refresh(
app: &mut App,
manager: &Arc<GoManager>,
action_tx: &mpsc::UnboundedSender<Action>,
) {
app.state.busy = Some(BusyState::Refreshing);
let mgr = manager.clone();
let tx = action_tx.clone();
tokio::spawn(async move {
let result = mgr.fetch_versions().await;
let _ = tx.send(Action::RefreshDone(result.map_err(|e| e.to_string())));
});
}
fn handle_refresh_done(app: &mut App, result: Result<Vec<GoVersion>, String>) {
app.state.busy = None;
match result {
Ok(versions) => {
app.state.versions = versions;
app.state.status_message = None;
app.clamp_selection();
}
Err(e) => {
app.set_status(e, MsgKind::Error);
}
}
}
fn start_install(
app: &mut App,
manager: &Arc<GoManager>,
action_tx: &mpsc::UnboundedSender<Action>,
v: GoVersion,
) {
app.state.busy = Some(BusyState::Installing {
version: v.raw_version.clone(),
phase: Phase::Downloading,
downloaded: 0,
total: v.size,
speed: 0.0,
started_at: std::time::Instant::now(),
});
app.state.status_message = None;
let mgr = manager.clone();
let progress_tx = action_tx.clone();
let done_tx = action_tx.clone();
let (cancel_tx, mut cancel_rx) = tokio::sync::watch::channel(false);
app.state.cancel_install = Some(cancel_tx);
tokio::spawn(async move {
let progress_tx2 = progress_tx.clone();
let result = tokio::select! {
res = mgr.download_and_install(&v, move |p| {
let _ = progress_tx2.send(Action::InstallProgress(p));
}) => res,
() = async {
while !*cancel_rx.borrow() {
if cancel_rx.changed().await.is_err() {
std::future::pending::<()>().await;
}
}
} => {
Err(crate::errors::GovmError::Cancelled)
}
};
match result {
Ok(_) => {
let _ = done_tx.send(Action::InstallDone(v));
}
Err(e) => {
let _ = done_tx.send(Action::InstallFailed(e.to_string()));
}
}
});
}
fn handle_install_done(
app: &mut App,
manager: &Arc<GoManager>,
action_tx: &mpsc::UnboundedSender<Action>,
v: &GoVersion,
) {
app.state.busy = None;
app.state.cancel_install = None;
let activated = match manager.switch_version(v) {
Ok(in_path) => {
app.state.is_shim_in_path = in_path;
for ver in &mut app.state.versions {
ver.active = ver.raw_version == v.raw_version;
}
logging::info(&format!(
"post-install: auto-activated version={} shim_in_path={in_path}",
v.raw_version
));
true
}
Err(e) => {
logging::error(&format!(
"post-install: auto-activate failed version={} error=\"{e}\" note=install_succeeded",
v.raw_version
));
false
}
};
let msg = if activated {
format!(
"Go {} installed & activated ✓ (archive cleaned to save space)",
v.raw_version
)
} else {
format!(
"Go {} installed ✓ — press u to activate (archive cleaned to save space)",
v.raw_version
)
};
app.set_status(msg, MsgKind::Success);
let _ = action_tx.send(Action::Refresh);
}
fn handle_install_failed(app: &mut App, message: &str) {
let version = app
.state
.busy
.as_ref()
.and_then(BusyState::target)
.unwrap_or("unknown")
.to_string();
app.state.busy = None;
app.state.cancel_install = None;
if is_user_cancel(message) {
logging::info(&format!(
"install: cancelled version={version} note=user_abort"
));
app.set_status("Installation cancelled", MsgKind::Info);
} else {
logging::error(&format!(
"install: failed version={version} error=\"{message}\"{}",
failure_hint(message)
));
app.set_status(format!("Installation failed: {message}"), MsgKind::Error);
}
}
fn handle_use(app: &mut App, manager: &Arc<GoManager>, v: &GoVersion) {
app.state.busy = Some(BusyState::Switching(v.raw_version.clone()));
match manager.switch_version(v) {
Ok(in_path) => {
app.set_status(
format!("Switched to Go {}", v.raw_version),
MsgKind::Success,
);
app.state.is_shim_in_path = in_path;
for ver in &mut app.state.versions {
ver.active = ver.raw_version == v.raw_version;
}
}
Err(e) => {
logging::error(&format!(
"use: failed version={} error=\"{e}\"{}",
v.raw_version,
failure_hint(&e.to_string())
));
app.set_status(e.to_string(), MsgKind::Error);
}
}
app.state.busy = None;
}
fn handle_delete(
app: &mut App,
manager: &Arc<GoManager>,
action_tx: &mpsc::UnboundedSender<Action>,
v: &GoVersion,
) {
app.state.busy = Some(BusyState::Deleting(v.raw_version.clone()));
match manager.delete_version(v) {
Ok(()) => {
app.set_status(format!("Deleted Go {}", v.raw_version), MsgKind::Success);
}
Err(e) => {
logging::error(&format!(
"delete: failed version={} error=\"{e}\"{}",
v.raw_version,
failure_hint(&e.to_string())
));
app.set_status(e.to_string(), MsgKind::Error);
}
}
let _ = action_tx.send(Action::Refresh);
}
fn handle_fix_path(app: &mut App, manager: &Arc<GoManager>) {
match manager.fix_path_permanently() {
Ok(lines) => {
let styled_lines: Vec<Line<'static>> = lines
.into_iter()
.enumerate()
.map(|(i, line)| {
if i == 0 {
Line::from(Span::styled(line, app.state.theme.success()))
} else if i == 1 && line.starts_with(" ") {
Line::from(Span::styled(line, app.state.theme.brand_bold()))
} else {
Line::from(Span::styled(line, app.state.theme.muted()))
}
})
.collect();
app.state.path_fix_notice = Some(styled_lines);
app.state.is_shim_in_path = manager.get_shim_manager().is_in_path();
}
Err(e) => {
logging::error(&format!("fix-path: failed error=\"{e}\""));
app.set_status(format!("Could not fix PATH: {e}"), MsgKind::Error);
}
}
}
fn spawn_update(
app: &mut App,
manager: &Arc<GoManager>,
action_tx: &mpsc::UnboundedSender<Action>,
) {
app.state.busy = Some(BusyState::Updating {
version: "latest".to_string(),
phase: Phase::Downloading,
downloaded: 0,
total: 0,
speed: 0.0,
started_at: std::time::Instant::now(),
});
app.state.status_message = None;
let mgr = manager.clone();
let tx = action_tx.clone();
let (cancel_tx, mut cancel_rx) = tokio::sync::watch::channel(false);
app.state.cancel_install = Some(cancel_tx);
tokio::spawn(async move {
let _ = tokio::select! {
res = async {
match mgr.check_for_update().await {
Ok(Some(ver)) => {
let _ = tx.send(Action::UpdateStarted(ver.clone()));
let progress_tx = tx.clone();
if let Err(e) = mgr.perform_update(&ver, move |p| {
let _ = progress_tx.send(Action::UpdateProgress(p));
}).await {
let _ = tx.send(Action::InstallFailed(format!("[UPDATE]{ver}|||{e}")));
} else {
let _ = tx.send(Action::UpdateDone(format!("Updated to v{ver}! Restart govmr to run it.")));
}
Ok(())
}
Ok(None) => {
let _ = tx.send(Action::UpdateDone("You are already on the latest version.".to_string()));
Ok(())
}
Err(e) => {
let _ = tx.send(Action::InstallFailed(format!("[UPDATE]unknown|||{e}")));
Err(e)
}
}
} => res,
() = async {
while !*cancel_rx.borrow() {
if cancel_rx.changed().await.is_err() {
std::future::pending::<()>().await;
}
}
} => {
let _ = tx.send(Action::InstallFailed("[UPDATE]latest|||cancelled".to_string()));
Err(crate::errors::GovmError::Cancelled)
}
};
});
}
fn handle_update_failed(app: &mut App, target: &str, message: &str) {
app.state.busy = None;
app.state.cancel_install = None;
if is_user_cancel(message) {
logging::info(&format!(
"update: cancelled target={target} note=user_abort"
));
app.set_status("Update cancelled", MsgKind::Info);
} else {
logging::error(&format!(
"update: failed target={target} error=\"{message}\"{}",
failure_hint(message)
));
app.set_status(format!("Update failed: {message}"), MsgKind::Error);
}
}
fn handle_uninstall(app: &mut App, manager: &Arc<GoManager>, purge: bool) {
if let Err(e) = manager.uninstall(purge) {
app.set_status(format!("Uninstall failed: {e}"), MsgKind::Error);
} else {
let msg = if purge {
"govmr uninstalled and ~/.govmr purged. Press q to exit."
} else {
"govmr binary removed. ~/.govmr kept intact. Press q to exit."
};
app.set_status(msg, MsgKind::Success);
}
}
fn is_user_cancel(message: &str) -> bool {
message.to_lowercase().contains("cancelled")
}
fn failure_hint(message: &str) -> String {
let lower = message.to_lowercase();
let hint = if lower.contains("text file busy") || lower.contains("os error 26") {
"the running binary is locked; quit other govmr instances and retry (or replace the executable manually)"
} else if lower.contains("is not a tar.gz archive") || lower.contains("is not a zip archive") {
"server sent a non-archive payload; check proxy/captive portal, or whether the release file exists"
} else if lower.contains("permission denied") {
"~/.govmr is not writable by the current user"
} else {
return String::new();
};
format!(" hint=\"{hint}\"")
}