From 0350e1502222f7305cc9c3ff532003e8ed2b7e03 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hendrik=20Niel=C3=A4nder?= Date: Mon, 2 Dec 2024 17:19:32 +0100 Subject: [PATCH] chore: use stdout instead of std.debug.print --- src/alias.zig | 33 ++++++++++++++---------- src/command.zig | 59 ++++++++++++++++++++++++------------------- src/install.zig | 3 ++- src/util/minisign.zig | 18 ++++++++----- 4 files changed, 67 insertions(+), 46 deletions(-) diff --git a/src/alias.zig b/src/alias.zig index 00769e1..fdb81fd 100644 --- a/src/alias.zig +++ b/src/alias.zig @@ -1,6 +1,6 @@ //! 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"); @@ -8,9 +8,9 @@ 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(); @@ -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); }; @@ -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}); } } diff --git a/src/command.zig b/src/command.zig index 2ac00ab..5d58060 100644 --- a/src/command.zig +++ b/src/command.zig @@ -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(), } } @@ -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; @@ -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; } } @@ -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}); } } @@ -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 '.\n", .{scmd}); + const error_message = "Unknown subcommand '{s}'. Use 'install zig/zls '.\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 '.\n", .{}); + const error_message = "Please specify a version to install: 'install {s} '.\n"; + try std.io.getStdErr().writer().print(error_message, .{scmd}); return; }; @@ -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 ' or 'install '.\n", .{}); + const error_message = "Please specify a version to install: 'install zig/zls ' or 'install '.\n"; + try std.io.getStdErr().writer().print(error_message, .{}); } } @@ -219,12 +225,14 @@ 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 ' or 'use zls '.\n", .{scmd}); + const error_message = "Unknown subcommand '{s}'. Use 'use zig ' or 'use zls '.\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 '.\n", .{}); + const error_message = "Please specify a version to use: 'use {s} '.\n"; + try std.io.getStdErr().writer().print(error_message, .{scmd}); return; }; @@ -232,10 +240,9 @@ fn use_version(subcmd: ?[]const u8, param: ?[]const u8) !void { } 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 ' or 'use '.\n", .{}); + const error_message = "Please specify a version to use: 'use zig/zls ' or 'use '.\n"; + try std.io.getStdErr().writer().print(error_message, .{}); } } @@ -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 ' or 'remove zls '.\n", .{scmd}); + const error_message = "Unknown subcommand '{s}'. Use 'remove zig ' or 'remove zls '.\n"; + try std.io.getStdErr().writer().print(error_message, .{scmd}); return; } const version = param orelse { - std.debug.print("Please specify a version: 'remove zig ' or 'remove zls '.\n", .{}); + const error_message = "Please specify a version: 'remove {s} '.\n"; + try std.io.getStdErr().writer().print(error_message, .{scmd}); return; }; @@ -264,24 +273,23 @@ 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 ' or 'remove '.\n", .{}); + const error_message = "Please specify a version to remove: 'remove zig/zls ' or 'remove '.\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:"); @@ -289,8 +297,7 @@ fn display_help() void { 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 [args]\n\n" ++ commands_title ++ "\n ls, list List all available versions of Zig or zls.\n" ++ @@ -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 { diff --git a/src/install.zig b/src/install.zig index 6f65347..61030ce 100644 --- a/src/install.zig +++ b/src/install.zig @@ -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; } diff --git a/src/util/minisign.zig b/src/util/minisign.zig index 4afd001..7d51c6a 100644 --- a/src/util/minisign.zig +++ b/src/util/minisign.zig @@ -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"); @@ -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..]; @@ -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"); @@ -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; } @@ -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; }