A SpaceTraders Agent

create agent module with start of config

altagos.dev b09d2a88 e513358c

verified
+84
+15
build.zig
··· 6 6 7 7 const options = b.addOptions(); 8 8 9 + const known_folders = b.dependency("known_folders", .{}).module("known-folders"); 9 10 const st = b.addModule("st", .{ 10 11 .root_source_file = b.path("src/st/root.zig"), 11 12 .target = target, 12 13 .optimize = optimize, 13 14 }); 14 15 16 + const agent = b.addModule("agent", .{ 17 + .root_source_file = b.path("src/agent/root.zig"), 18 + .target = target, 19 + .optimize = optimize, 20 + .imports = &.{ 21 + .{ .name = "pretty", .module = pretty }, 22 + .{ .name = "known-folders", .module = known_folders }, 23 + // Modules 24 + .{ .name = "st", .module = st }, 25 + }, 26 + }); 27 + 15 28 const space = b.createModule(.{ 16 29 .root_source_file = b.path("src/main.zig"), 17 30 .target = target, 18 31 .optimize = optimize, 19 32 .imports = &.{ 33 + .{ .name = "known-folders", .module = known_folders }, 20 34 .{ .name = "st", .module = st }, 35 + .{ .name = "agent", .module = agent }, 21 36 }, 22 37 }); 23 38
+4
build.zig.zon
··· 1 1 .{ 2 2 .name = .space, 3 3 .version = "0.0.1", 4 + .known_folders = .{ 5 + .url = "git+https://github.com/ziglibs/known-folders.git#bafef170a73c064dc706fcfbdc2e406a35681a9c", 6 + .hash = "known_folders-0.0.0-Fy-PJovNAAAtqbaXgBhV6G-Z4-WNo7P0Rov-x-npZq21", 7 + }, 4 8 .minimum_zig_version = "0.16.0-dev.1246+4b593a6c2", 5 9 .paths = .{ 6 10 "build.zig",
+64
src/agent/config.zig
··· 1 + const std = @import("std"); 2 + const kf = @import("known-folders"); 3 + 4 + const Auth = @import("st").http.Auth; 5 + 6 + pub const Config = struct { 7 + auth: Auth = .{ 8 + .account = "Bearer <TOKEN>", 9 + .agent = "Bearer <TOKEN>", 10 + }, 11 + }; 12 + 13 + const log = std.log.scoped(.agent); 14 + 15 + pub fn load(io: std.Io, allocator: std.mem.Allocator) !Config { 16 + log.debug("Loading config", .{}); 17 + 18 + const dir = try kf.open(io, allocator, .roaming_configuration, .{ .follow_symlinks = true }) orelse return error.NoConfigFolder; 19 + var file: ?std.Io.File = null; 20 + defer if (file) |f| f.close(io); 21 + 22 + _ = dir.statPath(io, "space/config.zon", .{ .follow_symlinks = true }) catch { 23 + file = try create(io, dir); 24 + }; 25 + 26 + if (file == null) file = try dir.openFile(io, "space/config.zon", .{}); 27 + const stat = try file.?.stat(io); 28 + 29 + const source = try allocator.allocSentinel(u8, stat.size, 0); 30 + defer allocator.free(source); 31 + 32 + var buffer: [64]u8 = undefined; 33 + var reader = file.?.reader(io, &buffer); 34 + try reader.interface.readSliceAll(source); 35 + 36 + return std.zon.parse.fromSliceAlloc( 37 + Config, 38 + allocator, 39 + source, 40 + null, 41 + .{}, 42 + ); 43 + } 44 + 45 + pub fn create(io: std.Io, dir: std.Io.Dir) !std.Io.File { 46 + log.warn("Creating new config file", .{}); 47 + const config = Config{}; 48 + 49 + try dir.makePath(io, "space"); 50 + const file = try dir.createFile(io, "space/config.zon", .{ .read = true }); 51 + const file_old = std.fs.File.adaptFromNewApi(file); 52 + 53 + var buffer: [64]u8 = undefined; 54 + var writer = file_old.writer(&buffer); 55 + 56 + try std.zon.stringify.serializeArbitraryDepth(config, .{}, &writer.interface); 57 + try writer.interface.flush(); 58 + 59 + return file; 60 + } 61 + 62 + pub fn free(allocator: std.mem.Allocator, config: Config) void { 63 + std.zon.parse.free(allocator, config); 64 + }
+1
src/agent/root.zig
··· 1 + pub const config = @import("config.zig");