extract method to read the config file

This commit is contained in:
2024-08-27 19:12:28 +02:00
parent d1cf64bdcb
commit 8854646cda

View File

@@ -5,6 +5,7 @@ const print = std.debug.print;
const Allocator = std.mem.Allocator;
const testing = std.testing;
const expect = testing.expect;
const ArrayList = std.ArrayList;
// otpauth://totp/AWS+Dev?secret=47STA47VFCMMLLWOLHWO3KY7MYNC36MLCDTHOLIYKJCTTSSAMKVM7YA3VWT2AJEP&digits=6&icon=Amazon
@@ -18,7 +19,7 @@ pub fn main() !void {
const arg: Args = try parseArgs(allocator, args);
//print("parsed Args: {?any}\n", arg);
if (arg.list) {
const names = try executeGetList(arg.config_location);
const names = try executeGetList(allocator, arg.config_location);
for (0..names.items.len) |i| {
std.debug.print("{s}\n", .{names.items[i]});
@@ -45,7 +46,6 @@ fn printHelp() void {
}
const Args = struct {
add: ?OtpAuthUrl,
list: bool,
config_location: []const u8,
};
@@ -60,7 +60,6 @@ const ArgumentError = error{ InvalidOtpAuthUrl, UnknownParameter, MissingConfigL
fn parseArgs(allocator: Allocator, args: []const []const u8) !Args {
var result = Args{
.add = null,
.list = false,
.config_location = try configLocation(allocator),
};
@@ -121,25 +120,33 @@ fn configLocation(allocator: Allocator) ![]const u8 {
return config_location;
}
fn executeGetList(config_location: []const u8) !std.ArrayList([]const u8) {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
const allocator = gpa.allocator();
fn read_config(allocator: Allocator, config_location: []const u8) !ArrayList(OtpAuthUrl) {
const file = try std.fs.cwd().openFile(config_location, .{});
defer file.close();
var buf_reader = std.io.bufferedReader(file.reader());
var in_stream = buf_reader.reader();
var names = std.ArrayList([]const u8).init(allocator);
var authenticators = std.ArrayList(OtpAuthUrl).init(allocator);
while (try in_stream.readUntilDelimiterOrEofAlloc(allocator, '\n', 1024 * 1024)) |line| {
//std.debug.print("line: {any}", .{line});
if (line.len > 0 and std.mem.trim(u8, line, " \r").len > 0) {
const url = try parseOtpAuthUrl(line);
try names.append(url.name);
const authenticator = try parseOtpAuthUrl(line);
try authenticators.append(authenticator);
}
}
return authenticators;
}
fn executeGetList(allocator: Allocator, config_location: []const u8) !std.ArrayList([]const u8) {
var names = std.ArrayList([]const u8).init(allocator);
const authenticators = try read_config(allocator, config_location);
for (0..authenticators.items.len) |i| {
try names.append(authenticators.items[i].name);
}
return names;
}
@@ -152,6 +159,9 @@ test "parse command line parameter: 'list'" {
}
test "read list of entries" {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
const allocator = gpa.allocator();
try std.fs.cwd().makeDir("test-tmp");
const file = try std.fs.cwd().createFile("test-tmp/zig-totp", .{ .read = true });
defer {
@@ -161,7 +171,7 @@ test "read list of entries" {
_ = try file.write("otpauth://totp/token1?secret=c2VjcmV0Cg==\notpauth://totp/token2?secret=c2VjcmV0Cg==\n");
const list: std.ArrayList([]const u8) = try executeGetList("test-tmp/zig-totp");
const list: std.ArrayList([]const u8) = try executeGetList(allocator, "test-tmp/zig-totp");
try std.testing.expectEqualStrings("token1", list.items[0]);
try std.testing.expectEqualStrings("token2", list.items[1]);
try std.testing.expectEqual(2, list.items.len);