Skip to content
1        10        20        30        40        50        60        70        80        90        100       1101//! Installing the embedded skill into a repository.2const std = @import("std");3const cli = @import("cli.zig");4const skill = @import("skill.zig");56const max_existing_bytes = 4 * 1024 * 1024;78pub const Outcome = enum {9    /// The file did not exist and was written.10    created,11    /// The file existed with other content and the skill was appended.12    appended,13    /// The file existed and --force replaced it.14    replaced,15    /// The file already contains the skill.16    unchanged,17    /// --check ran but the skill is not installed.18    absent,19};2021/// `.opencode/skills/silen-infra/SKILL.md` relative to the current directory.22pub fn defaultPath(allocator: std.mem.Allocator) ![]u8 {23    return std.fs.path.join(allocator, &.{ ".opencode", "skills", skill.name, skill.file_name });24}2526/// Creates or appends the skill at `target`. Never writes outside the target.27pub fn run(28    allocator: std.mem.Allocator,29    io: std.Io,30    target: []const u8,31    options: cli.Install,32) !Outcome {33    const existing = try readOrNull(allocator, io, target);3435    if (options.check) {36        if (existing) |text| {37            if (skill.contains(text)) return .unchanged;38        }39        return .absent;40    }4142    if (existing) |text| {43        if (skill.contains(text)) return .unchanged;44        if (options.force) {45            try writeFile(io, target, skill.full);46            return .replaced;47        }48        const merged = try skill.appended(allocator, text);49        try writeFile(io, target, merged);50        return .appended;51    }5253    try writeFile(io, target, skill.full);54    return .created;55}5657fn readOrNull(58    allocator: std.mem.Allocator,59    io: std.Io,60    path: []const u8,61) !?[]u8 {62    return std.Io.Dir.cwd().readFileAlloc(io, path, allocator, .limited(max_existing_bytes)) catch |err| switch (err) {63        error.FileNotFound => null,64        else => return err,65    };66}6768fn writeFile(io: std.Io, path: []const u8, data: []const u8) !void {69    if (std.fs.path.dirname(path)) |dir| {70        if (dir.len != 0) try std.Io.Dir.cwd().createDirPath(io, dir);71    }72    try std.Io.Dir.cwd().writeFile(io, .{ .sub_path = path, .data = data });73}7475test "default path is the opencode skills directory" {76    const path = try defaultPath(std.testing.allocator);77    defer std.testing.allocator.free(path);78    try std.testing.expectEqualStrings(79        ".opencode/skills/silen-infra/SKILL.md",80        path,81    );82}