Skip to content

Commit

Permalink
chore: use stdout instead of std.debug.print
Browse files Browse the repository at this point in the history
  • Loading branch information
hendriknielaender committed Dec 2, 2024
1 parent fec03bf commit 0350e15
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 46 deletions.
33 changes: 20 additions & 13 deletions src/alias.zig
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
//! This file is used to create soft links and verify version
//! for Windows, we will use copy dir(when Windows create soft link it requires admin)
//! for set version
//! for Windows, we will use copy dir (creating symlinks requires admin privileges)
//! for setting versions.
const std = @import("std");
const assert = std.debug.assert;
const builtin = @import("builtin");
const config = @import("config.zig");
const util_data = @import("util/data.zig");
const util_tool = @import("util/tool.zig");

/// try to set zig version
/// this will use system link on unix-like
/// for windows, this will use copy dir
/// Try to set the Zig version.
/// This will use a symlink on Unix-like systems.
/// For Windows, this will copy the directory.
pub fn set_version(version: []const u8, is_zls: bool) !void {
var arena = std.heap.ArenaAllocator.init(util_data.get_allocator());
defer arena.deinit();
Expand All @@ -33,7 +33,8 @@ pub fn set_version(version: []const u8, is_zls: bool) !void {
if (err != error.FileNotFound)
return err;

std.debug.print("zig version {s} is not installed. Please install it before proceeding.\n", .{version});
const err_msg = "{s} version {s} is not installed. Please install it before proceeding.\n";
try std.io.getStdErr().writer().print(err_msg, .{ if (is_zls) "zls" else "Zig", version });
std.process.exit(1);
};

Expand Down Expand Up @@ -67,32 +68,38 @@ fn update_current(zig_path: []const u8, symlink_path: []const u8) !void {
try std.posix.symlink(zig_path, symlink_path);
}

/// Verify current zig version
/// Verify the current Zig version
fn verify_zig_version(expected_version: []const u8) !void {
const allocator = util_data.get_allocator();

const actual_version = try util_data.get_current_version(allocator, false);
defer allocator.free(actual_version);

var stdout = std.io.getStdOut().writer();

if (std.mem.eql(u8, expected_version, "master")) {
std.debug.print("Now using Zig version {s}\n", .{actual_version});
try stdout.print("Now using Zig version {s}\n", .{actual_version});
} else if (!std.mem.eql(u8, expected_version, actual_version)) {
std.debug.print("Expected Zig version {s}, but currently using {s}. Please check.\n", .{ expected_version, actual_version });
const err_msg = "Expected Zig version {s}, but currently using {s}. Please check.\n";
try std.io.getStdErr().writer().print(err_msg, .{ expected_version, actual_version });
} else {
std.debug.print("Now using Zig version {s}\n", .{expected_version});
try stdout.print("Now using Zig version {s}\n", .{expected_version});
}
}

/// verify current zig version
/// Verify the current zls version
fn verify_zls_version(expected_version: []const u8) !void {
const allocator = util_data.get_allocator();

const actual_version = try util_data.get_current_version(allocator, true);
defer allocator.free(actual_version);

var stdout = std.io.getStdOut().writer();

if (!std.mem.eql(u8, expected_version, actual_version)) {
std.debug.print("Expected zls version {s}, but currently using {s}. Please check.\n", .{ expected_version, actual_version });
const err_msg = "Expected zls version {s}, but currently using {s}. Please check.\n";
try std.io.getStdErr().writer().print(err_msg, .{ expected_version, actual_version });
} else {
std.debug.print("Now using zls version {s}\n", .{expected_version});
try stdout.print("Now using zls version {s}\n", .{expected_version});
}
}
59 changes: 33 additions & 26 deletions src/command.zig
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ pub fn handle_command(params: []const []const u8, root_node: std.Progress.Node)
.Install => try install_version(command.subcmd, command.param, root_node),
.Use => try use_version(command.subcmd, command.param),
.Remove => try remove_version(command.subcmd, command.param),
.Version => get_version(),
.Help => display_help(),
.Version => try get_version(),
.Help => try display_help(),
.Unknown => try handle_unknown(),
}
}
Expand Down Expand Up @@ -132,7 +132,8 @@ pub fn handle_alias(params: []const []const u8) !void {

std.fs.accessAbsolute(current_path, .{}) catch |err| {
if (err == std.fs.Dir.AccessError.FileNotFound) {
std.debug.print("{s} has not been installed yet, please install it fist!\n", .{if (is_zls) "zls" else "Zig"});
const error_message = "{s} has not been installed yet, please install it first!\n";
try std.io.getStdErr().writer().print(error_message, .{if (is_zls) "zls" else "Zig"});
std.process.exit(1);
}
return err;
Expand Down Expand Up @@ -160,7 +161,8 @@ fn handle_list(param: ?[]const u8) !void {
} else
// when not zig
if (!util_tool.eql_str(p, "zig")) {
std.debug.print("Error param, you can specify zig or zls\n", .{});
const error_message = "Invalid parameter '{s}'. You can specify 'zig' or 'zls'.\n";
try std.io.getStdErr().writer().print(error_message, .{p});
return;
}
}
Expand All @@ -178,8 +180,9 @@ fn handle_list(param: ?[]const u8) !void {

defer util_tool.free_str_array(version_list, allocator);

var stdout = std.io.getStdOut().writer();
for (version_list) |version| {
std.debug.print("{s}\n", .{version});
try stdout.print("{s}\n", .{version});
}
}

Expand All @@ -192,12 +195,14 @@ fn install_version(subcmd: ?[]const u8, param: ?[]const u8, root_node: std.Progr
} else if (util_tool.eql_str(scmd, "zls")) {
is_zls = true;
} else {
std.debug.print("Unknown subcommand '{s}'. Use 'install zig/zls <version>'.\n", .{scmd});
const error_message = "Unknown subcommand '{s}'. Use 'install zig/zls <version>'.\n";
try std.io.getStdErr().writer().print(error_message, .{scmd});
return;
}

const version = param orelse {
std.debug.print("Please specify a version to install: 'install zig/zls <version>'.\n", .{});
const error_message = "Please specify a version to install: 'install {s} <version>'.\n";
try std.io.getStdErr().writer().print(error_message, .{scmd});
return;
};

Expand All @@ -206,7 +211,8 @@ fn install_version(subcmd: ?[]const u8, param: ?[]const u8, root_node: std.Progr
// set zig version
try install.install(version, false, root_node);
} else {
std.debug.print("Please specify a version to install: 'install zig/zls <version>' or 'install <version>'.\n", .{});
const error_message = "Please specify a version to install: 'install zig/zls <version>' or 'install <version>'.\n";
try std.io.getStdErr().writer().print(error_message, .{});
}
}

Expand All @@ -219,23 +225,24 @@ fn use_version(subcmd: ?[]const u8, param: ?[]const u8) !void {
} else if (util_tool.eql_str(scmd, "zls")) {
is_zls = true;
} else {
std.debug.print("Unknown subcommand '{s}'. Use 'use zig <version>' or 'use zls <version>'.\n", .{scmd});
const error_message = "Unknown subcommand '{s}'. Use 'use zig <version>' or 'use zls <version>'.\n";
try std.io.getStdErr().writer().print(error_message, .{scmd});
return;
}

const version = param orelse {
std.debug.print("Please specify a version to use: 'use zig/zls <version>'.\n", .{});
const error_message = "Please specify a version to use: 'use {s} <version>'.\n";
try std.io.getStdErr().writer().print(error_message, .{scmd});
return;
};

try alias.set_version(version, is_zls);
} else if (param) |version| {
// set zig version
try alias.set_version(version, false);
// set zls version
// try alias.set_version(version, true);
} else {
std.debug.print("Please specify a version to use: 'use zig/zls <version>' or 'use <version>'.\n", .{});
const error_message = "Please specify a version to use: 'use zig/zls <version>' or 'use <version>'.\n";
try std.io.getStdErr().writer().print(error_message, .{});
}
}

Expand All @@ -248,12 +255,14 @@ fn remove_version(subcmd: ?[]const u8, param: ?[]const u8) !void {
} else if (util_tool.eql_str(scmd, "zls")) {
is_zls = true;
} else {
std.debug.print("Unknown subcommand '{s}'. Use 'remove zig <version>' or 'remove zls <version>'.\n", .{scmd});
const error_message = "Unknown subcommand '{s}'. Use 'remove zig <version>' or 'remove zls <version>'.\n";
try std.io.getStdErr().writer().print(error_message, .{scmd});
return;
}

const version = param orelse {
std.debug.print("Please specify a version: 'remove zig <version>' or 'remove zls <version>'.\n", .{});
const error_message = "Please specify a version: 'remove {s} <version>'.\n";
try std.io.getStdErr().writer().print(error_message, .{scmd});
return;
};

Expand All @@ -264,33 +273,31 @@ fn remove_version(subcmd: ?[]const u8, param: ?[]const u8) !void {
// set zls version
try remove.remove(version, true);
} else {
std.debug.print("Please specify a version to use: 'remove zig/zls <version>' or 'remove <version>'.\n", .{});
const error_message = "Please specify a version to remove: 'remove zig/zls <version>' or 'remove <version>'.\n";
try std.io.getStdErr().writer().print(error_message, .{});
}
}

fn set_default() !void {
std.debug.print("Handling 'default' command.\n", .{});
// Your default code here
const default_message = "Handling 'default' command.\n";
try std.io.getStdOut().writer().print("{s}", .{default_message});
}

fn get_version() void {
fn get_version() !void {
comptime var color = util_color.Style.init();

const version_message = color.cyan().fmt("zvm " ++ options.version ++ "\n");

std.debug.print("{s}", .{version_message});
try std.io.getStdOut().writer().print("{s}", .{version_message});
}

fn display_help() void {
fn display_help() !void {
comptime var color = util_color.Style.init();

const usage_title = color.bold().magenta().fmt("Usage:");
const commands_title = color.bold().magenta().fmt("Commands:");
const examples_title = color.bold().magenta().fmt("Examples:");
const additional_info_title = color.bold().magenta().fmt("Additional Information:");

const help_message =
usage_title ++
const help_message = usage_title ++
"\n zvm <command> [args]\n\n" ++
commands_title ++
"\n ls, list List all available versions of Zig or zls.\n" ++
Expand All @@ -308,7 +315,7 @@ fn display_help() void {
additional_info_title ++
"\n For additional information and contributions, please visit https://github.com/hendriknielaender/zvm\n\n";

std.debug.print("{s}", .{help_message});
try std.io.getStdOut().writer().print("{s}", .{help_message});
}

fn handle_unknown() !void {
Expand Down
3 changes: 2 additions & 1 deletion src/install.zig
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,8 @@ fn install_zig(version: []const u8, root_node: Progress.Node) !void {
fn install_zls(version: []const u8) !void {
const true_version = blk: {
if (util_tool.eql_str("master", version)) {
std.debug.print("Sorry, the 'install zls' feature is not supported at this time. Please compile zls locally.", .{});
const zls_message = "Sorry, the 'install zls' feature is not supported at this time. Please compile zls locally.";
try std.io.getStdOut().writer().print("{s}", .{zls_message});
return;
}

Expand Down
18 changes: 12 additions & 6 deletions src/util/minisign.zig
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ pub const Signature = struct {
var line: []const u8 = undefined;
while (true) {
line = tokenizer.next() orelse {
std.debug.print("No more lines to read. Invalid encoding.\n", .{});
const err_msg = "No more lines to read. Invalid encoding.\n";
try std.io.getStdErr().writer().print(err_msg, .{});
return Error.invalid_encoding;
};
const trimmed_line = mem.trim(u8, line, " \t\r\n");
Expand All @@ -70,14 +71,16 @@ pub const Signature = struct {

// Decode trusted comment
const comment_line = tokenizer.next() orelse {
std.debug.print("Expected trusted comment line but none found.\n", .{});
const err_msg = "Expected trusted comment line but none found.\n";
try std.io.getStdErr().writer().print(err_msg, .{});
return Error.invalid_encoding;
};
const comment_line_trimmed = mem.trim(u8, comment_line, " \t\r\n");

const trusted_comment_prefix = "trusted comment: ";
if (!mem.startsWith(u8, comment_line_trimmed, trusted_comment_prefix)) {
std.debug.print("Trusted comment line does not start with the expected prefix.\n", .{});
const err_msg = "Trusted comment line does not start with the expected prefix.\n";
try std.io.getStdErr().writer().print(err_msg, .{});
return Error.invalid_encoding;
}
const trusted_comment_slice = comment_line_trimmed[trusted_comment_prefix.len..];
Expand All @@ -87,7 +90,8 @@ pub const Signature = struct {

// Decode global signature
const global_sig_line = tokenizer.next() orelse {
std.debug.print("Expected global signature line but none found.\n", .{});
const err_msg = "Expected global signature line but none found.\n";
try std.io.getStdErr().writer().print(err_msg, .{});
return Error.invalid_encoding;
};
const global_sig_line_trimmed = mem.trim(u8, global_sig_line, " \t\r\n");
Expand Down Expand Up @@ -126,7 +130,8 @@ pub const PublicKey = struct {
const trimmed_str = std.mem.trim(u8, str, " \t\r\n");

if (trimmed_str.len != 56) { // Base64 for 42-byte key
std.debug.print("Error: Public key string length is {d}, expected 56.\n", .{trimmed_str.len});
const err_msg = "Error: Public key string length is {d}, expected 56.\n";
try std.io.getStdErr().writer().print(err_msg, .{trimmed_str.len});
return Error.public_key_format_error;
}

Expand All @@ -136,7 +141,8 @@ pub const PublicKey = struct {
const signature_algorithm = bin[0..2];

if (bin[0] != 0x45 or (bin[1] != 0x64 and bin[1] != 0x44)) {
std.debug.print("Unsupported signature algorithm: {x}\n", .{signature_algorithm});
const err_msg = "Unsupported signature algorithm: {any}\n";
try std.io.getStdErr().writer().print(err_msg, .{signature_algorithm});
return Error.unsupported_algorithm;
}

Expand Down

0 comments on commit 0350e15

Please sign in to comment.