diff options
Diffstat (limited to 'src/remove.zig')
| -rw-r--r-- | src/remove.zig | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/src/remove.zig b/src/remove.zig new file mode 100644 index 0000000..c1c35d4 --- /dev/null +++ b/src/remove.zig | |||
| @@ -0,0 +1,50 @@ | |||
| 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 = | ||
| 9 | \\-f, --force Remove even if it is the active installation | ||
| 10 | \\<NAME> | ||
| 11 | ; | ||
| 12 | pub const description = "Removes an installed Zig version. Run `zup list -i` to see installed <NAME>s."; | ||
| 13 | pub const min_args = 1; | ||
| 14 | pub const max_args = 1; | ||
| 15 | |||
| 16 | pub fn main(comptime Result: type, allocator: Allocator, res: Result) !void { | ||
| 17 | const name = res.positionals[0]; | ||
| 18 | |||
| 19 | if (!try Installation.isInstalled(allocator, name)) { | ||
| 20 | std.log.err("{s} is not installed!", .{name}); | ||
| 21 | return error.InstallationNotFound; | ||
| 22 | } | ||
| 23 | |||
| 24 | const is_active = blk: { | ||
| 25 | const active = try Installation.getActiveName(allocator); | ||
| 26 | defer if (active) |s| allocator.free(s); | ||
| 27 | |||
| 28 | break :blk active != null and std.mem.eql(u8, active.?, name); | ||
| 29 | }; | ||
| 30 | |||
| 31 | if (is_active and !res.args.force) { | ||
| 32 | std.log.err("{s} is the active installation, not removing without --force!", .{name}); | ||
| 33 | return error.CantRemove; | ||
| 34 | } | ||
| 35 | |||
| 36 | var data_home = try xdg.openDataHome(allocator, "zup"); | ||
| 37 | defer data_home.close(); | ||
| 38 | |||
| 39 | std.log.info("Removing {s}...", .{name}); | ||
| 40 | try data_home.deleteTree(name); | ||
| 41 | |||
| 42 | if (is_active) { | ||
| 43 | var bin_home = try xdg.openBinHome(allocator); | ||
| 44 | defer bin_home.close(); | ||
| 45 | |||
| 46 | try bin_home.deleteFile("zig"); | ||
| 47 | } | ||
| 48 | |||
| 49 | std.log.info("Done", .{}); | ||
| 50 | } | ||