diff options
Diffstat (limited to 'src/storage/root.zig')
| -rw-r--r-- | src/storage/root.zig | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/src/storage/root.zig b/src/storage/root.zig new file mode 100644 index 0000000..bf9803e --- /dev/null +++ b/src/storage/root.zig @@ -0,0 +1,57 @@ +const std = @import("std"); +const config = @import("../config.zig"); +const prompt = @import("../prompt.zig"); + +pub const User = @import("user.zig"); + +const Self = @This(); + +dir: std.fs.Dir, + +pub fn init(allocator: std.mem.Allocator) !Self { + 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 { .dir = dir }; + + 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, + }; + + return self; +} + +pub fn deinit(self: *Self) void { + self.dir.close(); + self.* = undefined; +} |