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
#[cfg(not(windows))]
use crate::operations::remove_symlink;
use crate::{
    config_file::{load_mut_config_db, save_config_db, JuliaupConfigChannel},
    global_paths::GlobalPaths,
    operations::garbage_collect_versions,
};
use anyhow::{bail, Context, Result};

pub fn run_command_remove(channel: &str, paths: &GlobalPaths) -> Result<()> {
    let mut config_file = load_mut_config_db(paths)
        .with_context(|| "`remove` command failed to load configuration data.")?;

    if !config_file.data.installed_channels.contains_key(channel) {
        bail!(
            "'{}' cannot be removed because it is currently not installed.",
            channel
        );
    }

    if let Some(ref default_value) = config_file.data.default {
        if channel == default_value {
            bail!(
                "'{}' cannot be removed because it is currently configured as the default channel.",
                channel
            );
        }
    }

    if config_file
        .data
        .overrides
        .iter()
        .any(|i| i.channel == channel)
    {
        bail!(
            "'{}' cannot be removed because it is currently used in a directory override.",
            channel
        );
    }

    let x = config_file.data.installed_channels.get(channel).unwrap();

    if let JuliaupConfigChannel::DirectDownloadChannel {
        path,
        url: _,
        local_etag: _,
        server_etag: _,
        version: _,
    } = x
    {
        let path_to_delete = paths.juliauphome.join(&path);

        let display = path_to_delete.display();

        if std::fs::remove_dir_all(&path_to_delete).is_err() {
            eprintln!("WARNING: Failed to delete {}. You can try to delete at a later point by running `juliaup gc`.", display)
        }
    };

    config_file.data.installed_channels.remove(channel);

    #[cfg(not(windows))]
    remove_symlink(&format!("julia-{}", channel))?;

    garbage_collect_versions(&mut config_file.data, paths)?;

    save_config_db(&mut config_file).with_context(|| {
        format!(
            "Failed to save configuration file from `remove` command after '{}' was installed.",
            channel
        )
    })?;

    eprintln!("Julia '{}' successfully removed.", channel);

    Ok(())
}