Skip to content

Commit b51d87c

Browse files
committed
add uninstall command
1 parent 0606781 commit b51d87c

File tree

3 files changed

+49
-1
lines changed

3 files changed

+49
-1
lines changed

src/main.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ mod python;
22
mod utils;
33

44
use python::{
5-
env::posix_env, install::install_python, list::list_python_versions, usage::use_python,
5+
env::posix_env, install::install_python, list::list_python_versions, usage::use_python, uninstall::uninstall_python
66
};
77
use std::process::exit;
88

@@ -53,6 +53,8 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
5353
install_python(version.unwrap()).await?;
5454
} else if cmd == "use" {
5555
use_python(version.unwrap()).await?;
56+
} else if cmd == "uninstall" {
57+
uninstall_python(version.unwrap()).await?;
5658
} else if cmd == "list" {
5759
list_python_versions(version.unwrap()).await?;
5860
}

src/python/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
pub mod env;
22
pub mod install;
33
pub mod list;
4+
pub mod uninstall;
45
pub mod usage;
56
pub mod versions;

src/python/uninstall.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
use homedir::get_my_home;
2+
use std::fs::remove_dir_all;
3+
4+
use crate::python::versions::get_latest_minor_of_version;
5+
6+
pub async fn uninstall_python(mut version: String) -> Result<(), Box<dyn std::error::Error>> {
7+
version = version.replace("v", "");
8+
let version_string = version.split(".").filter(|&i| i != "").collect::<Vec<_>>();
9+
10+
if version_string.len() == 1 {
11+
version =
12+
get_latest_minor_of_version(version_string[0].to_string(), "".to_string()).await?;
13+
} else if version_string.len() == 2 {
14+
version = get_latest_minor_of_version(
15+
version_string[0].to_string(),
16+
version_string[1].to_string(),
17+
)
18+
.await?;
19+
} else if version_string.len() == 3 {
20+
version = version
21+
} else {
22+
panic!("error invalid version number")
23+
}
24+
25+
if !version.contains("v") {
26+
version = "v".to_owned() + &version
27+
}
28+
29+
let python_path = get_my_home()
30+
.unwrap()
31+
.unwrap()
32+
.as_path()
33+
.join(".vmp")
34+
.join("python")
35+
.join(&version);
36+
37+
if python_path.exists() {
38+
remove_dir_all(python_path)?;
39+
println!("Uninstall for python version {} is successfull", version)
40+
} else {
41+
println!("Python version {} is not installed", version)
42+
}
43+
44+
Ok(())
45+
}

0 commit comments

Comments
 (0)