aboutsummaryrefslogtreecommitdiffstats
path: root/crates/turtle/src/atuin_client/plugin.rs
blob: e97b1dbf164cdf64b73ab56981be41b8d18c0107 (plain) (blame)
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
use std::collections::HashMap;

#[derive(Debug, Clone)]
pub(crate) struct OfficialPlugin {
    pub(crate) name: String,
    pub(crate) description: String,
    pub(crate) install_message: String,
}

impl OfficialPlugin {
    pub(crate) fn new(name: &str, description: &str, install_message: &str) -> Self {
        Self {
            name: name.to_string(),
            description: description.to_string(),
            install_message: install_message.to_string(),
        }
    }
}

pub(crate) struct OfficialPluginRegistry {
    plugins: HashMap<String, OfficialPlugin>,
}

impl OfficialPluginRegistry {
    pub(crate) fn new() -> Self {
        let mut registry = Self {
            plugins: HashMap::new(),
        };

        // Register official plugins
        registry.register_official_plugins();

        registry
    }

    fn register_official_plugins(&mut self) {
        // atuin-update plugin
        self.plugins.insert(
            "update".to_string(),
            OfficialPlugin::new(
                "update",
                "Update atuin to the latest version",
                "The 'atuin update' command is provided by the atuin-update plugin.\n\
                 It is only installed if you used the install script\n  \
                 If you used a package manager (brew, apt, etc), please continue to use it for updates",
            ),
        );
    }

    pub(crate) fn get_plugin(&self, name: &str) -> Option<&OfficialPlugin> {
        self.plugins.get(name)
    }

    pub(crate) fn is_official_plugin(&self, name: &str) -> bool {
        self.plugins.contains_key(name)
    }

    pub(crate) fn get_install_message(&self, name: &str) -> Option<&str> {
        self.plugins
            .get(name)
            .map(|plugin| plugin.install_message.as_str())
    }
}

impl Default for OfficialPluginRegistry {
    fn default() -> Self {
        Self::new()
    }
}

pub(crate) struct PluginContext {
    #[cfg(windows)]
    _update_on_windows: Option<UpdateOnWindowsContext>,
}

impl PluginContext {
    pub(crate) fn new(_subcommand: &str) -> Self {
        PluginContext {
            #[cfg(windows)]
            _update_on_windows: (_subcommand == "update").then(UpdateOnWindowsContext::new),
        }
    }
}

impl Drop for PluginContext {
    fn drop(&mut self) {}
}

#[cfg(windows)]
struct UpdateOnWindowsContext {
    initial_exe: Option<std::path::PathBuf>,
}

#[cfg(windows)]
impl UpdateOnWindowsContext {
    const OLD_FILE_NAME: &'static str = "atuin.old";

    pub(crate) fn new() -> Self {
        // Windows doesn't let you overwrite a running exe, but it lets you rename it,
        // so make some room for atuin-update to install the new version.
        let initial_exe = std::env::current_exe().ok().and_then(|exe| {
            std::fs::rename(&exe, exe.with_file_name(Self::OLD_FILE_NAME)).ok()?;
            Some(exe)
        });

        Self { initial_exe }
    }
}

#[cfg(windows)]
impl Drop for UpdateOnWindowsContext {
    fn drop(&mut self) {
        if let Some(exe) = &self.initial_exe
            && !exe.exists()
        {
            // The update failed, roll back the current exe to its initial name.
            std::fs::rename(exe.with_file_name(Self::OLD_FILE_NAME), exe).unwrap_or_else(|e| {
                eprintln!("Failed to roll back the update, you may need to reinstall Atuin: {e}");
            });
        }
    }
}

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

    #[test]
    fn test_registry_creation() {
        let registry = OfficialPluginRegistry::new();
        assert!(registry.is_official_plugin("update"));
        assert!(!registry.is_official_plugin("nonexistent"));
    }

    #[test]
    fn test_get_plugin() {
        let registry = OfficialPluginRegistry::new();
        let plugin = registry.get_plugin("update");
        assert!(plugin.is_some());
        assert_eq!(plugin.unwrap().name, "update");
    }

    #[test]
    fn test_get_install_message() {
        let registry = OfficialPluginRegistry::new();
        let message = registry.get_install_message("update");
        assert!(message.is_some());
        assert!(message.unwrap().contains("atuin-update"));
    }
}