aboutsummaryrefslogtreecommitdiff
path: root/src/storage/root.zig
blob: bf9803eb8df601349d659233c696e5270bd4401f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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;
}