63 lines
1.9 KiB
Zig
63 lines
1.9 KiB
Zig
//! Generate skill.json from template by substituting Lambda ARN
|
|
//!
|
|
//! Usage: gen-skill-json <deploy-output.json> <skill.template.json>
|
|
//! Outputs the generated skill.json to stdout.
|
|
|
|
const std = @import("std");
|
|
|
|
pub fn main() !void {
|
|
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
|
defer _ = gpa.deinit();
|
|
const allocator = gpa.allocator();
|
|
|
|
const args = try std.process.argsAlloc(allocator);
|
|
defer std.process.argsFree(allocator, args);
|
|
|
|
if (args.len != 3) {
|
|
std.debug.print("Usage: {s} <deploy-output.json> <skill.template.json>\n", .{args[0]});
|
|
std.process.exit(1);
|
|
}
|
|
|
|
const deploy_output_path = args[1];
|
|
const template_path = args[2];
|
|
|
|
// Read deploy output JSON
|
|
const deploy_output = try readFile(allocator, deploy_output_path);
|
|
defer allocator.free(deploy_output);
|
|
|
|
// Parse to extract ARN
|
|
const parsed = try std.json.parseFromSlice(std.json.Value, allocator, deploy_output, .{});
|
|
defer parsed.deinit();
|
|
|
|
const arn = parsed.value.object.get("arn") orelse {
|
|
std.debug.print("Error: deploy output missing 'arn' field\n", .{});
|
|
std.process.exit(1);
|
|
};
|
|
|
|
const arn_str = switch (arn) {
|
|
.string => |s| s,
|
|
else => {
|
|
std.debug.print("Error: 'arn' field is not a string\n", .{});
|
|
std.process.exit(1);
|
|
},
|
|
};
|
|
|
|
// Read template
|
|
const template = try readFile(allocator, template_path);
|
|
defer allocator.free(template);
|
|
|
|
// Replace placeholder with ARN
|
|
const result = try std.mem.replaceOwned(u8, allocator, template, "{{LAMBDA_ARN}}", arn_str);
|
|
defer allocator.free(result);
|
|
|
|
// Write to stdout
|
|
const stdout = std.fs.File.stdout();
|
|
try stdout.writeAll(result);
|
|
}
|
|
|
|
fn readFile(allocator: std.mem.Allocator, path: []const u8) ![]u8 {
|
|
const file = try std.fs.cwd().openFile(path, .{});
|
|
defer file.close();
|
|
|
|
return try file.readToEndAlloc(allocator, 1024 * 1024);
|
|
}
|