diff options
| author | 2022-04-25 05:09:55 +0300 | |
|---|---|---|
| committer | 2022-04-25 23:34:05 +0300 | |
| commit | d303b53f2ced75703bf022a5d337ee3ba530b288 (patch) | |
| tree | f2e64057120d01ee8a821596ea01b8fc37c53c2c /src/switch.zig | |
| download | zup-0.1.0.tar.gz zup-0.1.0.tar.xz zup-0.1.0.zip | |
Initial commit0.1.0
Diffstat (limited to 'src/switch.zig')
| -rw-r--r-- | src/switch.zig | 38 |
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 @@ | |||
| 1 | const std = @import("std"); | ||
| 2 | const xdg = @import("xdg"); | ||
| 3 | const zup = @import("zup"); | ||
| 4 | |||
| 5 | const Allocator = std.mem.Allocator; | ||
| 6 | const Installation = zup.Installation; | ||
| 7 | |||
| 8 | pub const params = "<NAME>"; | ||
| 9 | pub const description = "Switches to a Zig version. Run `zup list -i` to see installed <NAME>s."; | ||
| 10 | pub const min_args = 1; | ||
| 11 | pub const max_args = 1; | ||
| 12 | |||
| 13 | pub 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 | } | ||