beads_rust 0.1.45

Agent-first issue tracker (SQLite + JSONL)
Documentation
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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
//! Upgrade command implementation.
//!
//! Enables br to update itself to the latest version using the `self_update` crate.

use crate::cli::UpgradeArgs;
use crate::error::{BeadsError, Result};
use crate::output::{OutputContext, OutputMode};
use rich_rust::prelude::*;
use self_update::backends::github;
use self_update::cargo_crate_version;
use self_update::update::ReleaseUpdate;
use serde::Serialize;
use std::env;

/// Repo owner for GitHub releases.
const REPO_OWNER: &str = "Dicklesworthstone";

/// Repo name for GitHub releases.
const REPO_NAME: &str = "beads_rust";

/// Binary name.
const BIN_NAME: &str = "br";

/// Update check result.
#[derive(Serialize)]
struct UpdateCheckResult {
    current_version: String,
    latest_version: String,
    update_available: bool,
    #[serde(skip_serializing_if = "Option::is_none")]
    download_url: Option<String>,
}

/// Update result.
#[derive(Serialize)]
struct UpdateResult {
    current_version: String,
    new_version: String,
    updated: bool,
    #[serde(skip_serializing_if = "Option::is_none")]
    message: Option<String>,
}

/// Execute the upgrade command.
///
/// # Errors
///
/// Returns an error if the update check or download fails.
pub fn execute(args: &UpgradeArgs, ctx: &OutputContext) -> Result<()> {
    let current_version = cargo_crate_version!();

    if args.dry_run {
        return execute_dry_run(args, current_version, ctx);
    }

    if args.check {
        return execute_check(current_version, ctx);
    }

    execute_upgrade(args, current_version, ctx)
}

/// Execute check-only mode.
fn execute_check(current_version: &str, ctx: &OutputContext) -> Result<()> {
    tracing::info!("Checking for updates...");

    let updater = build_updater(current_version)?;
    let latest = updater.get_latest_release().map_err(map_update_error)?;
    let latest_version = &latest.version;

    let update_available = version_newer(latest_version, current_version);

    // Get download URL from first asset if available
    let download_url = latest.assets.first().map(|a| a.download_url.clone());

    let result = UpdateCheckResult {
        current_version: current_version.to_string(),
        latest_version: latest_version.clone(),
        update_available,
        download_url,
    };

    if ctx.is_json() {
        ctx.json_pretty(&result);
    } else if ctx.is_quiet() {
    } else if matches!(ctx.mode(), OutputMode::Rich) {
        render_check_rich(&result, ctx);
    } else {
        println!("Current version: {current_version}");
        println!("Latest version:  {latest_version}");

        if update_available {
            println!("\n\u{2191} Update available! Run `br upgrade` to install.");
        } else {
            println!("\n\u{2713} Already up to date");
        }
    }

    Ok(())
}

/// Execute dry-run mode.
fn execute_dry_run(args: &UpgradeArgs, current_version: &str, ctx: &OutputContext) -> Result<()> {
    tracing::info!("Dry-run mode: checking what would happen...");

    let target_version = args.version.as_deref();
    let updater = build_updater(current_version)?;
    let latest = updater.get_latest_release().map_err(map_update_error)?;
    let latest_version = &latest.version;

    let install_version = target_version.unwrap_or(latest_version);
    let would_update = args.force || version_newer(install_version, current_version);

    // Get download URL from first asset if available
    let download_url = latest
        .assets
        .first()
        .map_or_else(|| "N/A".to_string(), |a| a.download_url.clone());

    if ctx.is_json() {
        let result = serde_json::json!({
            "dry_run": true,
            "current_version": current_version,
            "target_version": install_version,
            "would_download": download_url,
            "would_update": would_update,
        });
        ctx.json_pretty(&result);
    } else if ctx.is_quiet() {
    } else if matches!(ctx.mode(), OutputMode::Rich) {
        render_dry_run_rich(
            current_version,
            install_version,
            &download_url,
            would_update,
            ctx,
        );
    } else {
        println!("Dry-run mode (no changes will be made)\n");
        println!("Current version: {current_version}");
        println!("Target version:  {install_version}");
        println!("Would download:  {download_url}");
        println!(
            "Would install:   {}",
            if would_update {
                "yes"
            } else {
                "no (already up to date)"
            }
        );
        println!("\nNo changes made.");
    }

    Ok(())
}

/// Execute the actual upgrade.
fn execute_upgrade(args: &UpgradeArgs, current_version: &str, ctx: &OutputContext) -> Result<()> {
    tracing::info!(current = %current_version, "Starting upgrade...");

    let is_json = ctx.is_json();
    let is_quiet = ctx.is_quiet();
    let is_rich = matches!(ctx.mode(), OutputMode::Rich);

    if !is_json && !is_quiet && !is_rich {
        println!("Checking for updates...");
        println!("Current version: {current_version}");
    } else if is_rich {
        ctx.info(&format!(
            "Checking for updates (current: {current_version})..."
        ));
    }

    let updater = if let Some(ref target_version) = args.version {
        build_updater_with_target(target_version, current_version, !is_json && !is_rich)?
    } else {
        build_updater(current_version)?
    };

    // Get latest release info first
    let latest = updater.get_latest_release().map_err(map_update_error)?;
    let latest_version = &latest.version;

    if !is_json && !is_quiet && !is_rich {
        println!("Latest version:  {latest_version}");
    }

    let update_available = args.force || version_newer(latest_version, current_version);

    if !update_available {
        let result = UpdateResult {
            current_version: current_version.to_string(),
            new_version: latest_version.clone(),
            updated: false,
            message: Some("Already up to date".to_string()),
        };

        if is_json {
            ctx.json_pretty(&result);
        } else if is_quiet {
        } else if is_rich {
            render_up_to_date_rich(current_version, latest_version, ctx);
        } else {
            println!("\n\u{2713} Already up to date");
        }
        return Ok(());
    }

    if !is_json && !is_quiet && !is_rich {
        println!("\nDownloading {latest_version}...");
    } else if is_rich {
        ctx.info(&format!("Downloading {latest_version}..."));
    }

    // Perform the update
    let status = updater.update().map_err(|e| {
        let msg = e.to_string();
        if msg.contains("archive-tar") || msg.contains("ArchiveNotEnabled") || msg.contains("tar") {
            BeadsError::Other(anyhow::anyhow!(
                "{msg}\n\n\
                 This binary was built without archive support for the required format (e.g., .tar.gz).\n\
                 This is a known issue in some older versions (e.g., 0.1.21 - 0.1.26). Version 0.1.27 and later include the correct 'archive-tar' linkage.\n\n\
                 Please upgrade manually by running:\n\n  \
                 curl -fsSL https://raw.githubusercontent.com/Dicklesworthstone/beads_rust/main/install.sh | bash\n\n\
                 Or by downloading the release from:\n  \
                 https://github.com/Dicklesworthstone/beads_rust/releases\n\n\
                 After that, `br upgrade` will work correctly for future updates."
            ))
        } else {
            map_update_error(e)
        }
    })?;

    let result = UpdateResult {
        current_version: current_version.to_string(),
        new_version: status.version().to_string(),
        updated: status.updated(),
        message: if status.updated() {
            Some(format!("Updated to {}", status.version()))
        } else {
            Some("No update performed".to_string())
        },
    };

    if is_json {
        ctx.json_pretty(&result);
    } else if is_quiet {
    } else if is_rich {
        render_upgrade_result_rich(&result, current_version, ctx);
    } else if status.updated() {
        println!(
            "\n\u{2713} Updated br from {current_version} to {}",
            status.version()
        );
    } else {
        println!("\n\u{2713} Already up to date");
    }

    Ok(())
}

/// Resolve a GitHub auth token from environment variables.
///
/// Checks `GITHUB_TOKEN` first, then `GH_TOKEN`. Returns `None` if neither
/// is set or if the value is empty.
fn resolve_auth_token() -> Option<String> {
    env::var("GITHUB_TOKEN")
        .or_else(|_| env::var("GH_TOKEN"))
        .ok()
        .map(|t| t.trim().to_string())
        .filter(|t| !t.is_empty())
}

/// Map the Rust target triple to the asset name fragment used in GitHub releases.
///
/// Release assets follow the pattern `br-v{VERSION}-{platform}_{arch}.tar.gz`
/// (e.g. `darwin_amd64`, `linux_arm64`), which differs from the Rust target
/// triple that `self_update` uses by default (e.g. `x86_64-apple-darwin`).
fn asset_target_name() -> &'static str {
    match self_update::get_target() {
        "x86_64-apple-darwin" => "darwin_amd64",
        "aarch64-apple-darwin" => "darwin_arm64",
        "x86_64-unknown-linux-gnu" => "linux_amd64",
        "x86_64-unknown-linux-musl" => "linux_musl_amd64",
        "aarch64-unknown-linux-gnu" => "linux_arm64",
        "aarch64-unknown-linux-musl" => "linux_musl_arm64",
        "x86_64-pc-windows-msvc" | "x86_64-pc-windows-gnu" => "windows_amd64",
        other => other, // fall back to the raw triple for unknown targets
    }
}

/// Build the self-update updater.
fn build_updater(current_version: &str) -> Result<Box<dyn ReleaseUpdate>> {
    let mut builder = github::Update::configure();
    builder
        .repo_owner(REPO_OWNER)
        .repo_name(REPO_NAME)
        .bin_name(BIN_NAME)
        .target(asset_target_name())
        .show_download_progress(true)
        .no_confirm(true)
        .current_version(current_version);

    if let Some(token) = resolve_auth_token() {
        tracing::debug!("Using GitHub auth token from environment");
        builder.auth_token(&token);
    }

    builder.build().map_err(map_update_error)
}

/// Build updater with a specific target version.
fn build_updater_with_target(
    target_version: &str,
    current_version: &str,
    show_progress: bool,
) -> Result<Box<dyn ReleaseUpdate>> {
    let mut builder = github::Update::configure();
    builder
        .repo_owner(REPO_OWNER)
        .repo_name(REPO_NAME)
        .bin_name(BIN_NAME)
        .target(asset_target_name())
        .show_download_progress(show_progress)
        .no_confirm(true)
        .current_version(current_version)
        .target_version_tag(target_version);

    if let Some(token) = resolve_auth_token() {
        tracing::debug!("Using GitHub auth token from environment");
        builder.auth_token(&token);
    }

    builder.build().map_err(map_update_error)
}

/// Map `self_update` errors to `BeadsError`.
fn map_update_error<E: std::error::Error + Send + Sync + 'static>(err: E) -> BeadsError {
    BeadsError::Other(anyhow::Error::from(err))
}

/// Compare versions to check if new is greater than current.
///
/// Handles semver-like versions (e.g., "0.2.0" > "0.1.0", "0.10.0" > "0.9.0").
fn version_newer(new: &str, current: &str) -> bool {
    let parse_version = |v: &str| -> Vec<u32> {
        v.trim_start_matches('v')
            .split('.')
            .filter_map(|s| s.parse().ok())
            .collect()
    };

    let new_parts = parse_version(new);
    let current_parts = parse_version(current);

    for (n, c) in new_parts.iter().zip(current_parts.iter()) {
        match n.cmp(c) {
            std::cmp::Ordering::Greater => return true,
            std::cmp::Ordering::Less => return false,
            std::cmp::Ordering::Equal => {} // Continue to next part
        }
    }

    // If all compared parts are equal, the one with more parts is newer
    new_parts.len() > current_parts.len()
}

// ─────────────────────────────────────────────────────────────
// Rich Output Rendering
// ─────────────────────────────────────────────────────────────

/// Render update check results with rich formatting.
fn render_check_rich(result: &UpdateCheckResult, ctx: &OutputContext) {
    let console = Console::default();
    let theme = ctx.theme();
    let width = ctx.width();

    let mut content = Text::new("");

    // Version comparison
    content.append_styled("Current version:  ", theme.dimmed.clone());
    content.append_styled(&result.current_version, theme.emphasis.clone());
    content.append("\n");

    content.append_styled("Latest version:   ", theme.dimmed.clone());
    if result.update_available {
        content.append_styled(&result.latest_version, theme.success.clone());
        content.append_styled(" ✓ NEW", theme.success.clone());
    } else {
        content.append_styled(&result.latest_version, theme.emphasis.clone());
    }
    content.append("\n\n");

    // Status message
    if result.update_available {
        content.append_styled("", theme.success.clone());
        content.append_styled("Update available! ", theme.success.clone());
        content.append("Run ");
        content.append_styled("`br upgrade`", theme.accent.clone());
        content.append(" to install.\n");
    } else {
        content.append_styled("", theme.success.clone());
        content.append("Already up to date\n");
    }

    let panel = Panel::from_rich_text(&content, width)
        .title(Text::styled("Upgrade Check", theme.panel_title.clone()))
        .box_style(theme.box_style);

    console.print_renderable(&panel);
}

/// Render dry-run results with rich formatting.
fn render_dry_run_rich(
    current_version: &str,
    target_version: &str,
    download_url: &str,
    would_update: bool,
    ctx: &OutputContext,
) {
    let console = Console::default();
    let theme = ctx.theme();
    let width = ctx.width();

    let mut content = Text::new("");

    content.append_styled("⚡ Dry-run mode ", theme.warning.clone());
    content.append_styled("(no changes will be made)\n\n", theme.dimmed.clone());

    content.append_styled("Current version:  ", theme.dimmed.clone());
    content.append_styled(current_version, theme.emphasis.clone());
    content.append("\n");

    content.append_styled("Target version:   ", theme.dimmed.clone());
    content.append_styled(target_version, theme.emphasis.clone());
    content.append("\n");

    content.append_styled("Would download:   ", theme.dimmed.clone());
    content.append_styled(download_url, theme.accent.clone());
    content.append("\n");

    content.append_styled("Would install:    ", theme.dimmed.clone());
    if would_update {
        content.append_styled("yes", theme.success.clone());
    } else {
        content.append_styled("no (already up to date)", theme.warning.clone());
    }
    content.append("\n\n");

    content.append_styled("No changes made.", theme.dimmed.clone());
    content.append("\n");

    let panel = Panel::from_rich_text(&content, width)
        .title(Text::styled("Upgrade Dry Run", theme.panel_title.clone()))
        .box_style(theme.box_style);

    console.print_renderable(&panel);
}

/// Render "already up to date" message with rich formatting.
fn render_up_to_date_rich(current_version: &str, latest_version: &str, ctx: &OutputContext) {
    let console = Console::default();
    let theme = ctx.theme();
    let width = ctx.width();

    let mut content = Text::new("");

    content.append_styled("Current version:  ", theme.dimmed.clone());
    content.append_styled(current_version, theme.emphasis.clone());
    content.append("\n");

    content.append_styled("Latest version:   ", theme.dimmed.clone());
    content.append_styled(latest_version, theme.emphasis.clone());
    content.append("\n\n");

    content.append_styled("", theme.success.clone());
    content.append("Already up to date\n");

    let panel = Panel::from_rich_text(&content, width)
        .title(Text::styled("Upgrade Status", theme.panel_title.clone()))
        .box_style(theme.box_style);

    console.print_renderable(&panel);
}

/// Render upgrade result with rich formatting.
fn render_upgrade_result_rich(result: &UpdateResult, current_version: &str, ctx: &OutputContext) {
    let console = Console::default();
    let theme = ctx.theme();
    let width = ctx.width();

    let mut content = Text::new("");
    content.append_styled("", theme.success.clone());

    if result.updated {
        content.append_styled("Upgraded ", theme.success.clone());
        content.append("br from ");
        content.append_styled(current_version, theme.dimmed.clone());
        content.append(" to ");
        content.append_styled(&result.new_version, theme.success.clone());
        content.append("\n");
    } else {
        content.append("Already up to date\n");
    }

    let panel = Panel::from_rich_text(&content, width)
        .title(Text::styled("Upgrade Complete", theme.panel_title.clone()))
        .box_style(theme.box_style);

    console.print_renderable(&panel);
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_version_comparison_basic() {
        assert!(version_newer("0.2.0", "0.1.0"));
        assert!(version_newer("1.0.0", "0.9.0"));
        assert!(version_newer("0.1.1", "0.1.0"));
    }

    #[test]
    fn test_version_comparison_double_digits() {
        assert!(version_newer("0.10.0", "0.9.0"));
        assert!(version_newer("0.10.0", "0.2.0"));
        assert!(version_newer("1.0.0", "0.99.99"));
    }

    #[test]
    fn test_version_comparison_equal() {
        assert!(!version_newer("0.1.0", "0.1.0"));
        assert!(!version_newer("1.0.0", "1.0.0"));
    }

    #[test]
    fn test_version_comparison_older() {
        assert!(!version_newer("0.1.0", "0.2.0"));
        assert!(!version_newer("0.9.0", "1.0.0"));
    }

    #[test]
    fn test_version_with_v_prefix() {
        assert!(version_newer("v0.2.0", "v0.1.0"));
        assert!(version_newer("v0.2.0", "0.1.0"));
        assert!(version_newer("0.2.0", "v0.1.0"));
    }

    #[test]
    fn test_version_more_parts() {
        assert!(version_newer("0.1.0.1", "0.1.0"));
        assert!(!version_newer("0.1.0", "0.1.0.1"));
    }
}