• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

ssh-vault / ssh-vault / 17490029206

05 Sep 2025 10:00AM UTC coverage: 89.194% (-1.2%) from 90.38%
17490029206

push

github

nbari
bump versions

1948 of 2184 relevant lines covered (89.19%)

49.26 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

94.59
/src/cache.rs
1
use crate::tools::get_home;
2
use anyhow::{Result, anyhow};
3
use std::{
4
    fs,
5
    path::{Path, PathBuf},
6
    time::{Duration, SystemTime},
7
};
8

9
// Load the response from a cache file ~/.ssh/vault/keys/<key>
10
/// # Errors
11
/// Return an error if the cache is older than 30 days
12
pub fn get(key: &str) -> Result<String> {
1✔
13
    let cache = get_cache_path(key)?;
1✔
14
    if cache.exists() {
1✔
15
        let metadata = fs::metadata(&cache);
1✔
16
        let last_modified = metadata.map_or_else(
1✔
17
            |_| SystemTime::now(),
×
18
            |meta| meta.modified().unwrap_or_else(|_| SystemTime::now()),
1✔
19
        );
20

21
        // Calculate the duration since the file was last modified
22
        let duration_since_modified = SystemTime::now()
1✔
23
            .duration_since(last_modified)
1✔
24
            .unwrap_or(Duration::from_secs(0));
1✔
25

26
        // Return an error if the cache is older than 30 days
27
        if duration_since_modified > Duration::from_secs(30 * 24 * 60 * 60) {
1✔
28
            Err(anyhow!("cache expired"))
×
29
        } else {
30
            Ok(fs::read_to_string(cache)?)
1✔
31
        }
32
    } else {
33
        Err(anyhow!("cache not found"))
×
34
    }
35
}
1✔
36

37
/// Save the response to a cache file ~/.ssh/vault/keys/<key>
38
/// # Errors
39
/// Return an error if the cache file can't be created
40
pub fn put(key: &str, response: &str) -> Result<()> {
2✔
41
    let cache = get_cache_path(key)?;
2✔
42
    // Create parent directories if they don't exist
43
    if let Some(parent_dir) = std::path::Path::new(&cache).parent() {
2✔
44
        fs::create_dir_all(parent_dir)?;
2✔
45
    }
×
46
    Ok(fs::write(cache, response)?)
2✔
47
}
2✔
48

49
/// Get the path to the cache file ~/.ssh/vault/keys/<key>
50
/// # Errors
51
/// Return an error if we can't get the path to the cache file
52
fn get_cache_path(key: &str) -> Result<PathBuf> {
6✔
53
    let ssh_vault = get_ssh_vault_path()?;
6✔
54
    Ok(ssh_vault.join("keys").join(key))
6✔
55
}
6✔
56

57
/// Get the path to the ssh-vault directory ~/.ssh/vault
58
/// # Errors
59
/// Return an error if we can't get the path to the ssh-vault directory
60
fn get_ssh_vault_path() -> Result<PathBuf> {
7✔
61
    let home = get_home()?;
7✔
62
    Ok(Path::new(&home).join(".ssh").join("vault"))
7✔
63
}
7✔
64

65
#[cfg(test)]
66
mod tests {
67
    use super::*;
68
    use std::fs;
69

70
    #[test]
71
    fn test_get_cache_path() {
1✔
72
        let cache = get_cache_path("test").unwrap();
1✔
73
        assert_eq!(cache.is_dir(), false);
1✔
74
        assert_eq!(
1✔
75
            cache.to_str(),
1✔
76
            get_home()
1✔
77
                .unwrap()
1✔
78
                .join(".ssh")
1✔
79
                .join("vault")
1✔
80
                .join("keys")
1✔
81
                .join("test")
1✔
82
                .to_str()
1✔
83
        );
84
    }
1✔
85

86
    #[test]
87
    fn test_get_ssh_vault_path() {
1✔
88
        let ssh_vault = get_ssh_vault_path().unwrap();
1✔
89
        assert_eq!(ssh_vault.is_file(), false);
1✔
90
        assert_eq!(
1✔
91
            ssh_vault.to_str(),
1✔
92
            get_home().unwrap().join(".ssh").join("vault").to_str()
1✔
93
        );
94
    }
1✔
95

96
    #[test]
97
    fn test_put() {
1✔
98
        let cache = get_cache_path("test-2").unwrap();
1✔
99
        put("test-2", "test").unwrap();
1✔
100

101
        assert_eq!(cache.is_file(), true);
1✔
102
        assert_eq!(cache.is_dir(), false);
1✔
103
        assert_eq!(cache.exists(), true);
1✔
104
        assert_eq!(
1✔
105
            cache.to_str(),
1✔
106
            get_home()
1✔
107
                .unwrap()
1✔
108
                .join(".ssh")
1✔
109
                .join("vault")
1✔
110
                .join("keys")
1✔
111
                .join("test-2")
1✔
112
                .to_str()
1✔
113
        );
114
        fs::remove_file(cache).unwrap();
1✔
115
    }
1✔
116

117
    #[test]
118
    fn test_get() {
1✔
119
        let cache = get_cache_path("test-3").unwrap();
1✔
120
        put("test-3", "test").unwrap();
1✔
121
        let response = get("test-3").unwrap();
1✔
122
        assert_eq!(response, "test");
1✔
123
        fs::remove_file(cache).unwrap();
1✔
124
    }
1✔
125
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc