summaryrefslogtreecommitdiff
path: root/src/remove.zig
blob: bc5b06a3dc5e292d8338d8479d601e6de501ba21 (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
const std = @import("std");
const xdg = @import("xdg");
const zup = @import("root");

const Config = zup.Config;
const Installation = zup.Installation;

pub const params =
    \\-f, --force  Remove even if it is the active installation
    \\<NAME>
;
pub const description = "Removes an installed Zig version.  Run `zup list -i` to see installed <NAME>s.";
pub const min_args = 1;
pub const max_args = 1;

pub fn main(comptime Result: type, config: Config, res: Result) !void {
    const allocator = config.allocator;

    const name = res.positionals[0];

    if (!try Installation.isInstalled(allocator, name)) {
        std.log.err("{s} is not installed!", .{name});
        return error.InstallationNotFound;
    }

    const is_active = try Installation.isActive(allocator, name);

    if (is_active and res.args.force == 0) {
        std.log.err("{s} is the active installation, not removing without --force!", .{name});
        return error.CantRemove;
    }

    var data_home = try xdg.openDataHome(allocator, "zup");
    defer data_home.close();

    std.log.info("Removing {s}...", .{name});
    try data_home.deleteTree(name);

    if (is_active) {
        var bin_home = try xdg.openBinHome(allocator);
        defer bin_home.close();

        try bin_home.deleteFile("zig");
    }

    std.log.info("Done", .{});
}