add pacmanxfer script

This commit is contained in:
LordMZTE 2023-11-10 21:41:39 +01:00
parent 5663449efc
commit 6559c0a6a4
Signed by: LordMZTE
GPG key ID: B64802DC33A64FF6
5 changed files with 158 additions and 0 deletions

2
scripts/pacmanxfer/.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
zig-cache/
zig-out/

View file

@ -0,0 +1,36 @@
//! This is a tool which wraps rsync and curl and is intended to be used by pacman for downloading
//! packages.
//!
//! pacman.conf:
//! [common]
//! XferCommand = /path/to/pacmanxfer %u %o
const std = @import("std");
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const ansiterm_dep = b.dependency("ansi_term", .{ .target = target, .optimize = optimize });
const exe = b.addExecutable(.{
.name = "pacmanxfer",
.root_source_file = .{ .path = "src/main.zig" },
.target = target,
.optimize = optimize,
});
exe.addModule("ansi-term", ansiterm_dep.module("ansi-term"));
b.installArtifact(exe);
const run_cmd = b.addRunArtifact(exe);
run_cmd.step.dependOn(b.getInstallStep());
if (b.args) |args| {
run_cmd.addArgs(args);
}
const run_step = b.step("run", "Run the app");
run_step.dependOn(&run_cmd.step);
}

View file

@ -0,0 +1,12 @@
.{
.name = "pacmanxfer",
.version = "0.0.0",
.dependencies = .{
.ansi_term = .{
.url = "https://github.com/ziglibs/ansi-term/archive/1614b61486d567b59abe11a097d11aa6ce679819.tar.gz",
.hash = "1220647eea49d2c48d5e59354291e975f813be3cc5a9d9920a50bbfaa40a891a06ee",
},
},
.paths = .{""},
}

View file

@ -0,0 +1,107 @@
const std = @import("std");
const at = @import("ansi-term");
const Downloader = enum {
rsync,
curl,
fn forProtocol(proto: []const u8) ?Downloader {
return if (std.mem.eql(u8, proto, "rsync"))
.rsync
else if (std.mem.eql(u8, proto, "https") or std.mem.eql(u8, proto, "http"))
.curl
else
null;
}
fn getOutputLineCount(self: Downloader) usize {
return switch (self) {
.rsync => 2,
.curl => 3,
};
}
};
pub fn main() !u8 {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const alloc = gpa.allocator();
if (std.os.argv.len != 3)
return error.InvalidArguments;
const url_arg = std.mem.span(std.os.argv[1]);
const dest = std.mem.span(std.os.argv[2]);
const url = try std.Uri.parse(url_arg);
const downloader = Downloader.forProtocol(url.scheme) orelse return error.UnknownProtocol;
const argv = switch (downloader) {
.rsync => try alloc.dupe([]const u8, &.{
"rsync",
"--copy-links", // needed to correctly download pacman databases
"--partial",
"--info=progress2",
url_arg,
dest,
}),
// zig fmt: off
.curl => try alloc.dupe([]const u8, &.{
"curl",
"--location", // handle redirects
"--continue-at", "-", // resume partial downloads
"--fail", // fail fast
"--output", dest,
url_arg,
}),
// zig fmt: on
};
defer alloc.free(argv);
var stdout = std.io.bufferedWriter(std.io.getStdOut().writer());
var style: ?at.style.Style = null;
try updateStyle(stdout.writer(), &style, .{
.foreground = switch (downloader) {
.curl => .Red,
.rsync => .Blue,
},
.font_style = .{ .bold = true },
});
try stdout.writer().writeAll("==> ");
try updateStyle(stdout.writer(), &style, .{});
try stdout.writer().writeAll(std.fs.path.basename(url.path));
try stdout.writer().writeByte('\n');
try updateStyle(stdout.writer(), &style, .{
.foreground = .Green,
.font_style = .{ .bold = true },
});
try stdout.writer().writeAll(">> ");
try updateStyle(stdout.writer(), &style, .{});
try stdout.writer().writeAll(url_arg);
try stdout.writer().writeByte('\n');
try stdout.flush();
var child = std.process.Child.init(argv, alloc);
const term = try child.spawnAndWait();
const retcode = switch (term) {
.Exited => |t| t,
.Signal, .Stopped, .Unknown => 255,
};
if (retcode == 0) {
try at.cursor.cursorUp(stdout.writer(), downloader.getOutputLineCount() + 1);
try at.clear.clearFromCursorToScreenEnd(stdout.writer());
try stdout.flush();
}
return retcode;
}
fn updateStyle(writer: anytype, old: *?at.style.Style, new: at.style.Style) !void {
try at.format.updateStyle(writer, new, old.*);
old.* = new;
}

View file

@ -21,6 +21,7 @@
(install-rust "scripts/i3status")
(install-zig "scripts/mzteinit")
(install-zig "scripts/openbrowser")
(install-zig "scripts/pacmanxfer")
(install-zig "scripts/playtwitch")
(install-zig "scripts/prompt")
(install-zig "scripts/randomwallpaper")