cleanup
This commit is contained in:
91
src/main.zig
91
src/main.zig
@@ -17,25 +17,12 @@ pub fn main() !void {
|
||||
std.log.debug("arguments: {s}\n", .{args});
|
||||
const arg: Args = try parseArgs(allocator, args);
|
||||
//print("parsed Args: {?any}\n", arg);
|
||||
if (arg.add != null) {
|
||||
info("add:", .{});
|
||||
info("name: {s}", .{arg.add.?.name});
|
||||
info("secret: {s}", .{arg.add.?.secretEncoded});
|
||||
|
||||
const config_location = try configLocation(allocator);
|
||||
info("config location: {s}", .{config_location});
|
||||
|
||||
try readConfig(allocator);
|
||||
} else if (arg.list) {
|
||||
if (arg.list) {
|
||||
const names = try executeGetList(arg.config_location);
|
||||
|
||||
for (0..names.items.len) |i| {
|
||||
std.debug.print("{s}\n", .{names.items[i]});
|
||||
}
|
||||
|
||||
//for (names, 0..) |name, index| {
|
||||
// std.debug.print("{d} {s}", .{ index, name });
|
||||
//}
|
||||
} else {
|
||||
printHelp();
|
||||
}
|
||||
@@ -43,25 +30,16 @@ pub fn main() !void {
|
||||
//std.process.exit(returnValue);
|
||||
}
|
||||
|
||||
fn readConfig(allocator: Allocator) !void {
|
||||
const config_location = try configLocation(allocator);
|
||||
|
||||
var file = try std.fs.cwd().createFile(config_location, .{ .exclusive = true });
|
||||
defer file.close();
|
||||
|
||||
const bytes_read = try file.readToEndAlloc(allocator, 64 * 1024 * 1024); // read at most 64MB
|
||||
info("config content: {s}", .{bytes_read});
|
||||
|
||||
//const content = try std.fs.openFileAbsolute(config_location, .{ .mode = .read_only });
|
||||
|
||||
}
|
||||
|
||||
fn printHelp() void {
|
||||
const msg =
|
||||
\\Usage: zig-totp [options]
|
||||
\\Usage: zig-totp command [options]
|
||||
\\
|
||||
\\Commands:
|
||||
\\ list List the configured authentiators
|
||||
\\
|
||||
\\Options:
|
||||
\\ --add otpauthUrl Add a new TOTP thingy with an otpauth URL.
|
||||
\\ --config path The path to a config file (default is $XDG_CONFIG_HOME/zig-totp or $HOME/.zig-totp).
|
||||
\\
|
||||
;
|
||||
std.debug.print("{s}", .{msg});
|
||||
}
|
||||
@@ -90,41 +68,7 @@ fn parseArgs(allocator: Allocator, args: []const []const u8) !Args {
|
||||
while (i < args.len) : (i += 1) {
|
||||
const arg = args[i];
|
||||
|
||||
if (std.mem.eql(u8, "--add", arg)) {
|
||||
i += 1;
|
||||
if (i >= args.len) {
|
||||
return error.InvalidOtpAuthUrl;
|
||||
}
|
||||
|
||||
const otpauthUrlCandidate: ?[]const u8 = args[i];
|
||||
|
||||
if (otpauthUrlCandidate == null) {
|
||||
return error.InvalidOtpAuthUrl;
|
||||
}
|
||||
|
||||
const index = std.mem.indexOf(u8, otpauthUrlCandidate.?, "otpauth://totp/");
|
||||
if (index != 0) {
|
||||
return error.InvalidOtpAuthUrl;
|
||||
}
|
||||
|
||||
var it = std.mem.splitSequence(u8, otpauthUrlCandidate.?[15..], "?secret=");
|
||||
|
||||
const name = it.next();
|
||||
const secret = it.next();
|
||||
const empty = it.next();
|
||||
|
||||
if (name != null and secret != null and empty == null) {
|
||||
result.add = OtpAuthUrl{ .name = name.?, .secretEncoded = secret.?, .url = otpauthUrlCandidate.? };
|
||||
} else {
|
||||
return ArgumentError.InvalidOtpAuthUrl;
|
||||
}
|
||||
|
||||
//info("{s}", .{name.?});
|
||||
//info("{s}", .{secret.?});
|
||||
|
||||
//std.debug.print("add: {?s}\n", .{otpauthUrlCandidate});
|
||||
//std.log.info("add: {?s}\n", .{otpauthUrlCandidate});
|
||||
} else if (std.mem.eql(u8, "--config", arg)) {
|
||||
if (std.mem.eql(u8, "--config", arg)) {
|
||||
i += 1;
|
||||
if (i >= args.len) {
|
||||
return error.MissingConfigLocation;
|
||||
@@ -142,7 +86,6 @@ fn parseArgs(allocator: Allocator, args: []const []const u8) !Args {
|
||||
}
|
||||
|
||||
fn parseOtpAuthUrl(url: []const u8) !OtpAuthUrl {
|
||||
//std.debug.print("parsing >{s}<\n", .{url});
|
||||
const index = std.mem.indexOf(u8, url, "otpauth://totp/");
|
||||
if (index != 0) {
|
||||
return error.InvalidOtpAuthUrl;
|
||||
@@ -188,15 +131,6 @@ fn executeGetList(config_location: []const u8) !std.ArrayList([]const u8) {
|
||||
var buf_reader = std.io.bufferedReader(file.reader());
|
||||
var in_stream = buf_reader.reader();
|
||||
|
||||
//_ = try in_stream.readUntilDelimiterOrEofAlloc(allocator, '\n', 1024 * 10);
|
||||
//std.debug.print("line: {s}", .{line1});
|
||||
//
|
||||
//TODO print line
|
||||
//TODO read all lines
|
||||
//TODO parse the names
|
||||
//TODO return list of names
|
||||
//
|
||||
|
||||
var names = std.ArrayList([]const u8).init(allocator);
|
||||
|
||||
while (try in_stream.readUntilDelimiterOrEofAlloc(allocator, '\n', 1024 * 1024)) |line| {
|
||||
@@ -209,15 +143,6 @@ fn executeGetList(config_location: []const u8) !std.ArrayList([]const u8) {
|
||||
return names;
|
||||
}
|
||||
|
||||
test "bla" {
|
||||
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
||||
const allocator = gpa.allocator();
|
||||
const err = parseArgs(allocator, &[_][]const u8{ "path/of/executable", "--add", "not a totp url" });
|
||||
|
||||
try expect(err == ArgumentError.InvalidOtpAuthUrl);
|
||||
//try expect(err == Allocator.Error.OutOfMemory);
|
||||
}
|
||||
|
||||
test "parse command line parameter: 'list'" {
|
||||
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
||||
const allocator = gpa.allocator();
|
||||
|
||||
Reference in New Issue
Block a user