feat(cli): add vbs binaries list and fetch
This commit is contained in:
1 parent
8245a209a5
commit
8fd8c34525
8 files changed
+158
-1
No files matched your search
@@ -36,6 +36,7 @@
|
||||
#include <Void/System/TempDir.hpp>
|
||||
#include <Void/Text/Semver.hpp>
|
||||
#include <Void/Text/Str.hpp>
|
||||
#include <Void/Text/Url.hpp>
|
||||
#include <Void/Time/Stopwatch.hpp>
|
||||
|
||||
#include <algorithm>
|
||||
@@ -1151,6 +1152,67 @@ int execute_publish_binary_command(const PublishBinaryCmdOptions &opts) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
struct BinariesCmdOptions {
|
||||
std::optional<std::string> registry_arg;
|
||||
std::optional<std::string> name_arg;
|
||||
std::optional<std::string> version_arg;
|
||||
std::optional<std::string> variant_arg;
|
||||
std::optional<std::string> output_arg;
|
||||
std::string token_env{"VBS_REGISTRY_TOKEN"};
|
||||
};
|
||||
|
||||
int execute_binaries_command(std::string_view sub, const BinariesCmdOptions &opts) {
|
||||
const std::string registry_url = opts.registry_arg
|
||||
? *opts.registry_arg
|
||||
: Void::Configuration::get_env_or("VBS_REGISTRY_URL", "");
|
||||
if (registry_url.empty() || !opts.name_arg || !opts.version_arg) {
|
||||
std::cerr << "vbs binaries: --registry, --name, and --version are required\n";
|
||||
return 2;
|
||||
}
|
||||
if (sub == "fetch" && (!opts.variant_arg || !opts.output_arg)) {
|
||||
std::cerr << "vbs binaries fetch: --variant and --output are required\n";
|
||||
return 2;
|
||||
}
|
||||
|
||||
Package::RegistryAuth auth{Void::Configuration::get_env_or(opts.token_env, "")};
|
||||
auto variants =
|
||||
Package::fetch_binary_variants(registry_url, *opts.name_arg, *opts.version_arg, auth);
|
||||
if (!variants) {
|
||||
std::cerr << "vbs binaries: " << variants.error().message() << '\n';
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (sub == "list") {
|
||||
for (const auto &variant : *variants) {
|
||||
std::cout << variant.variant_id << ' ' << variant.sha256 << ' ' << variant.size_bytes
|
||||
<< ' ' << ((variant.signature && variant.public_key) ? "signed" : "unsigned")
|
||||
<< '\n';
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
const auto match = std::ranges::find_if(*variants, [&](const Package::BinaryReleaseInfo &info) {
|
||||
return info.variant_id == *opts.variant_arg;
|
||||
});
|
||||
if (match == variants->end()) {
|
||||
std::cerr << "vbs binaries fetch: variant '" << *opts.variant_arg << "' not found\n";
|
||||
return 1;
|
||||
}
|
||||
const std::string url = registry_url + "/api/v1/packages/" +
|
||||
Void::Encoding::Url::encode(*opts.name_arg) + "/" +
|
||||
Void::Encoding::Url::encode(*opts.version_arg) + "/binaries/" +
|
||||
Void::Encoding::Url::encode(match->variant_id) + "/download";
|
||||
auto downloaded = Package::download_binary_artifact(url, match->sha256,
|
||||
std::filesystem::path{*opts.output_arg});
|
||||
if (!downloaded) {
|
||||
std::cerr << "vbs binaries fetch: " << downloaded.error().message() << '\n';
|
||||
return 1;
|
||||
}
|
||||
std::cout << "fetched " << match->variant_id << " sha256=" << match->sha256 << " -> "
|
||||
<< downloaded->string() << '\n';
|
||||
return 0;
|
||||
}
|
||||
|
||||
struct PackCmdOptions {
|
||||
std::string project_arg;
|
||||
std::string out_arg{"package.tar"};
|
||||
@@ -2222,6 +2284,7 @@ int main(int argc, char **argv) {
|
||||
pack_opts.project_arg = cwd;
|
||||
PublishCmdOptions publish_opts;
|
||||
PublishBinaryCmdOptions publish_binary_opts;
|
||||
BinariesCmdOptions binaries_opts;
|
||||
publish_opts.project_arg = cwd;
|
||||
BridgeCmdOptions bridge_opts;
|
||||
GcCmdOptions gc_opts;
|
||||
@@ -2385,6 +2448,25 @@ int main(int argc, char **argv) {
|
||||
publish_binary_opts.token_env, "VBS_REGISTRY_TOKEN");
|
||||
add_log_flags(publish_binary_cmd, false); // `-v` is `--version` on publish-binary.
|
||||
|
||||
auto &binaries_cmd =
|
||||
root.subcommand("binaries", "Inspect prebuilt binary variants in a package registry");
|
||||
auto configure_binaries = [&](Void::Argparse::Parser &p) {
|
||||
p.option("--registry", "-r", "Registry base URL (or VBS_REGISTRY_URL)",
|
||||
binaries_opts.registry_arg)
|
||||
.option("--name", "-n", "Package name", binaries_opts.name_arg)
|
||||
.option("--version", "-v", "Release version", binaries_opts.version_arg)
|
||||
.option_default("--token-env", "", "Environment variable holding bearer token",
|
||||
binaries_opts.token_env, "VBS_REGISTRY_TOKEN");
|
||||
add_log_flags(p, false);
|
||||
};
|
||||
configure_binaries(binaries_cmd.subcommand(
|
||||
"list", "List a release's binary variants (id, digest, size, signature state)"));
|
||||
auto &binaries_fetch_cmd =
|
||||
binaries_cmd.subcommand("fetch", "Download one binary variant artifact");
|
||||
configure_binaries(binaries_fetch_cmd);
|
||||
binaries_fetch_cmd.option("--variant", "", "Binary variant id", binaries_opts.variant_arg)
|
||||
.option("--output", "-o", "Destination artifact path", binaries_opts.output_arg);
|
||||
|
||||
auto &pack_cmd = root.subcommand("pack", "Create a deterministic USTAR source archive (.tar)");
|
||||
pack_cmd.option_default("--project", "-C", "Project directory", pack_opts.project_arg, cwd)
|
||||
.option_default("--output", "-o", "Output archive path", pack_opts.out_arg, "package.tar");
|
||||
@@ -2614,6 +2696,9 @@ int main(int argc, char **argv) {
|
||||
if (cmd == "publish-binary") {
|
||||
return execute_publish_binary_command(publish_binary_opts);
|
||||
}
|
||||
if (cmd == "binaries") {
|
||||
return execute_binaries_command(parsed.matched_command_path.at(1), binaries_opts);
|
||||
}
|
||||
if (cmd == "bridge") {
|
||||
return execute_bridge_command(bridge_opts);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user