A SpaceTraders Agent
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

at main 77 lines 2.1 kB view raw
1const std = @import("std"); 2const kf = @import("known-folders"); 3 4const Auth = @import("st").http.Auth; 5 6pub const Config = struct { 7 auth: Auth = .{ 8 .account = "Bearer <TOKEN>", 9 .agent = "Bearer <TOKEN>", 10 }, 11 12 db: DBConfig = .{ 13 .path = "./space.db", 14 .pool_size = 5, 15 }, 16 17 pub fn updateAgentToken(self: *Config, token: []const u8) void { 18 self.auth.agent = "Bearer " ++ token; 19 } 20}; 21 22pub const DBConfig = struct { 23 path: []const u8, 24 pool_size: u32, 25}; 26 27const log = std.log.scoped(.agent); 28 29pub fn load(io: std.Io, allocator: std.mem.Allocator, environ: *std.process.Environ.Map) !Config { 30 log.debug("Loading config", .{}); 31 32 const dir = try kf.open( 33 io, 34 allocator, 35 environ.*, 36 .roaming_configuration, 37 .{ .follow_symlinks = true }, 38 ) orelse return error.NoConfigFolder; 39 var file: ?std.Io.File = null; 40 defer if (file) |f| f.close(io); 41 42 _ = dir.statFile(io, "space/config.zon", .{ .follow_symlinks = true }) catch { 43 file = try create(io, dir); 44 }; 45 46 if (file == null) file = try dir.openFile(io, "space/config.zon", .{}); 47 const stat = try file.?.stat(io); 48 49 const source = try allocator.allocSentinel(u8, stat.size, 0); 50 defer allocator.free(source); 51 52 var buffer: [64]u8 = undefined; 53 var reader = file.?.reader(io, &buffer); 54 try reader.interface.readSliceAll(source); 55 56 return std.zon.parse.fromSliceAlloc(Config, allocator, source, null, .{}); 57} 58 59pub fn create(io: std.Io, dir: std.Io.Dir) !std.Io.File { 60 log.warn("Creating new config file", .{}); 61 const config = Config{}; 62 63 try dir.createDirPath(io, "space"); 64 const file = try dir.createFile(io, "space/config.zon", .{ .read = true }); 65 66 var buffer: [64]u8 = undefined; 67 var writer = file.writer(io, &buffer); 68 69 try std.zon.stringify.serializeArbitraryDepth(config, .{}, &writer.interface); 70 try writer.interface.flush(); 71 72 return file; 73} 74 75pub fn free(allocator: std.mem.Allocator, config: Config) void { 76 std.zon.parse.free(allocator, config); 77}