diff options
| author | 2022-04-26 00:44:24 +0300 | |
|---|---|---|
| committer | 2022-04-26 00:44:24 +0300 | |
| commit | 595477339b9e7dd696aaa31e21308f48c5a6109d (patch) | |
| tree | 96dca0017a60c40dd5437645cb11d8ff92318baf | |
| parent | Add a missing const (diff) | |
| download | zup-595477339b9e7dd696aaa31e21308f48c5a6109d.tar.gz zup-595477339b9e7dd696aaa31e21308f48c5a6109d.tar.xz zup-595477339b9e7dd696aaa31e21308f48c5a6109d.zip | |
Add Installation.isActive, use it
| -rw-r--r-- | src/Installation.zig | 21 | ||||
| -rw-r--r-- | src/install.zig | 5 | ||||
| -rw-r--r-- | src/list.zig | 2 | ||||
| -rw-r--r-- | 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 { | |||
| 62 | return try allocator.dupe(u8, std.fs.path.dirname(rel_path).?); | 62 | return try allocator.dupe(u8, std.fs.path.dirname(rel_path).?); |
| 63 | } | 63 | } |
| 64 | 64 | ||
| 65 | pub fn isInstalled(allocator: Allocator, name: []const u8) !bool { | 65 | pub fn isActive(allocator: Allocator, name: []const u8) !bool { |
| 66 | var zup_data = try xdg.openDataHome(allocator, "zup"); | 66 | const active_name_opt = try getActiveName(allocator); |
| 67 | defer zup_data.close(); | 67 | if (active_name_opt) |active_name| { |
| 68 | 68 | defer allocator.free(active_name); | |
| 69 | zup_data.access(name, .{}) catch return false; | 69 | return std.mem.eql(u8, active_name, name); |
| 70 | return true; | 70 | } |
| 71 | return false; | ||
| 71 | } | 72 | } |
| 72 | 73 | ||
| 73 | pub fn getInstalledList(allocator: Allocator) !Installations { | 74 | pub fn getInstalledList(allocator: Allocator) !Installations { |
| @@ -119,6 +120,14 @@ pub fn getInstalledList(allocator: Allocator) !Installations { | |||
| 119 | return installations; | 120 | return installations; |
| 120 | } | 121 | } |
| 121 | 122 | ||
| 123 | pub fn isInstalled(allocator: Allocator, name: []const u8) !bool { | ||
| 124 | var zup_data = try xdg.openDataHome(allocator, "zup"); | ||
| 125 | defer zup_data.close(); | ||
| 126 | |||
| 127 | zup_data.access(name, .{}) catch return false; | ||
| 128 | return true; | ||
| 129 | } | ||
| 130 | |||
| 122 | pub fn getAvailableList(allocator: Allocator) !Installations { | 131 | pub fn getAvailableList(allocator: Allocator) !Installations { |
| 123 | var json_str = try curl.easyDownload(allocator, "https://ziglang.org/download/index.json"); | 132 | var json_str = try curl.easyDownload(allocator, "https://ziglang.org/download/index.json"); |
| 124 | defer allocator.free(json_str); | 133 | 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 | |||
| 87 | } | 87 | } |
| 88 | 88 | ||
| 89 | std.log.info("Installed to {s}", .{installation_dir}); | 89 | std.log.info("Installed to {s}", .{installation_dir}); |
| 90 | // TODO: Check if it is already active | 90 | if (!try Installation.isActive(allocator, name)) { |
| 91 | std.log.info("If you want to use this installation, run `zup switch {s}`", .{name}); | 91 | std.log.info("If you want to use this installation, run `zup switch {s}`", .{name}); |
| 92 | } | ||
| 92 | } | 93 | } |
| 93 | 94 | ||
| 94 | fn preparePathname(allocator: Allocator, installation_dir: []const u8, source: []const u8) ![:0]u8 { | 95 | 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 { | |||
| 31 | } | 31 | } |
| 32 | 32 | ||
| 33 | if (list_active) { | 33 | if (list_active) { |
| 34 | var active = try Installation.getActiveName(allocator); | 34 | const active = try Installation.getActiveName(allocator); |
| 35 | defer if (active) |s| allocator.free(s); | 35 | defer if (active) |s| allocator.free(s); |
| 36 | // TODO: zig-bug should I really need to do this? | 36 | // TODO: zig-bug should I really need to do this? |
| 37 | try printActive(if (active) |a| a else null); | 37 | 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 { | |||
| 21 | return error.InstallationNotFound; | 21 | return error.InstallationNotFound; |
| 22 | } | 22 | } |
| 23 | 23 | ||
| 24 | const is_active = blk: { | 24 | const is_active = try Installation.isActive(allocator, name); |
| 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 | 25 | ||
| 31 | if (is_active and !res.args.force) { | 26 | if (is_active and !res.args.force) { |
| 32 | std.log.err("{s} is the active installation, not removing without --force!", .{name}); | 27 | std.log.err("{s} is the active installation, not removing without --force!", .{name}); |