From 6ccf6414f278844a43c537a19e2f8c20f4f340d0 Mon Sep 17 00:00:00 2001 From: Radu Matei Date: Wed, 6 Nov 2024 08:32:06 +0100 Subject: [PATCH] feat(cli/wkg): wrap wkg publish command This commit wraps the `wkg publish` command as `spin deps publish`, so users can have a single CLI tool to publish, add, and generate bindings for component dependencies. Signed-off-by: Radu Matei --- Cargo.lock | 33 ++++++++++++++++++++++++++ Cargo.toml | 1 + src/commands/mod.rs | 1 + src/commands/publish.rs | 51 +++++++++++++++++++++++++++++++++++++++++ src/main.rs | 6 ++++- 5 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 src/commands/publish.rs diff --git a/Cargo.lock b/Cargo.lock index b36b62f..71f8cd5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -705,6 +705,7 @@ dependencies = [ "anstyle", "clap_lex", "strsim 0.11.1", + "terminal_size", ] [[package]] @@ -4274,6 +4275,7 @@ dependencies = [ "wit-bindgen-rust", "wit-component 0.217.0", "wit-parser 0.217.0", + "wkg", ] [[package]] @@ -4654,6 +4656,16 @@ dependencies = [ "termcolor", ] +[[package]] +name = "terminal_size" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "21bebf2b7c9e0a515f6e0f8c51dc0f8e4696391e6f1ff30379559f8365fb0df7" +dependencies = [ + "rustix 0.38.37", + "windows-sys 0.48.0", +] + [[package]] name = "thiserror" version = "1.0.63" @@ -6667,6 +6679,27 @@ dependencies = [ "wast 35.0.2", ] +[[package]] +name = "wkg" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "040ff7688fa0cc366a134afa2e2eefc6728cff41d8f6909715cfc2c38af01c67" +dependencies = [ + "anyhow", + "clap", + "docker_credential", + "futures-util", + "oci-client", + "oci-wasm 0.0.5", + "tempfile", + "tokio", + "tracing", + "tracing-subscriber", + "wasm-pkg-client", + "wasm-pkg-common 0.5.1", + "wit-component 0.216.0", +] + [[package]] name = "xdg-home" version = "1.3.0" diff --git a/Cargo.toml b/Cargo.toml index fc5f9dd..efb1c8b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -27,6 +27,7 @@ wit-bindgen-rust = { git = "https://github.com/fibonacci1729/wit-bindgen", branc wit-bindgen-core = { git = "https://github.com/fibonacci1729/wit-bindgen", branch = "deps" } wasm-pkg-common = "0.5.1" wasm-pkg-client = "0.5.1" +wkg = "0.5.1" [target.'cfg(target_os = "linux")'.dependencies] # This needs to be an explicit dependency to enable diff --git a/src/commands/mod.rs b/src/commands/mod.rs index 9132188..c895111 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -1,2 +1,3 @@ pub mod add; pub mod bindings; +pub mod publish; diff --git a/src/commands/publish.rs b/src/commands/publish.rs new file mode 100644 index 0000000..8f573ae --- /dev/null +++ b/src/commands/publish.rs @@ -0,0 +1,51 @@ +use anyhow::Result; +use clap::Args; +use std::path::PathBuf; +use wasm_pkg_client::{Client, Config, PublishOpts}; +use wasm_pkg_common::{package::PackageSpec, registry::Registry}; + +#[derive(Args, Debug)] +pub struct PublishCommand { + /// The registry domain to use. Overrides configuration file(s). + #[arg(long = "registry", value_name = "REGISTRY", env = "WKG_REGISTRY")] + registry: Option, + + /// The file to publish + file: PathBuf, + + /// If not provided, the package name and version will be inferred from the Wasm file. + /// Expected format: `:@` + #[arg(long, env = "WKG_PACKAGE")] + package: Option, +} + +impl PublishCommand { + pub async fn run(self) -> Result<()> { + let client = { + let config = Config::global_defaults()?; + Client::new(config) + }; + + let package = if let Some(package) = self.package { + Some(( + package.package, + package.version.ok_or_else(|| { + anyhow::anyhow!("version is required when manually overriding the package ID") + })?, + )) + } else { + None + }; + let (package, version) = client + .publish_release_file( + &self.file, + PublishOpts { + package, + registry: self.registry, + }, + ) + .await?; + println!("Published {}@{}", package, version); + Ok(()) + } +} diff --git a/src/main.rs b/src/main.rs index f2d87e0..b0d4e31 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,7 +3,7 @@ use clap::{Parser, Subcommand}; mod commands; mod common; -use commands::{add::AddCommand, bindings::GenerateBindingsCommand}; +use commands::{add::AddCommand, bindings::GenerateBindingsCommand, publish::PublishCommand}; /// Main CLI structure for command-line argument parsing. #[derive(Parser)] @@ -20,6 +20,9 @@ enum Commands { Add(AddCommand), /// Generates dependency bindings for selected component GenerateBindings(GenerateBindingsCommand), + + /// Publish dependency to a compatible registry + Publish(PublishCommand), } #[tokio::main] @@ -29,6 +32,7 @@ async fn main() -> Result<()> { match app.command { Commands::Add(cmd) => cmd.run().await?, Commands::GenerateBindings(cmd) => cmd.run().await?, + Commands::Publish(cmd) => cmd.run().await?, } Ok(())