tangled
alpha
login
or
join now
altagos.dev
/
space
0
fork
atom
A SpaceTraders Agent
0
fork
atom
overview
issues
pulls
pipelines
create agent module with start of config
altagos.dev
4 months ago
b09d2a88
e513358c
verified
This commit was signed with the committer's
known signature
.
altagos.dev
SSH Key Fingerprint:
SHA256:UbTjEcCZlc6GzQWLCuDK3D//HESWD2xFPkzue9XMras=
+84
4 changed files
expand all
collapse all
unified
split
build.zig
build.zig.zon
src
agent
config.zig
root.zig
+15
build.zig
···
6
6
7
7
const options = b.addOptions();
8
8
9
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
16
+
const agent = b.addModule("agent", .{
17
17
+
.root_source_file = b.path("src/agent/root.zig"),
18
18
+
.target = target,
19
19
+
.optimize = optimize,
20
20
+
.imports = &.{
21
21
+
.{ .name = "pretty", .module = pretty },
22
22
+
.{ .name = "known-folders", .module = known_folders },
23
23
+
// Modules
24
24
+
.{ .name = "st", .module = st },
25
25
+
},
26
26
+
});
27
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
33
+
.{ .name = "known-folders", .module = known_folders },
20
34
.{ .name = "st", .module = st },
35
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
4
+
.known_folders = .{
5
5
+
.url = "git+https://github.com/ziglibs/known-folders.git#bafef170a73c064dc706fcfbdc2e406a35681a9c",
6
6
+
.hash = "known_folders-0.0.0-Fy-PJovNAAAtqbaXgBhV6G-Z4-WNo7P0Rov-x-npZq21",
7
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
1
+
const std = @import("std");
2
2
+
const kf = @import("known-folders");
3
3
+
4
4
+
const Auth = @import("st").http.Auth;
5
5
+
6
6
+
pub const Config = struct {
7
7
+
auth: Auth = .{
8
8
+
.account = "Bearer <TOKEN>",
9
9
+
.agent = "Bearer <TOKEN>",
10
10
+
},
11
11
+
};
12
12
+
13
13
+
const log = std.log.scoped(.agent);
14
14
+
15
15
+
pub fn load(io: std.Io, allocator: std.mem.Allocator) !Config {
16
16
+
log.debug("Loading config", .{});
17
17
+
18
18
+
const dir = try kf.open(io, allocator, .roaming_configuration, .{ .follow_symlinks = true }) orelse return error.NoConfigFolder;
19
19
+
var file: ?std.Io.File = null;
20
20
+
defer if (file) |f| f.close(io);
21
21
+
22
22
+
_ = dir.statPath(io, "space/config.zon", .{ .follow_symlinks = true }) catch {
23
23
+
file = try create(io, dir);
24
24
+
};
25
25
+
26
26
+
if (file == null) file = try dir.openFile(io, "space/config.zon", .{});
27
27
+
const stat = try file.?.stat(io);
28
28
+
29
29
+
const source = try allocator.allocSentinel(u8, stat.size, 0);
30
30
+
defer allocator.free(source);
31
31
+
32
32
+
var buffer: [64]u8 = undefined;
33
33
+
var reader = file.?.reader(io, &buffer);
34
34
+
try reader.interface.readSliceAll(source);
35
35
+
36
36
+
return std.zon.parse.fromSliceAlloc(
37
37
+
Config,
38
38
+
allocator,
39
39
+
source,
40
40
+
null,
41
41
+
.{},
42
42
+
);
43
43
+
}
44
44
+
45
45
+
pub fn create(io: std.Io, dir: std.Io.Dir) !std.Io.File {
46
46
+
log.warn("Creating new config file", .{});
47
47
+
const config = Config{};
48
48
+
49
49
+
try dir.makePath(io, "space");
50
50
+
const file = try dir.createFile(io, "space/config.zon", .{ .read = true });
51
51
+
const file_old = std.fs.File.adaptFromNewApi(file);
52
52
+
53
53
+
var buffer: [64]u8 = undefined;
54
54
+
var writer = file_old.writer(&buffer);
55
55
+
56
56
+
try std.zon.stringify.serializeArbitraryDepth(config, .{}, &writer.interface);
57
57
+
try writer.interface.flush();
58
58
+
59
59
+
return file;
60
60
+
}
61
61
+
62
62
+
pub fn free(allocator: std.mem.Allocator, config: Config) void {
63
63
+
std.zon.parse.free(allocator, config);
64
64
+
}
+1
src/agent/root.zig
···
1
1
+
pub const config = @import("config.zig");