govmr 2.2.0

A high-performance, asynchronous Go Version Manager with a slick terminal UI.
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
//! Module actions - Execution of queued [`Action`]s against the [`GoManager`].
//!
//! Network-bound work (refresh, install) is spawned as a background task that
//! reports back through the shared channel, keeping the render loop fluid;
//! quick local operations (switch, delete, path-fix) run inline and post a
//! follow-up action where a refresh of the UI is needed.

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;

/// Drains and processes all queued background actions for one event-loop tick.
///
/// Each arm only manages the busy state of its *own* operation. Long-running
/// tasks are spawned and report back via the MPSC channel to keep the UI fluid.
///
/// # Errors
/// Always returns `Ok(())` today; individual action failures are reported to
/// the user through the status bar instead of aborting the loop.
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) => {
                // Route update errors to their own handler without adding a new enum variant.
                // This keeps cargo-semver-checks happy and prevents a v2.0.0 bump!
                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(())
}

// -------------------------------------- Internal Helpers -------------------------------------- //

/// Spawns the background manifest fetch so the render loop stays fluid.
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();

    // Spawn a background task to fetch versions without blocking the render loop
    tokio::spawn(async move {
        let result = mgr.fetch_versions().await;
        let _ = tx.send(Action::RefreshDone(result.map_err(|e| e.to_string())));
    });
}

/// Applies a finished manifest refresh to the UI state.
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);
        }
    }
}

/// Marks the install busy state and spawns the cancellable download task.
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 {
                // Wait for the cancel signal to become true
                while !*cancel_rx.borrow() {
                    if cancel_rx.changed().await.is_err() {
                        // Sender dropped without requesting a cancel: wait
                        // forever so only an explicit `true` can win the race.
                        std::future::pending::<()>().await;
                    }
                }
            } => {
                // Not logged here: `handle_install_failed` writes the one
                // `install: canceled` line, with the version attached.
                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()));
            }
        }
    });
}

/// Auto-activates a freshly installed version and posts a follow-up refresh.
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;

    // AUTO-ACTIVATE: Switch to the newly installed version immediately.
    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;
            }
            // The success half of the auto-activation used to live only in the
            // status bar, which is gone by the time anyone reads the log.
            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);

    // Refresh to pick up any manifest changes or updated installed flags
    let _ = action_tx.send(Action::Refresh);
}

/// Clears the install busy state and reports a failed installation.
///
/// This is the *only* place an install failure is logged. It extracts the
/// target version from the `BusyState` before clearing it, avoiding the need
/// to pass the version through the `Action` enum variant.
fn handle_install_failed(app: &mut App, message: &str) {
    // Extract version BEFORE clearing the busy state!
    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);
    }
}

/// Switches the active toolchain and updates local UI state immediately.
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,
            );

            // Update local UI state instantly without a network round-trip
            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);
        }
    }
    // Clear the busy state directly instead of waiting for RefreshDone
    app.state.busy = None;
}

/// Deletes an installed toolchain and queues a manifest refresh.
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(()) => {
            // The freed-space figure is logged by `delete_version`, which is the
            // only layer that saw the directory before it was removed.
            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);
}

/// Runs the permanent PATH fix and shows the summary in the help overlay.
fn handle_fix_path(app: &mut App, manager: &Arc<GoManager>) {
    // Runs the platform's permanent PATH snippet in a hidden child
    // process; fast and local, so it runs inline. The summary is
    // shown inside the still-open help overlay, not on the dashboard
    // status bar.
    match manager.fix_path_permanently() {
        Ok(lines) => {
            // lines is Vec<String> now
            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) => {
            // Sole owner of the fix-path failure line: the help overlay's `f` key
            // dispatches `Action::FixPath` too, so exactly one place can report it.
            logging::error(&format!("fix-path: failed error=\"{e}\""));
            app.set_status(format!("Could not fix PATH: {e}"), MsgKind::Error);
        }
    }
}

/// Spawns the background self-update check and download, supporting cancellation.
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); // Reuse the cancel channel

    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() {
                        // Sender dropped without requesting a cancel: wait
                        // forever so only an explicit `true` can win the race.
                        std::future::pending::<()>().await;
                    }
                }
            } => {
                let _ = tx.send(Action::InstallFailed("[UPDATE]latest|||cancelled".to_string()));
                Err(crate::errors::GovmError::Cancelled)
            }
        };
    });
}

/// Reports a failed self-update under its own `update:` prefix.
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);
    }
}

/// Whether a failure message is really the user pressing cancel.
fn is_user_cancel(message: &str) -> bool {
    message.to_lowercase().contains("cancelled")
}

/// Appends a `hint="..."` field to a failure line when the message matches a
/// known, actionable cause.
///
/// Hints live in the log only: the status bar stays short enough to read in one
/// glance, while whoever later greps the file gets the "so what do I do" half.
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}\"")
}