Accept password at command line for auth user create

Intended for local development only
This commit is contained in:
Bob Farrell 2024-11-13 20:35:50 +00:00
parent f9c6f9f38f
commit c8ae44b508
3 changed files with 35 additions and 21 deletions

View File

@ -349,6 +349,9 @@ pub fn jetzigInit(b: *std.Build, exe: *std.Build.Step.Compile, options: JetzigIn
if (b.option([]const u8, "auth_username", "Auth username")) |username| { if (b.option([]const u8, "auth_username", "Auth username")) |username| {
run_auth_user_create_cmd.addArg(username); run_auth_user_create_cmd.addArg(username);
} }
if (b.option([]const u8, "auth_password", "Auth password")) |password| {
run_auth_user_create_cmd.addArg(password);
}
const exe_database = b.addExecutable(.{ const exe_database = b.addExecutable(.{
.name = "database", .name = "database",

View File

@ -60,13 +60,16 @@ pub fn run(
std.debug.print("Missing argument. Expected an email/username parameter.\n", .{}); std.debug.print("Missing argument. Expected an email/username parameter.\n", .{});
break :blk error.JetzigCommandError; break :blk error.JetzigCommandError;
} else { } else {
try util.execCommand(allocator, &.{ var argv = std.ArrayList([]const u8).init(allocator);
"zig", try argv.append("zig");
"build", try argv.append("build");
util.environmentBuildOption(main_options.options.environment), try argv.append(util.environmentBuildOption(main_options.options.environment));
try std.mem.concat(allocator, u8, &.{ "-Dauth_username=", sub_args[0] }), try argv.append(try std.mem.concat(allocator, u8, &.{ "-Dauth_username=", sub_args[0] }));
"jetzig:auth:user:create", if (sub_args.len > 1) {
}); try argv.append(try std.mem.concat(allocator, u8, &.{ "-Dauth_password=", sub_args[1] }));
}
try argv.append("jetzig:auth:user:create");
try util.execCommand(allocator, argv.items);
} }
}, },
}; };

View File

@ -42,21 +42,29 @@ pub fn main() !void {
const stdin = std.io.getStdIn(); const stdin = std.io.getStdIn();
const reader = stdin.reader(); const reader = stdin.reader();
if (stdin.isTty()) std.debug.print("Enter password: ", .{}); const password = if (stdin.isTty() and args.len < 4) blk: {
std.debug.print("Enter password: ", .{});
var buf: [1024]u8 = undefined; var buf: [1024]u8 = undefined;
if (try reader.readUntilDelimiterOrEof(&buf, '\n')) |input| { if (try reader.readUntilDelimiterOrEof(&buf, '\n')) |input| {
const password = std.mem.trim(u8, input, &std.ascii.whitespace); break :blk std.mem.trim(u8, input, &std.ascii.whitespace);
const email = args[2]; } else {
std.debug.print("Blank password. Exiting.\n", .{});
try repo.insert(std.enums.nameCast(std.meta.DeclEnum(Schema), model), .{ return;
.email = email, }
.password_hash = try hashPassword(allocator, password), } else if (args.len >= 4)
}); args[3]
std.debug.print("Created user: `{s}`.\n", .{email}); else {
} else {
std.debug.print("Blank password. Exiting.\n", .{}); std.debug.print("Blank password. Exiting.\n", .{});
} return;
};
const email = args[2];
try repo.insert(std.enums.nameCast(std.meta.DeclEnum(Schema), model), .{
.email = email,
.password_hash = try hashPassword(allocator, password),
});
std.debug.print("Created user: `{s}`.\n", .{email});
}, },
} }
} }