jarvy 0.0.5

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
//! Shell initialization and ensure logic
//!
//! Provides two CLI features:
//! - `jarvy shell-init` — outputs an RC snippet for eval in shell profiles
//! - `jarvy ensure` — lightweight check-and-install for shell startup
//!
//! Configuration lives in `~/.jarvy/config.toml` under `[shell_init]`.
//! State is tracked in `~/.jarvy/ensure.stamp` to enable fast-path skipping.

use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fs;
use std::path::PathBuf;
use std::time::{SystemTime, UNIX_EPOCH};

use crate::env::ShellType;
use crate::tools;

/// Configuration for shell init auto-ensure (in ~/.jarvy/config.toml)
#[derive(Deserialize, Serialize, Debug, Clone)]
pub struct ShellInitConfig {
    /// Whether shell-init is enabled
    #[serde(default)]
    pub enabled: bool,
    /// Inline tool list to ensure on shell startup
    #[serde(default)]
    pub tools: Option<Vec<String>>,
    /// Version hints per tool
    #[serde(default)]
    pub versions: Option<HashMap<String, String>>,
    /// Run installation in background (default: true)
    #[serde(default = "default_true")]
    pub background: bool,
    /// Hours between re-checks (default: 24, 0 = every shell open)
    #[serde(default = "default_24")]
    pub check_interval: u64,
}

impl Default for ShellInitConfig {
    fn default() -> Self {
        Self {
            enabled: false,
            tools: None,
            versions: None,
            background: true,
            check_interval: 24,
        }
    }
}

fn default_true() -> bool {
    true
}

fn default_24() -> u64 {
    24
}

impl ShellInitConfig {
    /// Compute a hash of the config for stamp comparison.
    ///
    /// Streams the inputs into the hasher directly — no `Vec` / `String`
    /// clones — because this runs on every shell open via `jarvy ensure`.
    pub fn config_hash(&self) -> String {
        use sha2::{Digest, Sha256};
        let mut hasher = Sha256::new();
        if let Some(t) = self.tools.as_ref() {
            // Sort indices, not the strings themselves.
            let mut idx: Vec<&str> = t.iter().map(String::as_str).collect();
            idx.sort_unstable();
            for (i, name) in idx.iter().enumerate() {
                if i > 0 {
                    hasher.update(b",");
                }
                hasher.update(name.as_bytes());
            }
            hasher.update(b";");
        }
        if let Some(v) = self.versions.as_ref() {
            let mut keys: Vec<&str> = v.keys().map(String::as_str).collect();
            keys.sort_unstable();
            for k in keys {
                hasher.update(k.as_bytes());
                hasher.update(b"=");
                if let Some(val) = v.get(k) {
                    hasher.update(val.as_bytes());
                }
                hasher.update(b";");
            }
        }
        // Format matches drift::state::hash_string ("sha256:<hex>") so the
        // existing stamp files are not invalidated by this rewrite.
        format!("sha256:{}", hex::encode(hasher.finalize()))
    }

    /// Build (tool_name, version_hint) pairs from config without cloning.
    pub fn tool_tasks(&self) -> Vec<(&str, &str)> {
        let Some(tools) = self.tools.as_ref() else {
            return Vec::new();
        };
        let versions = self.versions.as_ref();
        tools
            .iter()
            .map(|name| {
                let hint = versions
                    .and_then(|v| v.get(name))
                    .map(String::as_str)
                    .unwrap_or("");
                (name.as_str(), hint)
            })
            .collect()
    }
}

/// Stamp file tracking ensure state (~/.jarvy/ensure.stamp)
#[derive(Deserialize, Serialize, Debug)]
pub struct EnsureStamp {
    pub config_hash: String,
    pub last_check: u64,
    pub tools_installed: Vec<String>,
    pub jarvy_version: String,
}

impl EnsureStamp {
    /// Path to the stamp file
    fn path() -> Option<PathBuf> {
        dirs::home_dir().map(|h| h.join(".jarvy").join("ensure.stamp"))
    }

    /// Load the stamp from disk
    pub fn load() -> Option<Self> {
        let path = Self::path()?;
        let content = fs::read_to_string(path).ok()?;
        serde_json::from_str(&content).ok()
    }

    /// Save the stamp to disk
    pub fn save(&self) -> Result<(), std::io::Error> {
        let path = Self::path().ok_or_else(|| {
            std::io::Error::new(std::io::ErrorKind::NotFound, "no home directory")
        })?;
        let dir = path.parent().unwrap_or_else(|| std::path::Path::new("."));
        fs::create_dir_all(dir)?;
        let json = serde_json::to_string_pretty(self).map_err(std::io::Error::other)?;
        // Atomic write: write to unpredictable temp file, then rename
        let tmp = tempfile::NamedTempFile::new_in(dir)?;
        fs::write(tmp.path(), &json)?;
        tmp.persist(&path).map_err(|e| e.error)?;
        Ok(())
    }

    /// Check if the stamp is fresh (config unchanged and within check interval)
    pub fn is_fresh(&self, config_hash: &str, interval_hours: u64) -> bool {
        if self.config_hash != config_hash {
            return false;
        }
        if interval_hours == 0 {
            return false;
        }
        let now = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .map(|d| d.as_secs())
            .unwrap_or(0);
        let elapsed_hours = (now.saturating_sub(self.last_check)) / 3600;
        elapsed_hours < interval_hours
    }
}

/// Generate the RC snippet for a given shell type
pub fn generate_rc_snippet(shell: ShellType) -> String {
    match shell {
        ShellType::Fish => "if command -q jarvy\n  jarvy ensure --quiet\nend".to_string(),
        ShellType::PowerShell => {
            "if (Get-Command jarvy -ErrorAction SilentlyContinue) {\n  jarvy ensure --quiet\n}"
                .to_string()
        }
        _ => {
            // Bash, Zsh, Sh
            "if command -v jarvy &> /dev/null; then\n  jarvy ensure --quiet\nfi".to_string()
        }
    }
}

/// Refuse to run ensure when the global config file is writable by anyone
/// other than the owner. This prevents persistence-via-shell-startup attacks
/// where a co-tenant rewrites `~/.jarvy/config.toml` to inject tool installs
/// that fire on every new shell.
#[cfg(unix)]
fn refuse_if_config_is_world_or_group_writable() -> Result<(), String> {
    use std::os::unix::fs::PermissionsExt;
    let Some(path) = crate::init::global_config_path() else {
        return Ok(());
    };
    if !path.exists() {
        return Ok(());
    }
    let Ok(meta) = std::fs::metadata(&path) else {
        return Ok(());
    };
    let mode = meta.permissions().mode();
    if mode & 0o022 != 0 {
        return Err(format!(
            "Refusing to run `jarvy ensure`: {} is writable by group/other ({:o}). \
             Run `chmod 600 ~/.jarvy/config.toml` and try again.",
            crate::network::redact_home(&path.display().to_string()),
            mode & 0o777
        ));
    }
    Ok(())
}

#[cfg(not(unix))]
fn refuse_if_config_is_world_or_group_writable() -> Result<(), String> {
    Ok(())
}

/// Run the ensure check: install missing tools if stamp is stale
pub fn run_ensure(config: &ShellInitConfig, force: bool, quiet: bool) -> Result<(), String> {
    refuse_if_config_is_world_or_group_writable()?;

    let config_hash = config.config_hash();
    let start = std::time::Instant::now();

    // Fast path: check stamp
    if !force {
        if let Some(stamp) = EnsureStamp::load() {
            if stamp.is_fresh(&config_hash, config.check_interval) {
                tracing::debug!(event = "ensure.fast_path", reason = "stamp_fresh");
                return Ok(());
            }
        }
    }

    // Slow path: register tools and install missing ones
    tools::register_all();

    let tasks = config.tool_tasks();
    let mut installed: Vec<String> = Vec::new();
    let mut failed_count: u32 = 0;

    for (name, hint) in &tasks {
        // Check if already installed via `has` (quick PATH check)
        if tools::has(name) && hint.is_empty() {
            installed.push((*name).to_string());
            continue;
        }

        if !quiet {
            eprintln!("jarvy ensure: installing {}...", name);
        }
        // Telemetry runs regardless of --quiet so debug bundles still see the
        // signal even when interactive output is suppressed.
        tracing::info!(
            event = "ensure.tool.start",
            tool = %name,
            hint = %hint,
        );

        match tools::add(name, hint) {
            Ok(_) => {
                if !quiet {
                    eprintln!("jarvy ensure: {} installed", name);
                }
                tracing::info!(event = "ensure.tool.success", tool = %name);
                installed.push((*name).to_string());
            }
            Err(e) => {
                if !quiet {
                    eprintln!("jarvy ensure: {} failed: {}", name, e);
                }
                tracing::warn!(
                    event = "ensure.tool.failed",
                    tool = %name,
                    error = %e,
                );
                failed_count += 1;
            }
        }
    }

    // Write stamp
    let now = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map(|d| d.as_secs())
        .unwrap_or(0);

    let stamp = EnsureStamp {
        config_hash,
        last_check: now,
        tools_installed: installed.clone(),
        jarvy_version: env!("CARGO_PKG_VERSION").to_string(),
    };

    if let Err(e) = stamp.save() {
        if !quiet {
            eprintln!("jarvy ensure: failed to write stamp: {}", e);
        }
        tracing::warn!(event = "ensure.stamp.write_failed", error = %e);
    }

    tracing::info!(
        event = "ensure.run.complete",
        tasks = tasks.len(),
        installed = installed.len(),
        failed = failed_count,
        duration_ms = start.elapsed().as_millis() as u64,
    );

    Ok(())
}

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

    #[test]
    fn test_shell_init_config_default() {
        let config = ShellInitConfig::default();
        assert!(!config.enabled);
        assert!(config.background);
        assert_eq!(config.check_interval, 24);
    }

    #[test]
    fn test_config_hash_deterministic() {
        let config = ShellInitConfig {
            enabled: true,
            tools: Some(vec!["git".into(), "docker".into()]),
            versions: None,
            background: true,
            check_interval: 24,
        };
        let h1 = config.config_hash();
        let h2 = config.config_hash();
        assert_eq!(h1, h2);
    }

    #[test]
    fn test_config_hash_changes_with_tools() {
        let c1 = ShellInitConfig {
            tools: Some(vec!["git".into()]),
            ..Default::default()
        };
        let c2 = ShellInitConfig {
            tools: Some(vec!["docker".into()]),
            ..Default::default()
        };
        assert_ne!(c1.config_hash(), c2.config_hash());
    }

    #[test]
    fn test_tool_tasks() {
        let config = ShellInitConfig {
            tools: Some(vec!["node".into(), "git".into()]),
            versions: Some(HashMap::from([("node".into(), "20".into())])),
            ..Default::default()
        };
        let tasks = config.tool_tasks();
        assert_eq!(tasks.len(), 2);
        assert!(tasks.contains(&("node", "20")));
        assert!(tasks.contains(&("git", "")));
    }

    #[test]
    fn config_hash_format_is_sha256_prefixed_hex() {
        let config = ShellInitConfig {
            tools: Some(vec!["git".into()]),
            ..Default::default()
        };
        let h = config.config_hash();
        assert!(h.starts_with("sha256:"));
        let hex = &h["sha256:".len()..];
        assert_eq!(hex.len(), 64);
        assert!(hex.chars().all(|c| c.is_ascii_hexdigit()));
    }

    #[test]
    fn config_hash_is_independent_of_tool_order() {
        let a = ShellInitConfig {
            tools: Some(vec!["git".into(), "node".into(), "docker".into()]),
            ..Default::default()
        };
        let b = ShellInitConfig {
            tools: Some(vec!["node".into(), "docker".into(), "git".into()]),
            ..Default::default()
        };
        assert_eq!(a.config_hash(), b.config_hash());
    }

    #[test]
    fn test_stamp_freshness() {
        let now = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap()
            .as_secs();

        let stamp = EnsureStamp {
            config_hash: "sha256:abc".into(),
            last_check: now,
            tools_installed: vec![],
            jarvy_version: "0.2".into(),
        };

        // Same hash, within interval
        assert!(stamp.is_fresh("sha256:abc", 24));
        // Different hash
        assert!(!stamp.is_fresh("sha256:def", 24));
        // Interval 0 always stale
        assert!(!stamp.is_fresh("sha256:abc", 0));
    }

    #[test]
    fn test_stamp_expired() {
        let old_time = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap()
            .as_secs()
            - (25 * 3600); // 25 hours ago

        let stamp = EnsureStamp {
            config_hash: "sha256:abc".into(),
            last_check: old_time,
            tools_installed: vec![],
            jarvy_version: "0.2".into(),
        };

        assert!(!stamp.is_fresh("sha256:abc", 24));
    }

    #[test]
    fn test_generate_rc_snippet_bash() {
        let snippet = generate_rc_snippet(ShellType::Bash);
        assert!(snippet.contains("command -v jarvy"));
        assert!(snippet.contains("jarvy ensure --quiet"));
    }

    #[test]
    fn test_generate_rc_snippet_fish() {
        let snippet = generate_rc_snippet(ShellType::Fish);
        assert!(snippet.contains("command -q jarvy"));
        assert!(snippet.contains("end"));
    }

    #[test]
    fn test_generate_rc_snippet_powershell() {
        let snippet = generate_rc_snippet(ShellType::PowerShell);
        assert!(snippet.contains("Get-Command"));
        assert!(snippet.contains("jarvy ensure --quiet"));
    }
}