const std = @import("std"); const kf = @import("known-folders"); const Auth = @import("st").http.Auth; pub const Config = struct { auth: Auth = .{ .account = "Bearer ", .agent = "Bearer ", }, }; const log = std.log.scoped(.agent); pub fn load(io: std.Io, allocator: std.mem.Allocator) !Config { log.debug("Loading config", .{}); const dir = try kf.open(io, allocator, .roaming_configuration, .{ .follow_symlinks = true }) orelse return error.NoConfigFolder; var file: ?std.Io.File = null; defer if (file) |f| f.close(io); _ = dir.statPath(io, "space/config.zon", .{ .follow_symlinks = true }) catch { file = try create(io, dir); }; if (file == null) file = try dir.openFile(io, "space/config.zon", .{}); const stat = try file.?.stat(io); const source = try allocator.allocSentinel(u8, stat.size, 0); defer allocator.free(source); var buffer: [64]u8 = undefined; var reader = file.?.reader(io, &buffer); try reader.interface.readSliceAll(source); return std.zon.parse.fromSliceAlloc(Config, allocator, source, null, .{}); } pub fn create(io: std.Io, dir: std.Io.Dir) !std.Io.File { log.warn("Creating new config file", .{}); const config = Config{}; try dir.makePath(io, "space"); const file = try dir.createFile(io, "space/config.zon", .{ .read = true }); const file_old = std.fs.File.adaptFromNewApi(file); var buffer: [64]u8 = undefined; var writer = file_old.writer(&buffer); try std.zon.stringify.serializeArbitraryDepth(config, .{}, &writer.interface); try writer.interface.flush(); return file; } pub fn free(allocator: std.mem.Allocator, config: Config) void { std.zon.parse.free(allocator, config); }