debugger-cli 0.1.3

LLM-friendly debugger CLI using the Debug Adapter Protocol
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
//! Core installation traits and logic
//!
//! Defines the Installer trait and common installation utilities.

use super::registry::{DebuggerInfo, Platform};
use super::verifier::VerifyResult;
use crate::common::{Error, Result};
use async_trait::async_trait;
use futures_util::StreamExt;
use indicatif::{ProgressBar, ProgressStyle};
use std::path::{Path, PathBuf};

/// Installation status of a debugger
#[derive(Debug, Clone)]
pub enum InstallStatus {
    /// Not installed
    NotInstalled,
    /// Installed at path, with optional version
    Installed {
        path: PathBuf,
        version: Option<String>,
    },
    /// Installed but not working
    Broken { path: PathBuf, reason: String },
}

/// Installation method for a debugger
#[derive(Debug, Clone)]
pub enum InstallMethod {
    /// Use system package manager
    PackageManager {
        manager: PackageManager,
        package: String,
    },
    /// Download from GitHub releases
    GitHubRelease {
        repo: String,
        asset_pattern: String,
    },
    /// Download from direct URL
    DirectDownload { url: String },
    /// Use language-specific package manager
    LanguagePackage { tool: String, package: String },
    /// Extract from VS Code extension
    VsCodeExtension { extension_id: String },
    /// Already available in PATH
    AlreadyInstalled { path: PathBuf },
    /// Cannot install on this platform
    NotSupported { reason: String },
}

/// Package managers
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PackageManager {
    // Linux
    Apt,
    Dnf,
    Pacman,
    // macOS
    Homebrew,
    // Windows
    Winget,
    Scoop,
    // Cross-platform
    Cargo,
    Pip,
    Go,
}

impl PackageManager {
    /// Detect available package managers
    pub fn detect() -> Vec<PackageManager> {
        let mut found = Vec::new();

        if which::which("apt").is_ok() {
            found.push(PackageManager::Apt);
        }
        if which::which("dnf").is_ok() {
            found.push(PackageManager::Dnf);
        }
        if which::which("pacman").is_ok() {
            found.push(PackageManager::Pacman);
        }
        if which::which("brew").is_ok() {
            found.push(PackageManager::Homebrew);
        }
        if which::which("winget").is_ok() {
            found.push(PackageManager::Winget);
        }
        if which::which("scoop").is_ok() {
            found.push(PackageManager::Scoop);
        }
        if which::which("cargo").is_ok() {
            found.push(PackageManager::Cargo);
        }
        if which::which("pip3").is_ok() || which::which("pip").is_ok() {
            found.push(PackageManager::Pip);
        }
        if which::which("go").is_ok() {
            found.push(PackageManager::Go);
        }

        found
    }

    /// Get install command for a package
    pub fn install_command(&self, package: &str) -> String {
        match self {
            PackageManager::Apt => format!("sudo apt install -y {}", package),
            PackageManager::Dnf => format!("sudo dnf install -y {}", package),
            PackageManager::Pacman => format!("sudo pacman -S --noconfirm {}", package),
            PackageManager::Homebrew => format!("brew install {}", package),
            PackageManager::Winget => format!("winget install {}", package),
            PackageManager::Scoop => format!("scoop install {}", package),
            PackageManager::Cargo => format!("cargo install {}", package),
            PackageManager::Pip => format!("pip3 install {}", package),
            PackageManager::Go => format!("go install {}", package),
        }
    }
}

/// Options for installation
#[derive(Debug, Clone, Default)]
pub struct InstallOptions {
    /// Specific version to install
    pub version: Option<String>,
    /// Force reinstall
    pub force: bool,
}

/// Result of an installation
#[derive(Debug, Clone)]
pub struct InstallResult {
    /// Path to the installed binary
    pub path: PathBuf,
    /// Installed version
    pub version: Option<String>,
    /// Additional arguments needed to run the adapter
    pub args: Vec<String>,
}

/// Trait for debugger installers
#[async_trait]
pub trait Installer: Send + Sync {
    /// Get debugger metadata
    fn info(&self) -> &DebuggerInfo;

    /// Check current installation status
    async fn status(&self) -> Result<InstallStatus>;

    /// Find the best installation method for current platform
    async fn best_method(&self) -> Result<InstallMethod>;

    /// Install the debugger
    async fn install(&self, opts: InstallOptions) -> Result<InstallResult>;

    /// Uninstall the debugger
    async fn uninstall(&self) -> Result<()>;

    /// Verify the installation works
    async fn verify(&self) -> Result<VerifyResult>;
}

/// Get the adapters installation directory
pub fn adapters_dir() -> PathBuf {
    let base = directories::ProjectDirs::from("", "", "debugger-cli")
        .map(|dirs| dirs.data_dir().to_path_buf())
        .unwrap_or_else(|| {
            // Fallback to platform-specific paths
            #[cfg(target_os = "linux")]
            let fallback = std::env::var("HOME")
                .map(PathBuf::from)
                .unwrap_or_else(|_| PathBuf::from("."))
                .join(".local/share/debugger-cli");

            #[cfg(target_os = "macos")]
            let fallback = std::env::var("HOME")
                .map(PathBuf::from)
                .unwrap_or_else(|_| PathBuf::from("."))
                .join("Library/Application Support/debugger-cli");

            #[cfg(target_os = "windows")]
            let fallback = std::env::var("LOCALAPPDATA")
                .map(PathBuf::from)
                .unwrap_or_else(|_| PathBuf::from("."))
                .join("debugger-cli");

            #[cfg(not(any(target_os = "linux", target_os = "macos", target_os = "windows")))]
            let fallback = PathBuf::from(".").join("debugger-cli");

            fallback
        });

    base.join("adapters")
}

/// Ensure the adapters directory exists
pub fn ensure_adapters_dir() -> Result<PathBuf> {
    let dir = adapters_dir();
    if !dir.exists() {
        std::fs::create_dir_all(&dir)?;
    }
    Ok(dir)
}

/// Download a file with progress reporting
pub async fn download_file(url: &str, dest: &Path) -> Result<()> {
    let client = reqwest::Client::new();
    let response = client
        .get(url)
        .header("User-Agent", "debugger-cli")
        .send()
        .await
        .map_err(|e| Error::Internal(format!("Failed to download {}: {}", url, e)))?;

    if !response.status().is_success() {
        return Err(Error::Internal(format!(
            "Download failed with status {}: {}",
            response.status(),
            url
        )));
    }

    let total_size = response.content_length().unwrap_or(0);

    let pb = if total_size > 0 {
        let pb = ProgressBar::new(total_size);
        pb.set_style(
            ProgressStyle::default_bar()
                .template("  [{bar:40.cyan/blue}] {bytes}/{total_bytes} ({eta})")
                .unwrap()
                .progress_chars("=> "),
        );
        Some(pb)
    } else {
        println!("  Downloading...");
        None
    };

    let mut file =
        std::fs::File::create(dest).map_err(|e| Error::Internal(format!("Failed to create file: {}", e)))?;

    let mut stream = response.bytes_stream();
    let mut downloaded: u64 = 0;

    while let Some(chunk) = stream.next().await {
        let chunk = chunk.map_err(|e| Error::Internal(format!("Download error: {}", e)))?;
        std::io::Write::write_all(&mut file, &chunk)?;
        downloaded += chunk.len() as u64;
        if let Some(ref pb) = pb {
            pb.set_position(downloaded);
        }
    }

    if let Some(pb) = pb {
        pb.finish_and_clear();
    }

    Ok(())
}

/// Extract a zip archive
pub fn extract_zip(archive_path: &Path, dest_dir: &Path) -> Result<()> {
    let file = std::fs::File::open(archive_path)?;
    let mut archive = zip::ZipArchive::new(file)
        .map_err(|e| Error::Internal(format!("Failed to open zip: {}", e)))?;

    for i in 0..archive.len() {
        let mut file = archive
            .by_index(i)
            .map_err(|e| Error::Internal(format!("Failed to read zip entry: {}", e)))?;

        let outpath = match file.enclosed_name() {
            Some(path) => dest_dir.join(path),
            None => continue,
        };

        if file.is_dir() {
            std::fs::create_dir_all(&outpath)?;
        } else {
            if let Some(parent) = outpath.parent() {
                if !parent.exists() {
                    std::fs::create_dir_all(parent)?;
                }
            }
            let mut outfile = std::fs::File::create(&outpath)?;
            std::io::copy(&mut file, &mut outfile)?;
        }

        // Set permissions on Unix
        #[cfg(unix)]
        {
            use std::os::unix::fs::PermissionsExt;
            if let Some(mode) = file.unix_mode() {
                std::fs::set_permissions(&outpath, std::fs::Permissions::from_mode(mode))?;
            }
        }
    }

    Ok(())
}

/// Extract a tar.gz archive
pub fn extract_tar_gz(archive_path: &Path, dest_dir: &Path) -> Result<()> {
    let file = std::fs::File::open(archive_path)?;
    let decoder = flate2::read::GzDecoder::new(file);
    let mut archive = tar::Archive::new(decoder);

    archive
        .unpack(dest_dir)
        .map_err(|e| Error::Internal(format!("Failed to extract tar.gz: {}", e)))?;

    Ok(())
}

/// Extract a tar.xz archive
pub fn extract_tar_xz(archive_path: &Path, dest_dir: &Path) -> Result<()> {
    let file = std::fs::File::open(archive_path)?;
    let decoder = xz2::read::XzDecoder::new(file);
    let mut archive = tar::Archive::new(decoder);

    archive
        .unpack(dest_dir)
        .map_err(|e| Error::Internal(format!("Failed to extract tar.xz: {}", e)))?;

    Ok(())
}

/// Make a file executable on Unix
#[cfg(unix)]
pub fn make_executable(path: &Path) -> Result<()> {
    use std::os::unix::fs::PermissionsExt;
    let mut perms = std::fs::metadata(path)?.permissions();
    perms.set_mode(perms.mode() | 0o755);
    std::fs::set_permissions(path, perms)?;
    Ok(())
}

#[cfg(not(unix))]
pub fn make_executable(_path: &Path) -> Result<()> {
    Ok(())
}

/// Run a shell command and return output
/// Note: This should only be used for trusted commands. For user input, use run_command_args.
pub async fn run_command(command: &str) -> Result<String> {
    let output = if cfg!(windows) {
        tokio::process::Command::new("cmd")
            .args(["/C", command])
            .output()
            .await
    } else {
        tokio::process::Command::new("sh")
            .args(["-c", command])
            .output()
            .await
    };

    let output = output.map_err(|e| Error::Internal(format!("Failed to run command: {}", e)))?;

    if !output.status.success() {
        let stderr = String::from_utf8_lossy(&output.stderr);
        return Err(Error::Internal(format!("Command failed: {}", stderr)));
    }

    Ok(String::from_utf8_lossy(&output.stdout).to_string())
}

/// Run a command with explicit arguments (safe from shell injection)
pub async fn run_command_args<S: AsRef<std::ffi::OsStr>>(
    program: &Path,
    args: &[S],
) -> Result<String> {
    let output = tokio::process::Command::new(program)
        .args(args)
        .output()
        .await
        .map_err(|e| Error::Internal(format!("Failed to run {}: {}", program.display(), e)))?;

    if !output.status.success() {
        let stderr = String::from_utf8_lossy(&output.stderr);
        return Err(Error::Internal(format!(
            "{} failed: {}",
            program.display(),
            stderr
        )));
    }

    Ok(String::from_utf8_lossy(&output.stdout).to_string())
}

/// Query GitHub API for latest release with retry logic
pub async fn get_github_release(repo: &str, version: Option<&str>) -> Result<GitHubRelease> {
    let client = reqwest::Client::new();
    let url = if let Some(v) = version {
        format!(
            "https://api.github.com/repos/{}/releases/tags/{}",
            repo, v
        )
    } else {
        format!("https://api.github.com/repos/{}/releases/latest", repo)
    };

    // Retry with exponential backoff (1s, 2s, 4s)
    let delays = [1, 2, 4];
    let mut last_error = None;

    for (attempt, delay) in std::iter::once(0).chain(delays.iter().copied()).enumerate() {
        if attempt > 0 {
            tokio::time::sleep(std::time::Duration::from_secs(delay)).await;
        }

        let response = match client
            .get(&url)
            .header("User-Agent", "debugger-cli")
            .header("Accept", "application/vnd.github.v3+json")
            .send()
            .await
        {
            Ok(r) => r,
            Err(e) => {
                last_error = Some(format!("GitHub API error: {}", e));
                continue;
            }
        };

        // Check for rate limiting
        if response.status() == reqwest::StatusCode::FORBIDDEN
            || response.status() == reqwest::StatusCode::TOO_MANY_REQUESTS
        {
            last_error = Some(
                "GitHub API rate limit exceeded. Set GITHUB_TOKEN env var to increase limit."
                    .to_string(),
            );
            continue;
        }

        if !response.status().is_success() {
            last_error = Some(format!("GitHub API returned status {}", response.status()));
            // Don't retry on 404 or other client errors
            if response.status().is_client_error() {
                break;
            }
            continue;
        }

        let release: GitHubRelease = response
            .json()
            .await
            .map_err(|e| Error::Internal(format!("Failed to parse GitHub response: {}", e)))?;

        return Ok(release);
    }

    Err(Error::Internal(
        last_error.unwrap_or_else(|| "GitHub API request failed".to_string()),
    ))
}

/// GitHub release information
#[derive(Debug, serde::Deserialize)]
pub struct GitHubRelease {
    pub tag_name: String,
    pub name: Option<String>,
    pub assets: Vec<GitHubAsset>,
}

/// GitHub release asset
#[derive(Debug, serde::Deserialize)]
pub struct GitHubAsset {
    pub name: String,
    pub browser_download_url: String,
    pub size: u64,
}

impl GitHubRelease {
    /// Find an asset matching a pattern
    pub fn find_asset(&self, patterns: &[&str]) -> Option<&GitHubAsset> {
        for pattern in patterns {
            if let Some(asset) = self.assets.iter().find(|a| {
                let name = a.name.to_lowercase();
                pattern
                    .to_lowercase()
                    .split('*')
                    .all(|part| name.contains(part))
            }) {
                return Some(asset);
            }
        }
        None
    }
}

/// Get current platform string for asset matching
pub fn platform_str() -> &'static str {
    match Platform::current() {
        Platform::Linux => "linux",
        Platform::MacOS => "darwin",
        Platform::Windows => "windows",
    }
}

/// Get current architecture string for asset matching
pub fn arch_str() -> &'static str {
    #[cfg(target_arch = "x86_64")]
    return "x86_64";

    #[cfg(target_arch = "aarch64")]
    return "aarch64";

    #[cfg(target_arch = "x86")]
    return "i686";

    #[cfg(not(any(target_arch = "x86_64", target_arch = "aarch64", target_arch = "x86")))]
    return "unknown";
}

/// Write version info to a file
pub fn write_version_file(dir: &Path, version: &str) -> Result<()> {
    let version_file = dir.join("version.txt");
    std::fs::write(&version_file, version)?;
    Ok(())
}

/// Read version from a version file
pub fn read_version_file(dir: &Path) -> Option<String> {
    let version_file = dir.join("version.txt");
    std::fs::read_to_string(&version_file)
        .ok()
        .map(|s| s.trim().to_string())
}