phpup/commands/
uninstall.rs1use super::{Command, Config, ConfigError};
2use crate::decorized::Decorized;
3use crate::symlink;
4use crate::version;
5use crate::version::Local;
6use crate::version::Version;
7use clap;
8use std::fs;
9use thiserror::Error;
10
11#[derive(clap::Parser, Debug)]
12pub struct Uninstall {
13 version: Version,
14}
15
16#[derive(Error, Debug)]
17pub enum Error {
18 #[error("Can't find installed version '{0}'")]
19 NotInstalled(Version),
20 #[error(transparent)]
21 NoMultiShellPath(#[from] ConfigError),
22}
23
24impl Command for Uninstall {
25 type Error = Error;
26
27 fn run(&self, config: &Config) -> Result<(), Error> {
28 let uninstall_version = version::installed(config)
29 .find(|installed_version| installed_version == &self.version)
30 .ok_or(Error::NotInstalled(self.version))?;
31
32 if Local::current(config) == Some(Local::Installed(uninstall_version)) {
33 symlink::remove(&config.multishell_path()?).expect("Can't remove symlink!");
34 }
35
36 let version_dir = config.versions_dir().join(uninstall_version.to_string());
37 fs::remove_dir_all(&version_dir).expect("Can't remove installed directory");
38 println!(
39 "{} was removed successfully from {}",
40 uninstall_version.decorized_with_prefix(),
41 version_dir.display().decorized()
42 );
43
44 Ok(())
45 }
46}
47
48#[cfg(test)]
49mod tests {}