From 595477339b9e7dd696aaa31e21308f48c5a6109d Mon Sep 17 00:00:00 2001 From: Uko Kokņevičs Date: Tue, 26 Apr 2022 00:44:24 +0300 Subject: Add Installation.isActive, use it --- src/Installation.zig | 21 +++++++++++++++------ src/install.zig | 5 +++-- src/list.zig | 2 +- src/remove.zig | 7 +------ 4 files changed, 20 insertions(+), 15 deletions(-) diff --git a/src/Installation.zig b/src/Installation.zig index 3b0382a..fb06824 100644 --- a/src/Installation.zig +++ b/src/Installation.zig @@ -62,12 +62,13 @@ pub fn getActiveName(allocator: Allocator) !?[]u8 { return try allocator.dupe(u8, std.fs.path.dirname(rel_path).?); } -pub fn isInstalled(allocator: Allocator, name: []const u8) !bool { - var zup_data = try xdg.openDataHome(allocator, "zup"); - defer zup_data.close(); - - zup_data.access(name, .{}) catch return false; - return true; +pub fn isActive(allocator: Allocator, name: []const u8) !bool { + const active_name_opt = try getActiveName(allocator); + if (active_name_opt) |active_name| { + defer allocator.free(active_name); + return std.mem.eql(u8, active_name, name); + } + return false; } pub fn getInstalledList(allocator: Allocator) !Installations { @@ -119,6 +120,14 @@ pub fn getInstalledList(allocator: Allocator) !Installations { return installations; } +pub fn isInstalled(allocator: Allocator, name: []const u8) !bool { + var zup_data = try xdg.openDataHome(allocator, "zup"); + defer zup_data.close(); + + zup_data.access(name, .{}) catch return false; + return true; +} + pub fn getAvailableList(allocator: Allocator) !Installations { var json_str = try curl.easyDownload(allocator, "https://ziglang.org/download/index.json"); defer allocator.free(json_str); diff --git a/src/install.zig b/src/install.zig index 7a52d44..ebf1c01 100644 --- a/src/install.zig +++ b/src/install.zig @@ -87,8 +87,9 @@ pub fn perform(allocator: Allocator, name: []const u8, force: bool, available: I } std.log.info("Installed to {s}", .{installation_dir}); - // TODO: Check if it is already active - std.log.info("If you want to use this installation, run `zup switch {s}`", .{name}); + if (!try Installation.isActive(allocator, name)) { + std.log.info("If you want to use this installation, run `zup switch {s}`", .{name}); + } } fn preparePathname(allocator: Allocator, installation_dir: []const u8, source: []const u8) ![:0]u8 { diff --git a/src/list.zig b/src/list.zig index e5daa87..b6e3847 100644 --- a/src/list.zig +++ b/src/list.zig @@ -31,7 +31,7 @@ pub fn main(comptime Result: type, allocator: Allocator, res: Result) !void { } if (list_active) { - var active = try Installation.getActiveName(allocator); + const active = try Installation.getActiveName(allocator); defer if (active) |s| allocator.free(s); // TODO: zig-bug should I really need to do this? try printActive(if (active) |a| a else null); diff --git a/src/remove.zig b/src/remove.zig index c1c35d4..ad1a610 100644 --- a/src/remove.zig +++ b/src/remove.zig @@ -21,12 +21,7 @@ pub fn main(comptime Result: type, allocator: Allocator, res: Result) !void { return error.InstallationNotFound; } - const is_active = blk: { - const active = try Installation.getActiveName(allocator); - defer if (active) |s| allocator.free(s); - - break :blk active != null and std.mem.eql(u8, active.?, name); - }; + const is_active = try Installation.isActive(allocator, name); if (is_active and !res.args.force) { std.log.err("{s} is the active installation, not removing without --force!", .{name}); -- cgit v1.2.3