purple_ssh/app/
reload_state.rs1use std::path::{Path, PathBuf};
4use std::time::SystemTime;
5
6use crate::ssh_config::model::SshConfigFile;
7
8#[derive(Default)]
10pub struct ReloadState {
11 pub(in crate::app) config_path: PathBuf,
12 pub(in crate::app) last_modified: Option<SystemTime>,
13 pub(in crate::app) include_mtimes: Vec<(PathBuf, Option<SystemTime>)>,
14 pub(in crate::app) include_dir_mtimes: Vec<(PathBuf, Option<SystemTime>)>,
15 pub(in crate::app) keys_dir_mtime: Option<SystemTime>,
19 pub(in crate::app) key_file_mtimes: Vec<(PathBuf, Option<SystemTime>)>,
23}
24
25#[derive(Default)]
27pub struct ConflictState {
28 pub form_mtime: Option<SystemTime>,
29 pub form_include_mtimes: Vec<(PathBuf, Option<SystemTime>)>,
30 pub form_include_dir_mtimes: Vec<(PathBuf, Option<SystemTime>)>,
31 pub provider_form_mtime: Option<SystemTime>,
32}
33
34impl ReloadState {
35 pub fn config_path(&self) -> &Path {
36 &self.config_path
37 }
38
39 pub fn from_config(config: &SshConfigFile) -> Self {
42 let config_path = config.path.clone();
43 let last_modified = get_mtime(&config_path);
44 let include_mtimes = snapshot_include_mtimes(config);
45 let include_dir_mtimes = snapshot_include_dir_mtimes(config);
46 Self {
47 config_path,
48 last_modified,
49 include_mtimes,
50 include_dir_mtimes,
51 keys_dir_mtime: None,
52 key_file_mtimes: Vec::new(),
53 }
54 }
55}
56
57pub fn get_mtime(path: &Path) -> Option<SystemTime> {
59 std::fs::metadata(path).ok()?.modified().ok()
60}
61
62pub fn snapshot_include_mtimes(config: &SshConfigFile) -> Vec<(PathBuf, Option<SystemTime>)> {
64 config
65 .include_paths()
66 .into_iter()
67 .map(|p| {
68 let mtime = get_mtime(&p);
69 (p, mtime)
70 })
71 .collect()
72}
73
74pub fn snapshot_include_dir_mtimes(config: &SshConfigFile) -> Vec<(PathBuf, Option<SystemTime>)> {
76 config
77 .include_glob_dirs()
78 .into_iter()
79 .map(|p| {
80 let mtime = get_mtime(&p);
81 (p, mtime)
82 })
83 .collect()
84}
85
86pub fn snapshot_key_mtimes(
91 ssh_dir: &Path,
92 keys: &[crate::ssh_keys::SshKeyInfo],
93) -> Vec<(PathBuf, Option<SystemTime>)> {
94 keys.iter()
95 .map(|k| {
96 let pub_path = ssh_dir.join(format!("{}.pub", k.name));
97 let mtime = get_mtime(&pub_path);
98 (pub_path, mtime)
99 })
100 .collect()
101}