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
This commit is contained in:
Jason Rayne
2025-06-25 17:47:01 -07:00
parent 1873add697
commit eed2006b4d

View File

@ -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");
}