╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ net.zig ──────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1381 var dest_entry_dir = try dest_dir.openDir(io, entry.name, .{ .access_sub_paths = true, .iterate = true, .follow_symlinks = false }); 1382 defer dest_entry_dir.close(io); 1383 var src_entry_dir = try src_dir.openDir(io, entry.name, .{ .access_sub_paths = true, .iterate = true, .follow_symlinks = false }); 1384 defer src_entry_dir.close(io); 1385 try copyDir(io, src_entry_dir, dest_entry_dir); 1386 }, 1387 else => {}, 1388 } 1389 } 1390 } 1391 1392 // the dev SSH public key matching the private key written by runServer 1393 const admin_ssh_key = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIKeIs8mJqigBZ5y84J4COgnAJJ5bHPKy+lM2SliMXbYm radar@roark"; 1394 1395 // create the admin event repo with a user holding the dev SSH public key 1396 fn setupAdmin(io: std.Io, allocator: std.mem.Allocator, data_dir_name: []const u8) !void { 1397 const cwd_path = try std.process.currentPathAlloc(io, allocator); 1398 defer allocator.free(cwd_path); 1399 1400 const admin_repo_path = try std.fs.path.join(allocator, &.{ cwd_path, data_dir_name, "admin" }); 1401 defer allocator.free(admin_repo_path); 1402 1403 var repo = try rp.Repo(.xit, evt.admin_repo_opts).init(io, allocator, .{ .path = admin_repo_path }); 1404 defer repo.deinit(io, allocator); 1405 1406 try repo.addConfig(io, allocator, .{ .name = "user.name", .value = "haxy" }); 1407 try repo.addConfig(io, allocator, .{ .name = "user.email", .value = "admin@haxy" }); 1408 1409 var prng = std.Random.DefaultPrng.init(std.testing.random_seed); 1410 const user_id = evt.EventWithId.randomId(prng.random()); 1411 1412 var password_hash_buf: [evt.User.password_hash_max_len]u8 = undefined; 1413 const password_hash = try evt.User.hashPassword("password", &password_hash_buf, io); 1414 1415 try evt.consume(.xit, evt.admin_repo_opts, io, allocator, &repo, evt.events_ref, &[_]evt.EventWithId{.{ 1416 .id = std.fmt.bytesToHex(user_id, .lower), 1417 .author = .{ .name = "admin", .email = "admin@example.test" }, 1418 .event = .{ .user = .{ 1419 .name = "admin", 1420 .email = "admin@example.test", 1421 .password_hash = password_hash, 1422 .ssh_keys = admin_ssh_key, 1423 } }, 1424 }}); 1425 } 1426 1427 // resolve admin/<repo_name> to its on-disk directory under <data_dir>/repos via 1428 // the event store 1429 fn repoOnDiskPath( 1430 io: std.Io, 1431 allocator: std.mem.Allocator, 1432 data_dir_name: []const u8, 1433 repo_name: []const u8, 1434 create: bool, 1435 ) !?[]u8 { 1436 const cwd_path = try std.process.currentPathAlloc(io, allocator); 1437 defer allocator.free(cwd_path); 1438 1439 const admin_repo_path = try std.fs.path.join(allocator, &.{ cwd_path, data_dir_name, "admin" }); 1440 defer allocator.free(admin_repo_path); 1441 1442 const event_id_hex = (try evt.resolveOrCreateRepo(io, allocator, admin_repo_path, "admin", repo_name, create)) orelse return null; 1443 return try std.fs.path.join(allocator, &.{ cwd_path, data_dir_name, "repos", &event_id_hex }); 1444 } 1445 1446 // build the remote URL addressing admin/<repo_name> over the given transport 1447 fn remoteUrl( 1448 comptime transport_def: net.TransportDefinition, 1449 allocator: std.mem.Allocator, 1450 repo_name: []const u8, 1451 port: u16, 1452 ) ![]u8 { 1453 return switch (transport_def) { 1454 .file => unreachable, 1455 .wire => |wire_kind| switch (wire_kind) { 1456 .http => try std.fmt.allocPrint(allocator, "http://localhost:{}/admin/{s}", .{ port, repo_name }), 1457 .raw => try std.fmt.allocPrint(allocator, "git://localhost:{}/admin/{s}", .{ port, repo_name }), 1458 .ssh => try std.fmt.allocPrint(allocator, "git@localhost:admin/{s}", .{repo_name}), 1459 }, 1460 }; 1461 } 1462 1463 // the ssh command the haxy git client should invoke, or null when not using 1464 // ssh. haxy generates a fresh host key on every run, so accept the unknown host 1465 // and skip writing to known_hosts. 1466 fn sshCommand( 1467 is_ssh: bool, 1468 allocator: std.mem.Allocator, 1469 cwd_path: []const u8, 1470 temp_dir_name: []const u8, 1471 port: u16, 1472 ) !?[]u8 { 1473 if (!is_ssh) return null; 1474 1475 const priv_key_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "key" }); 1476 defer allocator.free(priv_key_path); 1477 1478 return try std.fmt.allocPrint(allocator, "ssh -p {} -o BatchMode=yes -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o LogLevel=ERROR -o IdentitiesOnly=yes -o IdentityFile=\"{s}\"", .{ port + 1, priv_key_path }); 1479 } 1480