clean readCredentials

This commit is contained in:
Emil Lerch 2025-12-09 11:54:42 -08:00
parent 7f46d40027
commit 07ce67f429
Signed by: lobo
GPG key ID: A7B62D657EF764F8
2 changed files with 30 additions and 6 deletions

1
.gitignore vendored
View file

@ -1,2 +1,3 @@
zig-out
.zig-cache/
.credentials

View file

@ -13,14 +13,37 @@ const AuthResult = struct {
user_uuid: []const u8,
};
fn readCredentials(allocator: std.mem.Allocator) !struct { username: []const u8, password: []const u8, content: []const u8 } {
const file = try std.fs.cwd().openFile("../.credentials", .{});
const CognitoCredentials = struct {
allocator: std.mem.Allocator,
buffer: []const u8,
username: []const u8,
password: []const u8,
pub fn deinit(self: *CognitoCredentials) void {
self.allocator.free(self.buffer);
self.buffer = undefined;
self.username = undefined;
self.password = undefined;
}
};
fn readCredentials(allocator: std.mem.Allocator) !CognitoCredentials {
// TODO: something different
const file = try std.fs.cwd().openFile(".credentials", .{});
defer file.close();
const content = try file.readToEndAlloc(allocator, 1024);
var it = std.mem.splitScalar(u8, std.mem.trim(u8, content, &std.ascii.whitespace), '\n');
var it = std.mem.splitScalar(u8, std.mem.trim(
u8,
content,
&std.ascii.whitespace,
), '\n');
const username = it.next() orelse return error.InvalidCredentials;
const password = it.next() orelse return error.InvalidCredentials;
return .{ .username = username, .password = password, .content = content };
return .{
.username = username,
.password = password,
.buffer = content,
.allocator = allocator,
};
}
fn authenticate(allocator: std.mem.Allocator, username: []const u8, password: []const u8) !AuthResult {
@ -164,8 +187,8 @@ pub fn main() !void {
defer _ = gpa.deinit();
const allocator = gpa.allocator();
const creds = try readCredentials(allocator);
defer allocator.free(creds.content);
var creds = try readCredentials(allocator);
defer creds.deinit();
std.debug.print("🔐 Authenticating...\n", .{});
const auth = try authenticate(allocator, creds.username, creds.password);