From 113fb5e75cf4b15274f87588cf37848a428031ad Mon Sep 17 00:00:00 2001 From: Emil Lerch Date: Thu, 6 Jan 2022 12:13:57 -0800 Subject: [PATCH] getCurl implementation. Note Linux not tested --- src/clipboard.zig | 56 ++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 53 insertions(+), 3 deletions(-) diff --git a/src/clipboard.zig b/src/clipboard.zig index 533b8c2..52e05da 100644 --- a/src/clipboard.zig +++ b/src/clipboard.zig @@ -1,3 +1,4 @@ +const builtin = @import("builtin"); const std = @import("std"); const zfetch = @import("zfetch"); const crypt = @import("crypt.zig"); @@ -139,11 +140,60 @@ fn get(allocator: std.mem.Allocator) ![]const u8 { } fn getCurl(allocator: std.mem.Allocator, curl_path: []const u8) ![]const u8 { - _ = allocator; - _ = curl_path; - return "hello"; + std.log.debug("curl path: {s}", .{curl_path}); + const result = os: { + if (builtin.os.tag == .linux) { + const curl_cmd = try std.fmt.allocPrint(allocator, "{s} -s {s}", .{ curl_path, clipboard_url }); + defer allocator.free(curl_cmd); + break :os try execLinux(allocator, curl_cmd); + } else if (builtin.os.tag == .windows) { + break :os try std.ChildProcess.exec(.{ + .allocator = allocator, + .argv = &[_][]const u8{ + curl_path, // TODO: use Comspec + "-s", + clipboard_url, + }, + }); + } else { + return error.OsUnsupported; + } + }; + try std.io.getStdErr().writer().writeAll(result.stderr); + switch (result.term) { + .Exited => |code| if (code == 0) { + return result.stdout; + } else return error.NonZeroExit, + .Signal => return error.FailedWithSignal, + .Stopped => return error.WasStopped, + .Unknown => return error.Failed, + } } +fn execLinux(allocator: std.mem.Allocator, cmd: []const u8) !std.ChildProcess.ExecResult { + return std.ChildProcess.exec(.{ + .allocator = allocator, + .argv = &[_][]const u8{ + "/usr/bin/env", + "sh", + "-c", + cmd, + }, + }); +} + +// Potentially useful code, but no longer necessary +// fn execWindows(allocator: std.mem.Allocator, cmd: []const u8) !std.ChildProcess.ExecResult { +// return std.ChildProcess.exec(.{ +// .allocator = allocator, +// .argv = &[_][]const u8{ +// "c:\\windows\\system32\\cmd.exe", // TODO: use Comspec +// "/c", +// cmd, +// }, +// }); +// } + fn post(allocator: std.mem.Allocator, data: []const u8) !void { // TODO: Windows // var cert_reader = std.io.fixedBufferStream(