From b09d2a88116d262840603d9b32649511c07c89cb Mon Sep 17 00:00:00 2001 From: Altagos Date: Sat, 8 Nov 2025 17:59:36 +0100 Subject: [PATCH] create agent module with start of config --- build.zig | 15 +++++++++++ build.zig.zon | 4 +++ src/agent/config.zig | 64 ++++++++++++++++++++++++++++++++++++++++++++ src/agent/root.zig | 1 + 4 files changed, 84 insertions(+) create mode 100644 src/agent/config.zig create mode 100644 src/agent/root.zig diff --git a/build.zig b/build.zig index 6dedb3c..15e4a3c 100644 --- a/build.zig +++ b/build.zig @@ -6,18 +6,33 @@ pub fn build(b: *std.Build) void { const options = b.addOptions(); + const known_folders = b.dependency("known_folders", .{}).module("known-folders"); const st = b.addModule("st", .{ .root_source_file = b.path("src/st/root.zig"), .target = target, .optimize = optimize, }); + const agent = b.addModule("agent", .{ + .root_source_file = b.path("src/agent/root.zig"), + .target = target, + .optimize = optimize, + .imports = &.{ + .{ .name = "pretty", .module = pretty }, + .{ .name = "known-folders", .module = known_folders }, + // Modules + .{ .name = "st", .module = st }, + }, + }); + const space = b.createModule(.{ .root_source_file = b.path("src/main.zig"), .target = target, .optimize = optimize, .imports = &.{ + .{ .name = "known-folders", .module = known_folders }, .{ .name = "st", .module = st }, + .{ .name = "agent", .module = agent }, }, }); diff --git a/build.zig.zon b/build.zig.zon index 419fc64..f9f7bef 100644 --- a/build.zig.zon +++ b/build.zig.zon @@ -1,6 +1,10 @@ .{ .name = .space, .version = "0.0.1", + .known_folders = .{ + .url = "git+https://github.com/ziglibs/known-folders.git#bafef170a73c064dc706fcfbdc2e406a35681a9c", + .hash = "known_folders-0.0.0-Fy-PJovNAAAtqbaXgBhV6G-Z4-WNo7P0Rov-x-npZq21", + }, .minimum_zig_version = "0.16.0-dev.1246+4b593a6c2", .paths = .{ "build.zig", diff --git a/src/agent/config.zig b/src/agent/config.zig new file mode 100644 index 0000000..a89d220 --- /dev/null +++ b/src/agent/config.zig @@ -0,0 +1,64 @@ +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); +} diff --git a/src/agent/root.zig b/src/agent/root.zig new file mode 100644 index 0000000..63c3ebd --- /dev/null +++ b/src/agent/root.zig @@ -0,0 +1 @@ +pub const config = @import("config.zig"); -- 2.51.2