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