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
58
59
60
61
62
63
64
65
|
const std = @import("std");
const config = @import("../config.zig");
const prompt = @import("../prompt.zig");
pub const User = @import("user.zig");
pub const Image = @import("image.zig");
pub const SessionManager = @import("session-manager/root.zig");
pub const Session = SessionManager.Session;
const Self = @This();
dir: std.fs.Dir,
sessions: SessionManager = .empty,
allocator: std.mem.Allocator,
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,
.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,
};
return self;
}
pub fn deinit(self: *Self) void {
self.dir.close();
self.* = undefined;
}
|