jarvy 0.0.3

Jarvy is a fast, cross-platform CLI that installs and manages developer tools across macOS and Linux.
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
//! Installation method detection and update execution
//!
//! Detects how Jarvy was installed (Homebrew, Cargo, apt, etc.)
//! and executes updates via the appropriate package manager.

#![allow(dead_code)] // Public API for installation method detection

use serde::{Deserialize, Serialize};
use std::env;
use std::path::{Path, PathBuf};
#[allow(unused_imports)] // Stdio used only on Linux/Windows for package manager checks
use std::process::{Command, Stdio};

/// Installation method for Jarvy
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum InstallMethod {
    /// Homebrew on macOS
    Homebrew,
    /// Cargo install (Rust)
    Cargo,
    /// apt package manager (Debian/Ubuntu)
    Apt,
    /// dnf package manager (Fedora/RHEL)
    Dnf,
    /// pacman package manager (Arch)
    Pacman,
    /// winget package manager (Windows)
    Winget,
    /// Chocolatey package manager (Windows)
    Chocolatey,
    /// Scoop package manager (Windows)
    Scoop,
    /// Direct binary installation
    Binary,
    /// Unknown installation method
    Unknown,
}

impl InstallMethod {
    /// Detect installation method from binary path and system queries
    pub fn detect() -> Self {
        // First check cached method
        if let Some(cached) = Self::load_cached() {
            // Verify the cache is still valid (binary hasn't moved)
            if Self::verify_cache_valid() {
                return cached;
            }
        }

        let method = Self::detect_fresh();

        // Cache the detected method
        if method != InstallMethod::Unknown {
            let _ = method.cache();
        }

        method
    }

    /// Detect installation method without cache
    fn detect_fresh() -> Self {
        // Get current binary path
        let binary_path = match env::current_exe() {
            Ok(path) => path,
            Err(_) => return InstallMethod::Unknown,
        };

        // Path-based detection
        if let Some(method) = Self::detect_from_path(&binary_path) {
            return method;
        }

        // Package manager query detection
        if let Some(method) = Self::detect_from_package_managers(&binary_path) {
            return method;
        }

        // Default to binary if we can't determine
        InstallMethod::Binary
    }

    /// Detect from binary path patterns
    fn detect_from_path(path: &Path) -> Option<Self> {
        let path_str = path.to_string_lossy();

        // macOS Homebrew paths
        if path_str.contains("/opt/homebrew/") || path_str.contains("/usr/local/Cellar/") {
            return Some(InstallMethod::Homebrew);
        }

        // Cargo install path
        if path_str.contains(".cargo/bin") {
            return Some(InstallMethod::Cargo);
        }

        // Windows package managers
        #[cfg(windows)]
        {
            if path_str.contains("\\scoop\\") || path_str.contains("/scoop/") {
                return Some(InstallMethod::Scoop);
            }
            if path_str.contains("\\chocolatey\\") || path_str.contains("/chocolatey/") {
                return Some(InstallMethod::Chocolatey);
            }
            // winget typically installs to Program Files
            if path_str.contains("\\WindowsApps\\") || path_str.contains("\\Program Files\\") {
                return Some(InstallMethod::Winget);
            }
        }

        // Linux system paths (likely from package manager)
        #[cfg(target_os = "linux")]
        {
            if path_str.starts_with("/usr/bin/") || path_str.starts_with("/usr/local/bin/") {
                // Could be apt, dnf, or pacman - need to query
                return None;
            }
        }

        None
    }

    /// Detect from package manager queries
    #[allow(unused_variables)] // binary_path only used on Linux/Windows
    fn detect_from_package_managers(binary_path: &Path) -> Option<Self> {
        // Linux: Check dpkg (Debian/Ubuntu)
        #[cfg(target_os = "linux")]
        {
            let path_str = binary_path.to_string_lossy();
            if Self::check_dpkg(&path_str) {
                return Some(InstallMethod::Apt);
            }

            if Self::check_rpm(&path_str) {
                return Some(InstallMethod::Dnf);
            }

            if Self::check_pacman() {
                return Some(InstallMethod::Pacman);
            }
        }

        // Windows: Check winget
        #[cfg(windows)]
        {
            if Self::check_winget() {
                return Some(InstallMethod::Winget);
            }
        }

        None
    }

    #[cfg(target_os = "linux")]
    fn check_dpkg(path: &str) -> bool {
        Command::new("dpkg")
            .args(["-S", path])
            .stdout(Stdio::null())
            .stderr(Stdio::null())
            .status()
            .map(|s| s.success())
            .unwrap_or(false)
    }

    #[cfg(target_os = "linux")]
    fn check_rpm(path: &str) -> bool {
        Command::new("rpm")
            .args(["-qf", path])
            .stdout(Stdio::null())
            .stderr(Stdio::null())
            .status()
            .map(|s| s.success())
            .unwrap_or(false)
    }

    #[cfg(target_os = "linux")]
    fn check_pacman() -> bool {
        Command::new("pacman")
            .args(["-Qs", "jarvy"])
            .stdout(Stdio::null())
            .stderr(Stdio::null())
            .status()
            .map(|s| s.success())
            .unwrap_or(false)
    }

    #[cfg(windows)]
    fn check_winget() -> bool {
        let output = Command::new("winget")
            .args(["list", "--name", "jarvy"])
            .stdout(Stdio::piped())
            .stderr(Stdio::null())
            .output();

        match output {
            Ok(out) => {
                let stdout = String::from_utf8_lossy(&out.stdout);
                stdout.to_lowercase().contains("jarvy")
            }
            Err(_) => false,
        }
    }

    #[cfg(not(target_os = "linux"))]
    fn check_dpkg(_path: &str) -> bool {
        false
    }

    #[cfg(not(target_os = "linux"))]
    fn check_rpm(_path: &str) -> bool {
        false
    }

    #[cfg(not(target_os = "linux"))]
    fn check_pacman() -> bool {
        false
    }

    #[cfg(not(windows))]
    fn check_winget() -> bool {
        false
    }

    /// Load cached installation method
    fn load_cached() -> Option<Self> {
        let cache_path = Self::cache_path()?;
        let content = std::fs::read_to_string(cache_path).ok()?;

        #[derive(Deserialize)]
        struct CacheEntry {
            method: InstallMethod,
            binary_path: String,
        }

        let entry: CacheEntry = serde_json::from_str(&content).ok()?;

        // Verify binary path matches
        let current_path = env::current_exe().ok()?;
        if current_path.to_string_lossy() == entry.binary_path {
            Some(entry.method)
        } else {
            None
        }
    }

    /// Verify the cached method is still valid
    fn verify_cache_valid() -> bool {
        // Already checked in load_cached via binary_path comparison
        true
    }

    /// Cache the installation method
    fn cache(&self) -> std::io::Result<()> {
        let cache_path = Self::cache_path()
            .ok_or_else(|| std::io::Error::new(std::io::ErrorKind::NotFound, "No cache path"))?;

        let binary_path = env::current_exe()?;

        #[derive(Serialize)]
        struct CacheEntry {
            method: InstallMethod,
            binary_path: String,
        }

        let entry = CacheEntry {
            method: *self,
            binary_path: binary_path.to_string_lossy().to_string(),
        };

        if let Some(parent) = cache_path.parent() {
            std::fs::create_dir_all(parent)?;
        }

        let content = serde_json::to_string_pretty(&entry)?;
        std::fs::write(cache_path, content)
    }

    /// Get cache file path
    fn cache_path() -> Option<PathBuf> {
        dirs::home_dir().map(|h| h.join(".jarvy").join("install-method.json"))
    }

    /// Parse installation method from string
    pub fn from_str(s: &str) -> Option<Self> {
        match s.to_lowercase().as_str() {
            "homebrew" | "brew" => Some(InstallMethod::Homebrew),
            "cargo" => Some(InstallMethod::Cargo),
            "apt" | "apt-get" => Some(InstallMethod::Apt),
            "dnf" | "yum" => Some(InstallMethod::Dnf),
            "pacman" => Some(InstallMethod::Pacman),
            "winget" => Some(InstallMethod::Winget),
            "chocolatey" | "choco" => Some(InstallMethod::Chocolatey),
            "scoop" => Some(InstallMethod::Scoop),
            "binary" => Some(InstallMethod::Binary),
            _ => None,
        }
    }

    /// Get method name as string
    pub fn as_str(&self) -> &'static str {
        match self {
            InstallMethod::Homebrew => "homebrew",
            InstallMethod::Cargo => "cargo",
            InstallMethod::Apt => "apt",
            InstallMethod::Dnf => "dnf",
            InstallMethod::Pacman => "pacman",
            InstallMethod::Winget => "winget",
            InstallMethod::Chocolatey => "chocolatey",
            InstallMethod::Scoop => "scoop",
            InstallMethod::Binary => "binary",
            InstallMethod::Unknown => "unknown",
        }
    }

    /// Execute update via this installation method
    pub fn execute_update(&self, version: Option<&str>) -> Result<(), UpdateError> {
        match self {
            InstallMethod::Homebrew => self.update_homebrew(),
            InstallMethod::Cargo => self.update_cargo(version),
            InstallMethod::Apt => self.update_apt(),
            InstallMethod::Dnf => self.update_dnf(),
            InstallMethod::Pacman => self.update_pacman(),
            InstallMethod::Winget => self.update_winget(),
            InstallMethod::Chocolatey => self.update_chocolatey(),
            InstallMethod::Scoop => self.update_scoop(),
            InstallMethod::Binary | InstallMethod::Unknown => {
                Err(UpdateError::MethodUnsupported(*self))
            }
        }
    }

    fn update_homebrew(&self) -> Result<(), UpdateError> {
        let status = Command::new("brew")
            .args(["upgrade", "jarvy"])
            .status()
            .map_err(|e| UpdateError::ExecutionFailed(format!("brew upgrade: {}", e)))?;

        if status.success() {
            Ok(())
        } else {
            Err(UpdateError::ExecutionFailed(
                "brew upgrade jarvy failed".to_string(),
            ))
        }
    }

    fn update_cargo(&self, version: Option<&str>) -> Result<(), UpdateError> {
        let mut cmd = Command::new("cargo");
        cmd.args(["install", "jarvy"]);

        if let Some(v) = version {
            cmd.args(["--version", v]);
        }

        let status = cmd
            .status()
            .map_err(|e| UpdateError::ExecutionFailed(format!("cargo install: {}", e)))?;

        if status.success() {
            Ok(())
        } else {
            Err(UpdateError::ExecutionFailed(
                "cargo install jarvy failed".to_string(),
            ))
        }
    }

    fn update_apt(&self) -> Result<(), UpdateError> {
        // First update package lists
        let update_status = Command::new("sudo")
            .args(["apt-get", "update"])
            .status()
            .map_err(|e| UpdateError::ExecutionFailed(format!("apt update: {}", e)))?;

        if !update_status.success() {
            return Err(UpdateError::ExecutionFailed(
                "apt-get update failed".to_string(),
            ));
        }

        // Then upgrade jarvy
        let status = Command::new("sudo")
            .args(["apt-get", "install", "--only-upgrade", "-y", "jarvy"])
            .status()
            .map_err(|e| UpdateError::ExecutionFailed(format!("apt upgrade: {}", e)))?;

        if status.success() {
            Ok(())
        } else {
            Err(UpdateError::ExecutionFailed(
                "apt-get upgrade jarvy failed".to_string(),
            ))
        }
    }

    fn update_dnf(&self) -> Result<(), UpdateError> {
        let status = Command::new("sudo")
            .args(["dnf", "upgrade", "-y", "jarvy"])
            .status()
            .map_err(|e| UpdateError::ExecutionFailed(format!("dnf upgrade: {}", e)))?;

        if status.success() {
            Ok(())
        } else {
            Err(UpdateError::ExecutionFailed(
                "dnf upgrade jarvy failed".to_string(),
            ))
        }
    }

    fn update_pacman(&self) -> Result<(), UpdateError> {
        let status = Command::new("sudo")
            .args(["pacman", "-Syu", "--noconfirm", "jarvy"])
            .status()
            .map_err(|e| UpdateError::ExecutionFailed(format!("pacman upgrade: {}", e)))?;

        if status.success() {
            Ok(())
        } else {
            Err(UpdateError::ExecutionFailed(
                "pacman upgrade jarvy failed".to_string(),
            ))
        }
    }

    fn update_winget(&self) -> Result<(), UpdateError> {
        let status = Command::new("winget")
            .args(["upgrade", "jarvy"])
            .status()
            .map_err(|e| UpdateError::ExecutionFailed(format!("winget upgrade: {}", e)))?;

        if status.success() {
            Ok(())
        } else {
            Err(UpdateError::ExecutionFailed(
                "winget upgrade jarvy failed".to_string(),
            ))
        }
    }

    fn update_chocolatey(&self) -> Result<(), UpdateError> {
        let status = Command::new("choco")
            .args(["upgrade", "jarvy", "-y"])
            .status()
            .map_err(|e| UpdateError::ExecutionFailed(format!("choco upgrade: {}", e)))?;

        if status.success() {
            Ok(())
        } else {
            Err(UpdateError::ExecutionFailed(
                "choco upgrade jarvy failed".to_string(),
            ))
        }
    }

    fn update_scoop(&self) -> Result<(), UpdateError> {
        let status = Command::new("scoop")
            .args(["update", "jarvy"])
            .status()
            .map_err(|e| UpdateError::ExecutionFailed(format!("scoop update: {}", e)))?;

        if status.success() {
            Ok(())
        } else {
            Err(UpdateError::ExecutionFailed(
                "scoop update jarvy failed".to_string(),
            ))
        }
    }

    /// Check if this method supports direct updates
    pub fn supports_direct_update(&self) -> bool {
        !matches!(self, InstallMethod::Binary | InstallMethod::Unknown)
    }
}

impl std::fmt::Display for InstallMethod {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.as_str())
    }
}

/// Errors during update execution
#[derive(Debug, thiserror::Error)]
pub enum UpdateError {
    #[error("Unsupported installation method: {0}")]
    MethodUnsupported(InstallMethod),

    #[error("Update execution failed: {0}")]
    ExecutionFailed(String),

    #[error("Binary download failed: {0}")]
    DownloadFailed(String),

    #[error("Checksum verification failed")]
    ChecksumMismatch,

    #[error("Binary installation failed: {0}")]
    InstallationFailed(String),

    #[error("Rollback failed: {0}")]
    RollbackFailed(String),

    #[error("IO error: {0}")]
    Io(#[from] std::io::Error),
}

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

    #[test]
    fn test_install_method_from_str() {
        assert_eq!(
            InstallMethod::from_str("homebrew"),
            Some(InstallMethod::Homebrew)
        );
        assert_eq!(
            InstallMethod::from_str("brew"),
            Some(InstallMethod::Homebrew)
        );
        assert_eq!(InstallMethod::from_str("CARGO"), Some(InstallMethod::Cargo));
        assert_eq!(InstallMethod::from_str("apt"), Some(InstallMethod::Apt));
        assert_eq!(InstallMethod::from_str("invalid"), None);
    }

    #[test]
    fn test_install_method_as_str() {
        assert_eq!(InstallMethod::Homebrew.as_str(), "homebrew");
        assert_eq!(InstallMethod::Cargo.as_str(), "cargo");
        assert_eq!(InstallMethod::Binary.as_str(), "binary");
    }

    #[test]
    fn test_supports_direct_update() {
        assert!(InstallMethod::Homebrew.supports_direct_update());
        assert!(InstallMethod::Cargo.supports_direct_update());
        assert!(!InstallMethod::Binary.supports_direct_update());
        assert!(!InstallMethod::Unknown.supports_direct_update());
    }
}