From d9ea5cf7d656cc76fa0b40b82f499ec552e30dee Mon Sep 17 00:00:00 2001 From: kogeletey Date: Sat, 24 Aug 2024 16:07:28 +0300 Subject: [PATCH] feat: fetching openapi schema and types of lock file chore: update zig to 0.13 --- .gitignore | 2 +- .mise.toml | 2 +- build.zig | 4 ++-- src/main.zig | 40 ++++++++++++++++++++++++++++++++++++---- 4 files changed, 40 insertions(+), 8 deletions(-) diff --git a/.gitignore b/.gitignore index e73c965..3389c86 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,2 @@ -zig-cache/ +.zig-cache/ zig-out/ diff --git a/.mise.toml b/.mise.toml index 15cc23c..8f83f92 100644 --- a/.mise.toml +++ b/.mise.toml @@ -1,2 +1,2 @@ [tools] -zig = "0.11.0" +zig = "0.13" diff --git a/build.zig b/build.zig index a3847cc..d78f743 100644 --- a/build.zig +++ b/build.zig @@ -19,7 +19,7 @@ pub fn build(b: *std.Build) void { .name = "wacli", // In this case the main source file is merely a path, however, in more // complicated build scripts, this could be a generated file. - .root_source_file = .{ .path = "src/main.zig" }, + .root_source_file = b.path("src/main.zig"), .target = target, .optimize = optimize, }); @@ -55,7 +55,7 @@ pub fn build(b: *std.Build) void { // Creates a step for unit testing. This only builds the test executable // but does not run it. const unit_tests = b.addTest(.{ - .root_source_file = .{ .path = "src/main.zig" }, + .root_source_file = b.path("src/main.zig"), .target = target, .optimize = optimize, }); diff --git a/src/main.zig b/src/main.zig index 8784280..08f7140 100644 --- a/src/main.zig +++ b/src/main.zig @@ -21,6 +21,20 @@ const Aliases = struct { content: []const u8, }; +const Tools = std.json.ArrayHashMap(struct { + integrity: [std.crypto.hash.sha256.Sha256.digest_length]u8, + source: []const u8, + installPath: []const u8, + alias: ?[]const u8 = null, + version: []const u8, +}); + +const LockFile = struct { + fileVersion: i8 = 1, + bins: Tools, + ains: Tools +}; + const OauthFlows = struct { implict: struct { authrizationUrl: []const u8, scopes: std.json.ArrayHashMap([]const u8) } }; const WellKnownSettings = struct { @@ -34,8 +48,6 @@ const WellKnownSchema = struct { api: ?[]const u8 = null, registry: ?bool = fals const UserSettings = struct { db_path: []const u8 = "$HOME/.cache/wacrd.db", install_dir: []const u8 = "$HOME/.local/bin", uri_schemes: ?std.json.ArrayHashMap([]const u8) = null }; -const json = std.json; - pub fn getHomeDir() !?std.fs.Dir { return try std.fs.openDirAbsolute(std.posix.getenv("HOME") orelse { return null; @@ -48,17 +60,37 @@ pub fn getXDGConfigHomeDir() !?std.fs.Dir { }, .{ .iterate = true }); } -pub fn readTypedConfig(allocator: std.mem.Allocator, comptime T: type, filePath: []const u8, buffer_size: usize) !json.Parsed(T) { +pub fn readTypedConfig(allocator: std.mem.Allocator, comptime T: type, filePath: []const u8, buffer_size: usize) !std.json.Parsed(T) { const contents = try std.fs.cwd().readFileAlloc(allocator, filePath, buffer_size); defer allocator.free(contents); - return json.parseFromSlice(T, allocator, contents, .{ + return std.json.parseFromSlice(T, allocator, contents, .{ .allocate = .alloc_always, .ignore_unknown_fields = true, }); } +pub fn fetchResource(allocator: std.mem.Allocator, api: ?[]const u8,) !struct{ std.http.Status, std.ArrayList(u8)} { + const http = std.http; + var client = http.Client{ .allocator = allocator }; + defer client.deinit(); + var list = std.ArrayList(u8).init(allocator); + const fetch = try client.fetch(.{.location = .{.url = api.?}, .method = .GET, .response_storage = .{.dynamic = &list}}); + return .{ fetch.status, list}; +} + pub fn main() !void {} +test "fetch openapi schema" { + const parsedWaCLISchema = try readTypedConfig(std.testing.allocator, WellKnownSchema, "examples/git.0ut0f.space/.well-known/wacli.json", 2048); + defer parsedWaCLISchema.deinit(); + const waCLISchema = parsedWaCLISchema.value; + const status, const data = try fetchResource(std.testing.allocator,waCLISchema.api); + defer data.deinit(); + + std.debug.print("{u}\n",.{status}); + std.debug.print("{s}\n",.{data.items}); +} + test "parse registry schema:" { const parsedExampleSettings = try readTypedConfig(std.testing.allocator, UserSettings, "examples/settings.json", 512); const parsedWaCLISchema = try readTypedConfig(std.testing.allocator, WellKnownSchema, "examples/wacli.ofs.lol/.well-known/wacli.json", 512);