Updater: continue on implementing Updater.inform

This commit is contained in:
Halil Durak
2026-06-26 02:30:41 +03:00
parent bbd53e4d83
commit 6acb58eff4
2 changed files with 41 additions and 4 deletions

View File

@@ -111,9 +111,43 @@ fn resetConnection(self: *Updater) !void {
self.conn_err = .none;
}
const SortContext = struct {
object: std.json.ObjectMap,
pub fn lessThan(ctx: SortContext, a_index: usize, b_index: usize) bool {
const keys = ctx.object.keys();
const a_version = std.SemanticVersion.parse(keys[a_index]) catch unreachable;
const b_version = std.SemanticVersion.parse(keys[b_index]) catch unreachable;
return a_version.order(b_version).compare(.gt);
}
};
/// Informs about running version to given `Writer` by desired `Channel`.
pub fn inform(self: *Updater, channel: Channel, writer: std.Io.Writer) void {
pub fn inform(self: *Updater, channel: Channel, writer: *std.Io.Writer) !void {
const versions = try self.getVersions();
defer self.resetConnection() catch {};
switch (channel) {
.stable => {
var stable = versions.object;
// Get rid of `nightly`.
lp.assert(stable.swapRemove("nightly"), "Updater.inform: incorrect JSON", .{});
// Sort and retrieve the newest.
stable.sort(SortContext{ .object = stable });
// Newest is at the beginning.
const newest = stable.values()[0].object;
const version = (newest.get("version") orelse return error.IncorrectJson).string;
try writer.print("Latest stable Lightpanda version is {s}.\n", .{version});
return writer.flush();
},
.nightly => {
const nightly = (versions.object.get("nightly") orelse return error.IncorrectJson).object;
const version = (nightly.get("version") orelse return error.IncorrectJson).string;
try writer.print("Latest nightly Lightpanda version is {s}.\n", .{version});
return writer.flush();
},
}
}
/// Invoked by `Connection` when there are body bytes.

View File

@@ -227,12 +227,15 @@ fn dumpContent(app: *App, mode: Config.DumpFormat, dump_opts: dump.Opts, frame:
/// Check `Config.zig` for `options`.
pub fn update(allocator: std.mem.Allocator, config: *const Config, options: anytype) !void {
if (options.channel == .nightly) @panic("TODO");
var client = try Updater.init(allocator, config);
defer client.deinit();
try client.getVersions();
var stdout = std.fs.File.stdout();
var buf: [4096]u8 = undefined;
var writer = stdout.writer(&buf);
const w = &writer.interface;
// Inform to stdout about version.
try client.inform(options.channel, w);
}
// Writes a single page's result object. Framing (the enclosing array and any