Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118const std = @import("std");const zli = @import("zli");const atproto = @import("atproto");
pub fn main(init: std.process.Init) !void { try zli.run(init, "atproto tools", &.{ .group("resolve", "a thing", &.{ .group("user", "another thing", &.{ .func(res.did, "resolve did"), .func(res.pds, "resolve pds"), }), }), });}
const res = struct { pub fn did( init: std.process.Init, handle: zli.param.String("handle", "handle to resolve", .{}), ) !void { const arena = init.arena.allocator(); var resolver: atproto.Resolver = .init(arena, init.io); defer resolver.deinit(); const id = resolver.resolve(arena, handle.value) catch |err| return mapError(err); var buffer: [4096]u8 = undefined; var stdout = std.Io.File.stdout().writer(init.io, &buffer); const writer = &stdout.interface;
render(writer, &resolver, arena, id, .did) catch |err| switch (err) { error.WriteFailed => std.process.exit(1), error.OutOfMemory => |e| return e, else => |e| return mapError(e), }; writer.flush() catch std.process.exit(1); }
pub fn pds( init: std.process.Init, handle: zli.param.String("handle", "resolve pds for", .{}), ) !void { const arena = init.arena.allocator(); var resolver: atproto.Resolver = .init(arena, init.io); defer resolver.deinit(); const id = resolver.resolve(arena, handle.value) catch |err| return mapError(err); var buffer: [4096]u8 = undefined; var stdout = std.Io.File.stdout().writer(init.io, &buffer); const writer = &stdout.interface;
render(writer, &resolver, arena, id, .pds) catch |err| switch (err) { error.WriteFailed => std.process.exit(1), error.OutOfMemory => |e| return e, else => |e| return mapError(e), }; writer.flush() catch std.process.exit(1); }};const Return = enum { pds, did, alias, key, history, repo,};
const Allocator = std.mem.Allocator;
const HandlerError = error{ InvalidHandle, NotFound, RequestFailed } || Allocator.Error;
fn render( out: *std.Io.Writer, resolver: *atproto.Resolver, gpa: Allocator, identity: atproto.Identity, which: Return,) (HandlerError || std.Io.Writer.Error)!void { switch (which) { .did => try out.print("{f}\n", .{identity.did}), .pds => try out.print("{s}\n", .{identity.pds orelse "unknown"}), .key => try out.print("{s}\n", .{identity.signing_key orelse "unknown"}), .alias => for (identity.aliases) |alias| { try out.print("{s}\n", .{alias}); }, .history => { const log = resolver.auditLog(gpa, identity.did) catch |err| return mapError(err); for (log) |entry| { try out.print("{s} {s} {s}{s}\n", .{ entry.created_at, entry.handle orelse "?", entry.pds orelse "?", if (entry.nullified) " (nullified)" else "", }); } }, .repo => { const desc = resolver.describeRepo(gpa, identity) catch |err| return mapError(err); try out.print("handle correct: {s}\ncollections:\n", .{ if (desc.handleIsCorrect) "yes" else "no", }); for (desc.collections) |collection| { try out.print(" {s}\n", .{collection}); } }, }}
fn mapError(err: anyerror) HandlerError { return switch (err) { error.OutOfMemory => error.OutOfMemory, error.InvalidHandle, error.NotFound => @errorCast(err), else => error.RequestFailed, };}