summaryrefslogtreecommitdiff
path: root/src/cli/ssh_cache.zig
diff options
context:
space:
mode:
authorJason Rayne <yo@arcayne.dev>2025-06-25 17:47:01 -0700
committerJason Rayne <yo@arcayne.dev>2025-06-25 17:47:01 -0700
commiteed2006b4d7110b935c14f3fcb7ba5679aa010e5 (patch)
tree070bb32510d53008c8ab571f431f842fb927ff72 /src/cli/ssh_cache.zig
parent1873add697ac9a4b9b5b73d1d6225689eebfa071 (diff)
fix: correct resources directory fallback path and eliminate code duplication in ssh_cache
- Fix fallback path from full path to "src" since full path is built later - Extract duplicate code from listCachedHosts and clearCache into runCacheCommand helper - Addresses feedback from @00-kat
Diffstat (limited to 'src/cli/ssh_cache.zig')
-rw-r--r--src/cli/ssh_cache.zig38
1 files changed, 9 insertions, 29 deletions
diff --git a/src/cli/ssh_cache.zig b/src/cli/ssh_cache.zig
index c3484735a..02462816c 100644
--- a/src/cli/ssh_cache.zig
+++ b/src/cli/ssh_cache.zig
@@ -7,19 +7,19 @@ fn getCacheScriptPath(alloc: Allocator) ![]u8 {
// Use GHOSTTY_RESOURCES_DIR if available, otherwise assume relative path
const resources_dir = std.process.getEnvVarOwned(alloc, "GHOSTTY_RESOURCES_DIR") catch {
// Fallback: assume we're running from build directory
- return try alloc.dupe(u8, "src/shell-integration/shared/ghostty-ssh-cache");
+ return try alloc.dupe(u8, "src");
};
defer alloc.free(resources_dir);
return try std.fs.path.join(alloc, &[_][]const u8{ resources_dir, "shell-integration", "shared", "ghostty-ssh-cache" });
}
-/// List cached hosts by calling the external script
-pub fn listCachedHosts(alloc: Allocator, writer: anytype) !void {
+/// Generic function to run cache script commands
+fn runCacheCommand(alloc: Allocator, writer: anytype, command: []const u8) !void {
const script_path = try getCacheScriptPath(alloc);
defer alloc.free(script_path);
- var child = Child.init(&[_][]const u8{ script_path, "list" }, alloc);
+ var child = Child.init(&[_][]const u8{ script_path, command }, alloc);
child.stdout_behavior = .Pipe;
child.stderr_behavior = .Pipe;
@@ -38,34 +38,14 @@ pub fn listCachedHosts(alloc: Allocator, writer: anytype) !void {
if (stderr.len > 0) {
try writer.writeAll(stderr);
}
+}
- // Script handles its own success/error messaging, so we don't need to check exit code
+/// List cached hosts by calling the external script
+pub fn listCachedHosts(alloc: Allocator, writer: anytype) !void {
+ try runCacheCommand(alloc, writer, "list");
}
/// Clear cache by calling the external script
pub fn clearCache(alloc: Allocator, writer: anytype) !void {
- const script_path = try getCacheScriptPath(alloc);
- defer alloc.free(script_path);
-
- var child = Child.init(&[_][]const u8{ script_path, "clear" }, alloc);
- child.stdout_behavior = .Pipe;
- child.stderr_behavior = .Pipe;
-
- try child.spawn();
-
- const stdout = try child.stdout.?.readToEndAlloc(alloc, std.math.maxInt(usize));
- defer alloc.free(stdout);
-
- const stderr = try child.stderr.?.readToEndAlloc(alloc, std.math.maxInt(usize));
- defer alloc.free(stderr);
-
- _ = try child.wait();
-
- // Output the results regardless of exit code
- try writer.writeAll(stdout);
- if (stderr.len > 0) {
- try writer.writeAll(stderr);
- }
-
- // Script handles its own success/error messaging, so we don't need to check exit code
+ try runCacheCommand(alloc, writer, "clear");
}