Skip to content

Commit

Permalink
update to zig 0.13
Browse files Browse the repository at this point in the history
  • Loading branch information
ivansantiagojr committed Aug 26, 2024
1 parent 3438f6c commit 25da26e
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 39 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
- name: Install Zig
uses: goto-bus-stop/setup-zig@v2
with:
version: 0.11.0
version: 0.13.0

- name: Build
run: zig build -Dtarget=${{ matrix.TARGET }}
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
- name: Install Zig
uses: goto-bus-stop/setup-zig@v2
with:
version: 0.11.0
version: 0.13.0

- name: Build
run: zig build ${{ matrix.OPTIMIZE }}
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
### zig ###
# Zig programming language

zig-cache/
.zig-cache/
zig-out/
build/
build-*/
Expand Down
18 changes: 1 addition & 17 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub fn build(b: *std.Build) void {
.name = "zignr",
// 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,
});
Expand Down Expand Up @@ -51,20 +51,4 @@ pub fn build(b: *std.Build) void {
// This will evaluate the `run` step rather than the default, which is "install".
const run_step = b.step("run", "Run the app");
run_step.dependOn(&run_cmd.step);

// 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" },
.target = target,
.optimize = optimize,
});

const run_unit_tests = b.addRunArtifact(unit_tests);

// Similar to creating the run step earlier, this exposes a `test` step to
// the `zig build --help` menu, providing a way for the user to request
// running the unit tests.
const test_step = b.step("test", "Run unit tests");
test_step.dependOn(&run_unit_tests.step);
}
27 changes: 8 additions & 19 deletions src/main.zig
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
const std = @import("std");
const print = std.io.getStdOut().writer();
const http = std.http;
const is_zig_11 = @import("builtin").zig_version.minor == 11;

pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const allocator = gpa.allocator();

var args = try std.process.argsAlloc(allocator);
const args = try std.process.argsAlloc(allocator);
defer std.process.argsFree(allocator, args);

if (args.len != 2) {
Expand All @@ -22,31 +21,21 @@ pub fn main() !void {
}
const language = std.mem.sliceTo(args[1], 0);

var headers = http.Headers{ .allocator = allocator };
defer headers.deinit();

var client = http.Client{ .allocator = allocator };
defer client.deinit();
var base_url = "https://www.toptal.com/developers/gitignore/api/";
const base_url = "https://www.toptal.com/developers/gitignore/api/";
var url_buffer: [256]u8 = undefined;
const url_to_request = try std.fmt.bufPrint(&url_buffer, "{s}{s}", .{ base_url, language });

const uri = try std.Uri.parse(url_to_request);
var req = if (is_zig_11) blk: {
var req = try client.request(.GET, uri, headers, .{});
errdefer req.deinit();

try req.start();
break :blk req;
} else blk: {
var req = try client.open(.GET, uri, headers, .{});
errdefer req.deinit();

try req.send(.{});
break :blk req;
};
const buf = try allocator.alloc(u8, 1024 * 1014 * 4);
defer allocator.free(buf);

var req = try client.open(.GET, uri, .{ .server_header_buffer = buf });
defer req.deinit();

try req.send();
try req.finish();
try req.wait();

if (req.response.status != .ok) {
Expand Down

0 comments on commit 25da26e

Please sign in to comment.