From 543e3137f4d4f9841089923264ff370c54136822 Mon Sep 17 00:00:00 2001 From: Meghan Denny Date: Sun, 2 May 2021 02:26:24 -0700 Subject: [PATCH] switch from zuri/iguanatls to zfetch/json --- deps.zig | 26 +++++++++++++----- src/cmd/zpm_add.zig | 64 ++++++++++++++------------------------------- zig.mod | 8 +++--- 3 files changed, 42 insertions(+), 56 deletions(-) diff --git a/deps.zig b/deps.zig index 331877e78271d863bcff33e118a60e34d5460bde..29ec84c7bb4c34bb4eae407c1ee02aed120d0287 100644 --- a/deps.zig +++ b/deps.zig @@ -30,9 +30,13 @@ pub const _ids = .{ "8mdbh0zuneb0i3hs5jby5je0heem1i6yxusl7c8y8qx68hqc", "s84v9o48ucb0xq0cmzq0cn433hgw0iaqztugja16h8bzxu3h", "2ta738wrqbaqzl3iwzoo8nj35k9ynwz5p5iyz80ryrpp4ttf", - "2b7mq571jmq31ktmpigopu29480iw245heueajgxzxn7ab8o", - "csbnipaad8n77buaszsnjvlmn6j173fl7pkprsctelswjywe", "0npcrzfdlrvkf44mzjo8bduj9gmqyefo0j3rstt6b0pm2r6r", + "ejw82j2ipa0eul25ohgdh6yy5nkrtn2pf0rq18m0079w6wj7", + "9k24gimke1anv665ilg4si32ayl3dsaqgmdfdpu1ceoky8tl", + "csbnipaad8n77buaszsnjvlmn6j173fl7pkprsctelswjywe", + "yyhw90zkzgmubwpp87n0pzf936n850an66y1c6qan5y6sogv", + "1vv8d3ee4k0g7kmy1bhi9qon55bj1arxq9181hc8sesa7x3f", + "ocmr9rtohgccd6gm6tp8b1yzylyzkqwvo1q4btrsvj0cse9y", }; pub const _paths = .{ @@ -40,18 +44,26 @@ pub const _paths = .{ "/v/git/github.com/yaml/libyaml/tag-0.2.5/", "/v/git/github.com/nektro/zig-ansi/commit-25039ca/", "/v/git/github.com/ziglibs/known-folders/commit-f0f4188/", - "/v/git/github.com/Vexu/zuri/commit-41bcd78/", - "/v/git/github.com/alexnask/iguanaTLS/commit-1767e48/", "/v/git/github.com/nektro/zig-licenses/commit-1a19e4b/", + "/v/git/github.com/truemedian/zfetch/commit-5b9d6a2/", + "/git/github.com/truemedian/hzzp/", + "/git/github.com/alexnask/iguanaTLS/", + "/git/github.com/MasterQ32/zig-network/", + "/git/github.com/MasterQ32/zig-uri/", + "/v/git/github.com/nektro/zig-json/commit-ad4232e/", }; pub const package_data = struct { pub const _s84v9o48ucb0xq0cmzq0cn433hgw0iaqztugja16h8bzxu3h = std.build.Pkg{ .name = "ansi", .path = cache ++ "/v/git/github.com/nektro/zig-ansi/commit-25039ca/src/lib.zig", .dependencies = &[_]std.build.Pkg{ } }; pub const _2ta738wrqbaqzl3iwzoo8nj35k9ynwz5p5iyz80ryrpp4ttf = std.build.Pkg{ .name = "known-folders", .path = cache ++ "/v/git/github.com/ziglibs/known-folders/commit-f0f4188/known-folders.zig", .dependencies = &[_]std.build.Pkg{ } }; - pub const _2b7mq571jmq31ktmpigopu29480iw245heueajgxzxn7ab8o = std.build.Pkg{ .name = "zuri", .path = cache ++ "/v/git/github.com/Vexu/zuri/commit-41bcd78/src/zuri.zig", .dependencies = &[_]std.build.Pkg{ } }; - pub const _csbnipaad8n77buaszsnjvlmn6j173fl7pkprsctelswjywe = std.build.Pkg{ .name = "iguanatls", .path = cache ++ "/v/git/github.com/alexnask/iguanaTLS/commit-1767e48/src/main.zig", .dependencies = &[_]std.build.Pkg{ } }; pub const _0npcrzfdlrvkf44mzjo8bduj9gmqyefo0j3rstt6b0pm2r6r = std.build.Pkg{ .name = "licenses", .path = cache ++ "/v/git/github.com/nektro/zig-licenses/commit-1a19e4b/src/lib.zig", .dependencies = &[_]std.build.Pkg{ } }; - pub const _89ujp8gq842x6mzok8feypwze138n2d96zpugw44hcq7406r = std.build.Pkg{ .name = "zigmod", .path = cache ++ "/../../src/lib.zig", .dependencies = &[_]std.build.Pkg{ _s84v9o48ucb0xq0cmzq0cn433hgw0iaqztugja16h8bzxu3h, _2ta738wrqbaqzl3iwzoo8nj35k9ynwz5p5iyz80ryrpp4ttf, _2b7mq571jmq31ktmpigopu29480iw245heueajgxzxn7ab8o, _csbnipaad8n77buaszsnjvlmn6j173fl7pkprsctelswjywe, _0npcrzfdlrvkf44mzjo8bduj9gmqyefo0j3rstt6b0pm2r6r, } }; + pub const _9k24gimke1anv665ilg4si32ayl3dsaqgmdfdpu1ceoky8tl = std.build.Pkg{ .name = "hzzp", .path = cache ++ "/git/github.com/truemedian/hzzp/src/main.zig", .dependencies = &[_]std.build.Pkg{ } }; + pub const _csbnipaad8n77buaszsnjvlmn6j173fl7pkprsctelswjywe = std.build.Pkg{ .name = "iguanaTLS", .path = cache ++ "/git/github.com/alexnask/iguanaTLS/src/main.zig", .dependencies = &[_]std.build.Pkg{ } }; + pub const _yyhw90zkzgmubwpp87n0pzf936n850an66y1c6qan5y6sogv = std.build.Pkg{ .name = "network", .path = cache ++ "/git/github.com/MasterQ32/zig-network/network.zig", .dependencies = &[_]std.build.Pkg{ } }; + pub const _1vv8d3ee4k0g7kmy1bhi9qon55bj1arxq9181hc8sesa7x3f = std.build.Pkg{ .name = "uri", .path = cache ++ "/git/github.com/MasterQ32/zig-uri/uri.zig", .dependencies = &[_]std.build.Pkg{ } }; + pub const _ejw82j2ipa0eul25ohgdh6yy5nkrtn2pf0rq18m0079w6wj7 = std.build.Pkg{ .name = "zfetch", .path = cache ++ "/v/git/github.com/truemedian/zfetch/commit-5b9d6a2/src/main.zig", .dependencies = &[_]std.build.Pkg{ _9k24gimke1anv665ilg4si32ayl3dsaqgmdfdpu1ceoky8tl, _csbnipaad8n77buaszsnjvlmn6j173fl7pkprsctelswjywe, _yyhw90zkzgmubwpp87n0pzf936n850an66y1c6qan5y6sogv, _1vv8d3ee4k0g7kmy1bhi9qon55bj1arxq9181hc8sesa7x3f, } }; + pub const _ocmr9rtohgccd6gm6tp8b1yzylyzkqwvo1q4btrsvj0cse9y = std.build.Pkg{ .name = "json", .path = cache ++ "/v/git/github.com/nektro/zig-json/commit-ad4232e/src/lib.zig", .dependencies = &[_]std.build.Pkg{ } }; + pub const _89ujp8gq842x6mzok8feypwze138n2d96zpugw44hcq7406r = std.build.Pkg{ .name = "zigmod", .path = cache ++ "/../../src/lib.zig", .dependencies = &[_]std.build.Pkg{ _s84v9o48ucb0xq0cmzq0cn433hgw0iaqztugja16h8bzxu3h, _2ta738wrqbaqzl3iwzoo8nj35k9ynwz5p5iyz80ryrpp4ttf, _0npcrzfdlrvkf44mzjo8bduj9gmqyefo0j3rstt6b0pm2r6r, _ejw82j2ipa0eul25ohgdh6yy5nkrtn2pf0rq18m0079w6wj7, _ocmr9rtohgccd6gm6tp8b1yzylyzkqwvo1q4btrsvj0cse9y, } }; }; pub const packages = &[_]std.build.Pkg{ diff --git a/src/cmd/zpm_add.zig b/src/cmd/zpm_add.zig index 413b9336da873745800dbb26ee6b3d031f7826ef..1c411ab242d89074434ab38d3cbb959bfdd51aa8 100644 --- a/src/cmd/zpm_add.zig +++ b/src/cmd/zpm_add.zig @@ -1,8 +1,9 @@ const std = @import("std"); const gpa = std.heap.c_allocator; -const zuri = @import("zuri"); -const iguanatls = @import("iguanatls"); +const zfetch = @import("zfetch"); +const json = @import("json"); + const u = @import("./../util/index.zig"); // @@ -20,47 +21,20 @@ pub const Zpm = struct { }; pub fn execute(args: [][]u8) !void { - const url = try zuri.Uri.parse("https://zpm.random-projects.net:443/api/packages", true); + const url = "https://zpm.random-projects.net/api/packages"; - const sock = try std.net.tcpConnectToHost(gpa, url.host.name, url.port.?); - defer sock.close(); + const req = try zfetch.Request.init(gpa, url, null); + defer req.deinit(); - var client = try iguanatls.client_connect(.{ - .reader = sock.reader(), - .writer = sock.writer(), - .cert_verifier = .none, - .temp_allocator = gpa, - .ciphersuites = iguanatls.ciphersuites.all, - }, url.host.name); - defer client.close_notify() catch {}; + try req.do(.GET, null, null); + const r = req.reader(); - const w = client.writer(); - try w.print("GET {s} HTTP/1.1\r\n", .{url.path}); - try w.print("Host: {s}:{}\r\n", .{ url.host.name, url.port.? }); - try w.writeAll("Accept: application/json; charset=UTF-8\r\n"); - try w.writeAll("Connection: close\r\n"); - try w.writeAll("\r\n"); - - const r = client.reader(); - var buf: [1]u8 = undefined; - const data = &std.ArrayList(u8).init(gpa); - while (true) { - const len = try r.read(&buf); - if (len == 0) { - break; - } - try data.appendSlice(buf[0..len]); - } - - const index = std.mem.indexOf(u8, data.items, "\r\n\r\n").?; - const html_contents = data.items[index..]; - - var stream = std.json.TokenStream.init(html_contents[4..]); - const res = try std.json.parse([]Zpm.Package, &stream, .{ .allocator = gpa }); + const body_content = try r.readAllAlloc(gpa, std.math.maxInt(usize)); + const val = try json.parse(gpa, body_content); const found = blk: { - for (res) |pkg| { - if (std.mem.eql(u8, pkg.name, args[0])) { + for (val.Array) |pkg| { + if (std.mem.eql(u8, pkg.get("name").?.String, args[0])) { break :blk pkg; } } @@ -68,12 +42,12 @@ pub fn execute(args: [][]u8) !void { unreachable; }; - u.assert(found.root_file != null, "package must have an entry point to be able to be added to your dependencies", .{}); + u.assert(found.get("root_file") != null, "package must have an entry point to be able to be added to your dependencies", .{}); const self_module = try u.ModFile.init(gpa, "zig.mod"); for (self_module.deps) |dep| { - if (std.mem.eql(u8, dep.name, found.name)) { - u.assert(false, "dependency with name '{s}' already exists in your dependencies", .{found.name}); + if (std.mem.eql(u8, dep.name, found.get("name").?.String)) { + u.assert(false, "dependency with name '{s}' already exists in your dependencies", .{found.get("name").?.String}); } } @@ -82,9 +56,9 @@ pub fn execute(args: [][]u8) !void { const file_w = file.writer(); try file_w.print("\n", .{}); - try file_w.print(" - src: git {s}\n", .{found.git}); - try file_w.print(" name: {s}\n", .{found.name}); - try file_w.print(" main: {s}\n", .{found.root_file.?[1..]}); + try file_w.print(" - src: git {s}\n", .{found.get("git").?.String}); + try file_w.print(" name: {s}\n", .{found.get("name").?.String}); + try file_w.print(" main: {s}\n", .{found.get("root_file").?.String[1..]}); - std.log.info("Successfully added package {s} by {s}", .{ found.name, found.author }); + std.log.info("Successfully added package {s} by {s}", .{ found.get("name").?.String, found.get("author").?.String }); } diff --git a/zig.mod b/zig.mod index 0b38acffff893b6da310a47980f4e7082a865015..c7a8140543a73227bc2c7a72972d5b6581858e4b 100644 --- a/zig.mod +++ b/zig.mod @@ -32,11 +32,11 @@ dependencies: - src: git https://github.com/ziglibs/known-folders commit-f0f4188 - - src: git https://github.com/Vexu/zuri commit-41bcd78 - - - src: git https://github.com/alexnask/iguanaTLS commit-1767e48 - - src: git https://github.com/nektro/zig-licenses commit-1a19e4b + - src: git https://github.com/truemedian/zfetch commit-5b9d6a2 + + - src: git https://github.com/nektro/zig-json commit-ad4232e + dev_dependencies: - src: git https://github.com/nektro/zig-ansi commit-25039ca -- 2.54.0