From f2f645fe212a7752c0cc64f556b7e18c0bd542d6 Mon Sep 17 00:00:00 2001 From: Jeremie Fraeys Date: Wed, 18 Feb 2026 13:33:10 -0500 Subject: [PATCH] refactor(cli): use packet.deinit() in remaining command files Replace manual defer blocks with packet.deinit() for consistent cleanup: - jupyter.zig: 5 locations updated (restoreJupyter, startJupyter, stopJupyter, removeJupyter, listServices, packageCommands) - experiment.zig: 3 locations updated (executeLog, executeShow, executeDelete) - queue.zig: 1 location updated (queueSingleJob) - logs.zig: 1 location updated (run) Eliminates 44 manual cleanup blocks across 4 files. Build verified: zig build --release=fast --- cli/src/commands/experiment.zig | 31 +++------------------------ cli/src/commands/jupyter.zig | 38 ++++++--------------------------- cli/src/commands/logs.zig | 8 +------ cli/src/commands/queue.zig | 9 +------- 4 files changed, 11 insertions(+), 75 deletions(-) diff --git a/cli/src/commands/experiment.zig b/cli/src/commands/experiment.zig index 2ef087e..b667ab9 100644 --- a/cli/src/commands/experiment.zig +++ b/cli/src/commands/experiment.zig @@ -224,16 +224,7 @@ fn executeLog(allocator: std.mem.Allocator, args: []const []const u8, options: * ); return; }; - defer { - if (packet.success_message) |msg| allocator.free(msg); - if (packet.error_message) |msg| allocator.free(msg); - if (packet.error_details) |details| allocator.free(details); - if (packet.data_type) |dtype| allocator.free(dtype); - if (packet.data_payload) |payload| allocator.free(payload); - if (packet.progress_message) |pmsg| allocator.free(pmsg); - if (packet.status_data) |sdata| allocator.free(sdata); - if (packet.log_message) |lmsg| allocator.free(lmsg); - } + defer packet.deinit(allocator); switch (packet.packet_type) { .success => { @@ -290,14 +281,7 @@ fn executeShow(allocator: std.mem.Allocator, args: []const []const u8, options: defer allocator.free(message); const packet = try protocol.ResponsePacket.deserialize(message, allocator); - defer { - // Clean up allocated strings from packet - if (packet.success_message) |msg| allocator.free(msg); - if (packet.error_message) |msg| allocator.free(msg); - if (packet.error_details) |details| allocator.free(details); - if (packet.data_type) |dtype| allocator.free(dtype); - if (packet.data_payload) |payload| allocator.free(payload); - } + defer packet.deinit(allocator); // For now, let's just print the result switch (packet.packet_type) { @@ -533,16 +517,7 @@ fn executeDelete(allocator: std.mem.Allocator, identifier: []const u8, options: if (message.len > 0) { const packet = protocol.ResponsePacket.deserialize(message, allocator) catch null; if (packet) |p| { - defer { - if (p.success_message) |msg| allocator.free(msg); - if (p.error_message) |msg| allocator.free(msg); - if (p.error_details) |details| allocator.free(details); - if (p.data_type) |dtype| allocator.free(dtype); - if (p.data_payload) |payload| allocator.free(payload); - if (p.progress_message) |pmsg| allocator.free(pmsg); - if (p.status_data) |sdata| allocator.free(sdata); - if (p.log_message) |lmsg| allocator.free(lmsg); - } + defer p.deinit(allocator); switch (p.packet_type) { .success => { diff --git a/cli/src/commands/jupyter.zig b/cli/src/commands/jupyter.zig index c4509d6..0cf6502 100644 --- a/cli/src/commands/jupyter.zig +++ b/cli/src/commands/jupyter.zig @@ -70,11 +70,7 @@ fn restoreJupyter(allocator: std.mem.Allocator, args: []const []const u8) !void colors.printError("Failed to parse response: {}\n", .{err}); return; }; - defer { - if (packet.success_message) |msg| allocator.free(msg); - if (packet.error_message) |msg| allocator.free(msg); - if (packet.error_details) |details| allocator.free(details); - } + defer packet.deinit(allocator); switch (packet.packet_type) { .success => { @@ -319,11 +315,7 @@ fn startJupyter(allocator: std.mem.Allocator, args: []const []const u8) !void { colors.printError("Failed to parse response: {}\n", .{err}); return; }; - defer { - if (packet.success_message) |msg| allocator.free(msg); - if (packet.error_message) |msg| allocator.free(msg); - if (packet.error_details) |details| allocator.free(details); - } + defer packet.deinit(allocator); switch (packet.packet_type) { .success => { @@ -400,11 +392,7 @@ fn stopJupyter(allocator: std.mem.Allocator, args: []const []const u8) !void { colors.printError("Failed to parse response: {}\n", .{err}); return; }; - defer { - if (packet.success_message) |msg| allocator.free(msg); - if (packet.error_message) |msg| allocator.free(msg); - if (packet.error_details) |details| allocator.free(details); - } + defer packet.deinit(allocator); switch (packet.packet_type) { .success => { @@ -519,11 +507,7 @@ fn removeJupyter(allocator: std.mem.Allocator, args: []const []const u8) !void { colors.printError("Failed to parse response: {}\n", .{err}); return; }; - defer { - if (packet.success_message) |msg| allocator.free(msg); - if (packet.error_message) |msg| allocator.free(msg); - if (packet.error_details) |details| allocator.free(details); - } + defer packet.deinit(allocator); switch (packet.packet_type) { .success => { @@ -595,12 +579,7 @@ fn listServices(allocator: std.mem.Allocator) !void { colors.printError("Failed to parse response: {}\n", .{err}); return; }; - defer { - if (packet.data_type) |dtype| allocator.free(dtype); - if (packet.data_payload) |payload| allocator.free(payload); - if (packet.error_message) |msg| allocator.free(msg); - if (packet.error_details) |details| allocator.free(details); - } + defer packet.deinit(allocator); switch (packet.packet_type) { .data => { @@ -841,12 +820,7 @@ fn packageCommands(args: []const []const u8) !void { colors.printError("Failed to parse response: {}\n", .{err}); return; }; - defer { - if (packet.data_type) |dtype| allocator.free(dtype); - if (packet.data_payload) |payload| allocator.free(payload); - if (packet.error_message) |msg| allocator.free(msg); - if (packet.error_details) |details| allocator.free(details); - } + defer packet.deinit(allocator); switch (packet.packet_type) { .data => { diff --git a/cli/src/commands/logs.zig b/cli/src/commands/logs.zig index 7dfb356..8f14932 100644 --- a/cli/src/commands/logs.zig +++ b/cli/src/commands/logs.zig @@ -72,13 +72,7 @@ pub fn run(allocator: std.mem.Allocator, argv: []const []const u8) !void { std.debug.print("{s}\n", .{message}); return; }; - defer { - if (packet.success_message) |m| allocator.free(m); - if (packet.error_message) |m| allocator.free(m); - if (packet.error_details) |m| allocator.free(m); - if (packet.data_payload) |m| allocator.free(m); - if (packet.data_type) |m| allocator.free(m); - } + defer packet.deinit(allocator); switch (packet.packet_type) { .data => { diff --git a/cli/src/commands/queue.zig b/cli/src/commands/queue.zig index 34efaba..43f4a9d 100644 --- a/cli/src/commands/queue.zig +++ b/cli/src/commands/queue.zig @@ -488,14 +488,7 @@ fn queueSingleJob( } return; }; - defer { - if (packet.success_message) |m| allocator.free(m); - if (packet.error_message) |m| allocator.free(m); - if (packet.error_details) |m| allocator.free(m); - if (packet.data_payload) |m| allocator.free(m); - if (packet.data_type) |m| allocator.free(m); - if (packet.status_data) |m| allocator.free(m); - } + defer packet.deinit(allocator); switch (packet.packet_type) { .success => {