store iid in application data directory

This commit is contained in:
Karl Seguin
2025-03-10 14:46:15 +08:00
parent b0a2087015
commit 1e6a1bd3af
2 changed files with 49 additions and 18 deletions

View File

@@ -4,6 +4,8 @@ const Loop = @import("jsruntime").Loop;
const Allocator = std.mem.Allocator;
const Telemetry = @import("telemetry/telemetry.zig").Telemetry;
const log = std.log.scoped(.app);
pub const RunMode = enum {
serve,
fetch,
@@ -13,6 +15,7 @@ pub const RunMode = enum {
// might need.
pub const App = struct {
loop: *Loop,
app_dir_path: ?[]const u8,
allocator: Allocator,
telemetry: Telemetry,
@@ -23,19 +26,42 @@ pub const App = struct {
loop.* = try Loop.init(allocator);
errdefer loop.deinit();
const telemetry = Telemetry.init(allocator, run_mode);
const app_dir_path = getAndMakeAppDir(allocator);
const telemetry = Telemetry.init(allocator, run_mode, app_dir_path);
errdefer telemetry.deinit();
return .{
.loop = loop,
.allocator = allocator,
.telemetry = telemetry,
.app_dir_path = app_dir_path,
};
}
pub fn deinit(self: *App) void {
if (self.app_dir_path) |app_dir_path| {
self.allocator.free(app_dir_path);
}
self.telemetry.deinit();
self.loop.deinit();
self.allocator.destroy(self.loop);
}
};
fn getAndMakeAppDir(allocator: Allocator) ?[]const u8 {
const app_dir_path = std.fs.getAppDataDir(allocator, "lightpanda") catch |err| {
log.warn("failed to get lightpanda data dir: {}", .{err});
return null;
};
std.fs.makeDirAbsolute(app_dir_path) catch |err| switch (err) {
error.PathAlreadyExists => return app_dir_path,
else => {
allocator.free(app_dir_path);
log.warn("failed to create lightpanda data dir: {}", .{err});
return null;
}
};
return app_dir_path;
}