Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add fallback when set zig version fails #34

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion src/alias.zig
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,31 @@ pub fn setZigVersion(version: []const u8) !void {
const symlinkPath = try std.fs.path.join(allocator, &[_][]const u8{ userHome, ".zm", "current" });
defer allocator.free(symlinkPath);

const previousVersionPath = try getCurrentVersionSymlink(allocator, symlinkPath);
defer allocator.free(previousVersionPath);

try updateSymlink(zigPath, symlinkPath);
try verifyZigVersion(allocator, version);
verifyZigVersion(allocator, version) catch |err| switch (err) {
error.WrongVersion => {
std.debug.print("Failed to set Zig version {s}. Reverting to previous version.\n", .{version});
try updateSymlink(previousVersionPath, symlinkPath);
std.debug.print("Reverted to previous Zig version successfully.\n", .{});
},
else => return err,
};
}

fn getCurrentVersionSymlink(allocator: std.mem.Allocator, symlinkPath: []const u8) ![]u8 {
var buffer: [1000]u8 = undefined;

if (doesFileExist(symlinkPath)) {
const linkTarget = try std.fs.cwd().readLink(symlinkPath, &buffer);
// Allocate memory to return a copy of the link target
const linkTargetCopy = try allocator.dupe(u8, linkTarget);
return linkTargetCopy;
}
// Return an empty string if no symlink exists
return try allocator.dupe(u8, "");
}

fn getUserHome() []const u8 {
Expand Down Expand Up @@ -48,6 +71,7 @@ fn verifyZigVersion(allocator: std.mem.Allocator, expectedVersion: []const u8) !

if (!std.mem.eql(u8, expectedVersion, actualVersion)) {
std.debug.print("Expected Zig version {s}, but currently using {s}. Please check.\n", .{ expectedVersion, actualVersion });
return error.WrongVersion;
} else {
std.debug.print("Now using Zig version {s}\n", .{expectedVersion});
}
Expand Down
Loading