const std = @import("std"); const prompt = @import("../prompt.zig"); const Config = @import("../config.zig"); pub const User = @import("user.zig"); pub const ImageManager = @import("image-manager/root.zig"); pub const Image = ImageManager.Image; pub const SessionManager = @import("session-manager/root.zig"); pub const Session = SessionManager.Session; const Self = @This(); dir: std.fs.Dir, sessions: SessionManager = .empty, images: ImageManager = .empty, config: Config, allocator: std.mem.Allocator, pub fn init( allocator: std.mem.Allocator, ) !Self { var config = try Config.load(allocator); errdefer config.deinit(); const dir = std.fs.cwd().openDir(config.storage_path, .{}) catch blk: { try std.fs.cwd().makeDir(config.storage_path); break :blk try std.fs.cwd().openDir(config.storage_path, .{}); }; var self = Self { .config = config, .dir = dir, .allocator = allocator, }; dir.access("user", .{}) catch |err| switch (err) { error.FileNotFound => { const name = try prompt.read("Username", allocator); defer allocator.free(name); const full_name = try prompt.read("Full Name", allocator); defer allocator.free(full_name); const birthday = try prompt.read("Birthday", allocator); defer allocator.free(birthday); const password = try prompt.read("Password", allocator); defer allocator.free(password); try self.dir.makeDir("user"); var user: User = try .new( &self, name, full_name, birthday, password, true, allocator, ); defer user.deinit(); try user.sync(); }, else => return err, }; try self.images.init(&self); return self; } pub fn deinit(self: *Self) void { self.dir.close(); self.config.deinit(); self.* = undefined; }