diff options
| author | Nathan Reiner <nathan@nathanreiner.xyz> | 2025-11-14 21:55:59 +0100 |
|---|---|---|
| committer | Nathan Reiner <nathan@nathanreiner.xyz> | 2025-11-14 21:55:59 +0100 |
| commit | 3f18f02d07802d1fc705a500e5978a9b3cb2e751 (patch) | |
| tree | 283970a2f5a693706456b853c550eeaa669b5d72 /src/storage/root.zig | |
| parent | 351ad457f0ff95e20301a146b8c88a8f0f659aa1 (diff) | |
implement login
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; +} |