summaryrefslogtreecommitdiff
path: root/src/switch.zig
diff options
context:
space:
mode:
authorGravatar Uko Kokņevičs2022-04-25 05:09:55 +0300
committerGravatar Uko Kokņevičs2022-04-25 23:34:05 +0300
commitd303b53f2ced75703bf022a5d337ee3ba530b288 (patch)
treef2e64057120d01ee8a821596ea01b8fc37c53c2c /src/switch.zig
downloadzup-d303b53f2ced75703bf022a5d337ee3ba530b288.tar.gz
zup-d303b53f2ced75703bf022a5d337ee3ba530b288.tar.xz
zup-d303b53f2ced75703bf022a5d337ee3ba530b288.zip
Initial commit0.1.0
Diffstat (limited to 'src/switch.zig')
-rw-r--r--src/switch.zig38
1 files changed, 38 insertions, 0 deletions
diff --git a/src/switch.zig b/src/switch.zig
new file mode 100644
index 0000000..058067d
--- /dev/null
+++ b/src/switch.zig
@@ -0,0 +1,38 @@
1const std = @import("std");
2const xdg = @import("xdg");
3const zup = @import("zup");
4
5const Allocator = std.mem.Allocator;
6const Installation = zup.Installation;
7
8pub const params = "<NAME>";
9pub const description = "Switches to a Zig version. Run `zup list -i` to see installed <NAME>s.";
10pub const min_args = 1;
11pub const max_args = 1;
12
13pub fn main(comptime Result: type, allocator: Allocator, res: Result) !void {
14 const name = res.positionals[0];
15 if (!try Installation.isInstalled(allocator, name)) {
16 std.log.err(
17 "No installation by name {s} found, run `zup install {s}`",
18 .{ name, name },
19 );
20 return error.InstallationNotFound;
21 }
22
23 const target = blk: {
24 const data_home = try xdg.getDataHome(allocator, "zup");
25 defer allocator.free(data_home);
26
27 break :blk try std.fs.path.join(allocator, &.{ data_home, name, "zig" });
28 };
29 defer allocator.free(target);
30
31 var bin_home = try xdg.openBinHome(allocator);
32 defer bin_home.close();
33
34 bin_home.deleteFile("zig") catch {};
35 try bin_home.symLink(target, "zig", .{});
36
37 std.log.info("Switched to {s}", .{name});
38}