Skip to content
This repository has been archived by the owner on Aug 1, 2023. It is now read-only.

Commit

Permalink
add repository arg to add and publish subcommands (#159)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattnite authored Jan 31, 2022
1 parent 3c19e88 commit 4b4e510
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 13 deletions.
2 changes: 1 addition & 1 deletion libs/version
Submodule version updated 3 files
+1 −1 build.zig
+5 −6 gyro.zzz
+4 −1 src/main.zig
16 changes: 12 additions & 4 deletions src/api.zig
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub fn getLatest(
range: ?version.Range,
) !version.Semver {
const url = if (range) |r|
try std.fmt.allocPrintZ(allocator, "https://{s}/pkgs/{s}/{s}/latest?v={}", .{
try std.fmt.allocPrintZ(allocator, "https://{s}/pkgs/{s}/{s}/latest?v={u}", .{
repository,
user,
package,
Expand Down Expand Up @@ -354,6 +354,7 @@ pub fn pollDeviceCode(

pub fn postPublish(
allocator: Allocator,
repository_opt: ?[]const u8,
access_token: []const u8,
pkg: *Package,
) !void {
Expand All @@ -368,7 +369,10 @@ pub fn postPublish(
std.fs.cwd().deleteFile(filename) catch {};
}

const url = "https://" ++ utils.default_repo ++ "/publish";
const repository = repository_opt orelse utils.default_repo;
const url = try std.fmt.allocPrintZ(allocator, "https://{s}/publish", .{repository});
defer allocator.free(url);

const payload = try file.reader().readAllAlloc(allocator, std.math.maxInt(usize));
defer allocator.free(payload);

Expand Down Expand Up @@ -404,8 +408,12 @@ pub fn postPublish(
200 => {},
401 => return error.Unauthorized,
else => |code| {
std.log.err("http status code: {}", .{code});
return error.HttpError;
if (fifo.readableSlice(0).len > 0) {
return error.Explained;
} else {
std.log.err("http status code: {}", .{code});
return error.HttpError;
}
},
}
}
11 changes: 6 additions & 5 deletions src/commands.zig
Original file line number Diff line number Diff line change
Expand Up @@ -658,14 +658,15 @@ pub fn add(
build_deps: bool,
ref: ?[]const u8,
root_path: ?[]const u8,
repository_opt: ?[]const u8,
target: []const u8,
) !void {
switch (src_tag) {
.pkg, .github, .local, .git => {},
else => return error.Todo,
}

const repository = utils.default_repo;
const repository = repository_opt orelse utils.default_repo;
var arena = ThreadSafeArenaAllocator.init(allocator);
defer arena.deinit();

Expand Down Expand Up @@ -714,7 +715,7 @@ pub fn add(
.min = latest,
.kind = .caret,
},
.repository = utils.default_repo,
.repository = repository,
},
},
};
Expand Down Expand Up @@ -814,7 +815,7 @@ pub fn rm(
try project.toFile(file);
}

pub fn publish(allocator: Allocator, pkg: ?[]const u8) anyerror!void {
pub fn publish(allocator: Allocator, repository: ?[]const u8, pkg: ?[]const u8) anyerror!void {
const client_id = "ea14bba19a49f4cba053";
const scope = "read:user user:email";

Expand Down Expand Up @@ -933,7 +934,7 @@ pub fn publish(allocator: Allocator, pkg: ?[]const u8) anyerror!void {
return error.Explained;
}

api.postPublish(allocator, access_token.?, project.get(name).?) catch |err| switch (err) {
api.postPublish(allocator, repository, access_token.?, project.get(name).?) catch |err| switch (err) {
error.Unauthorized => {
if (from_env) {
std.log.err("the access token from the env var 'GYRO_ACCESS_TOKEN' is using an outdated format for github. You need to get a new one.", .{});
Expand All @@ -946,7 +947,7 @@ pub fn publish(allocator: Allocator, pkg: ?[]const u8) anyerror!void {
}

std.log.info("getting you a new token...", .{});
try publish(allocator, pkg);
try publish(allocator, repository, pkg);
},
else => return err,
};
Expand Down
13 changes: 10 additions & 3 deletions src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const Dependency = @import("Dependency.zig");
const cmds = @import("commands.zig");
const loadSystemCerts = @import("certs.zig").loadSystemCerts;
const Display = @import("Display.zig");
const utils = @import("utils.zig");

const c = @cImport({
@cInclude("git2.h");
Expand Down Expand Up @@ -190,7 +191,8 @@ pub const commands = struct {
cmd.addFlag('b', "build-dep", "Add this as a build dependency");
cmd.addOption('r', "root", "file", completion.Param.File, "Set root path with respect to the project root, default is 'src/main.zig'");
cmd.addOption('c', "ref", "file", completion.Param.File, "commit, tag, or branch to reference for git or github source types");
cmd.addPositional("package", completion.Param.Package, .many, "The package(s) to add");
cmd.addOption('p', "repository", "repo", ?completion.Param.Repository, "The package repository you want to add a package from, default is " ++ utils.default_repo);
cmd.addPositional("package", completion.Param.Package, .one, "The package to add");

cmd.done();
break :blk cmd;
Expand All @@ -214,7 +216,7 @@ pub const commands = struct {
args.flag("--build-dep"),
args.option("--ref"),
args.option("--root"),
// TODO fix
args.option("--repository"),
args.positionals()[0],
);
}
Expand Down Expand Up @@ -296,14 +298,19 @@ pub const commands = struct {

cmd.addFlag('h', "help", "Display help");
cmd.addPositional("package", ?completion.Param.Package, .one, "The package to publish");
cmd.addOption('r', "repository", "repo", ?completion.Param.Repository, "The package repository you want to publish to, default is " ++ utils.default_repo);

cmd.done();
break :blk cmd;
};

pub const Args = info.ClapComptime();
pub fn run(allocator: std.mem.Allocator, args: *Args, _: *clap.args.OsIterator) !void {
try cmds.publish(allocator, if (args.positionals().len > 0) args.positionals()[0] else null);
try cmds.publish(
allocator,
args.option("--repository"),
if (args.positionals().len > 0) args.positionals()[0] else null,
);
}
};

Expand Down

0 comments on commit 4b4e510

Please sign in to comment.