initial commit
This commit is contained in:
109
src/main.zig
Normal file
109
src/main.zig
Normal file
@@ -0,0 +1,109 @@
|
||||
const std = @import("std");
|
||||
const info = std.log.info;
|
||||
const print = std.debug.print;
|
||||
const Allocator = std.mem.Allocator;
|
||||
|
||||
// otpauth://totp/AWS+Dev?secret=47STA47VFCMMLLWOLHWO3KY7MYNC36MLCDTHOLIYKJCTTSSAMKVM7YA3VWT2AJEP&digits=6&icon=Amazon
|
||||
|
||||
pub fn main() !void {
|
||||
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
||||
const allocator = gpa.allocator();
|
||||
std.log.info("\n\nstart\n", .{});
|
||||
|
||||
const args = try std.process.argsAlloc(allocator);
|
||||
|
||||
std.debug.print("arguments: {s}\n", .{args});
|
||||
const x: Args = try parseArgs(args);
|
||||
print("parsed Args: {?any}\n", x);
|
||||
if (x.add != null) {
|
||||
info("add:", .{});
|
||||
info("name: {s}", .{x.add.?.name});
|
||||
info("secret: {s}", .{x.add.?.secretEncoded});
|
||||
|
||||
const config_location = try configLocation(allocator);
|
||||
info("config location: {s}", .{config_location});
|
||||
}
|
||||
}
|
||||
|
||||
const Args = struct {
|
||||
add: ?OtpAuthUrl,
|
||||
};
|
||||
|
||||
const OtpAuthUrl = struct {
|
||||
name: []const u8,
|
||||
secretEncoded: []const u8,
|
||||
url: []const u8,
|
||||
};
|
||||
|
||||
const ArgumentError = error{InvalidOtpAuthUrl};
|
||||
|
||||
fn parseArgs(args: [][:0]u8) !Args {
|
||||
var result = Args{
|
||||
.add = null,
|
||||
};
|
||||
var i: u17 = 1;
|
||||
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: ?[:0]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 {
|
||||
std.debug.print("unknown parameter: {s}\n", .{arg});
|
||||
std.process.exit(1);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
fn configLocation(allocator: Allocator) Allocator.Error![]const u8 {
|
||||
const xdg_config_home: ?[]const u8 = std.process.getEnvVarOwned(allocator, "XDG_CONFIG_HOME") catch null;
|
||||
|
||||
if (xdg_config_home) |base| {
|
||||
const config_location = try std.mem.concat(allocator, u8, &[_][]const u8{ base, "/zig-totp" });
|
||||
info("config_location: {s}", .{config_location});
|
||||
return config_location;
|
||||
}
|
||||
|
||||
const home = std.process.getEnvVarOwned(allocator, "HOME") catch null;
|
||||
const base = home orelse unreachable;
|
||||
const config_location = try std.mem.concat(allocator, u8, &[_][]const u8{ base, "/.zig-totp" });
|
||||
info("config_location: {s}", .{config_location});
|
||||
return config_location;
|
||||
}
|
||||
|
||||
fn exit(returnValue: u8, message: []const u8) void {
|
||||
std.log.warn("{s}", .{message});
|
||||
std.process.exit(returnValue);
|
||||
}
|
||||
Reference in New Issue
Block a user