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

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

pub const params = "<NAME>";
pub const description = "Switches to a 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(
            "No installation by name {s} found, run `zup install {s}`",
            .{ name, name },
        );
        return error.InstallationNotFound;
    }

    const target = blk: {
        const data_home = try xdg.getDataHome(allocator, "zup");
        defer allocator.free(data_home);

        break :blk try std.fs.path.join(allocator, &.{ data_home, name, "zig" });
    };
    defer allocator.free(target);

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

    bin_home.deleteFile("zig") catch {};
    try bin_home.symLink(target, "zig", .{});

    std.log.info("Switched to {s}", .{name});
}