Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

updated Warg and refactored #6

Merged
merged 7 commits into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
target/
Cargo.lock

*.swp
7 changes: 4 additions & 3 deletions crates/wasm-pkg-loader/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ tokio = { version = "1.35.1", features = ["rt", "macros"] }
tokio-util = { version = "0.7.10", features = ["io"] }
toml = "0.8.8"
tracing = "0.1.40"
tracing-subscriber = "0.3.18"
warg-client = "=0.4.1"
warg-protocol = "=0.4.1"
tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }
url = "2.5.0"
warg-client = "0.6.0"
warg-protocol = "0.6.0"
20 changes: 12 additions & 8 deletions crates/wasm-pkg-loader/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,27 @@ impl ClientConfig {

pub fn merge_config(&mut self, other: ClientConfig) -> &mut Self {
if let Some(default_registry) = other.default_registry {
self.default_registry(default_registry);
self.set_default_registry(default_registry);
}
for (namespace, registry) in other.namespace_registries {
self.namespace_registry(namespace, registry);
self.set_namespace_registry(namespace, registry);
}
for (registry, config) in other.registry_configs {
self.registry_configs.insert(registry, config);
}
self
}

pub fn default_registry(&mut self, registry: impl Into<String>) -> &mut Self {
pub fn set_default_registry(&mut self, registry: impl Into<String>) -> &mut Self {
self.default_registry = Some(registry.into());
self
}

pub fn namespace_registry(
pub fn default_registry(&self) -> Option<&str> {
self.default_registry.as_deref()
}

pub fn set_namespace_registry(
&mut self,
namespace: impl Into<String>,
registry: impl Into<String>,
Expand All @@ -54,7 +58,7 @@ impl ClientConfig {
self
}

pub fn local_registry_config(
pub fn set_local_registry_config(
&mut self,
registry: impl Into<String>,
root: impl Into<PathBuf>,
Expand All @@ -66,7 +70,7 @@ impl ClientConfig {
self
}

pub fn oci_registry_config(
pub fn set_oci_registry_config(
&mut self,
registry: impl Into<String>,
client_config: Option<OciClientConfig>,
Expand All @@ -88,14 +92,14 @@ impl ClientConfig {
Ok(self)
}

pub fn warg_registry_config(
pub fn set_warg_registry_config(
&mut self,
registry: impl Into<String>,
client_config: Option<warg_client::Config>,
auth_token: Option<impl Into<SecretString>>,
) -> Result<&mut Self, Error> {
let cfg = RegistryConfig::Warg(WargConfig {
client_config: client_config.unwrap_or_default(),
client_config,
auth_token: auth_token.map(Into::into),
});
self.registry_configs.insert(registry.into(), cfg);
Expand Down
4 changes: 2 additions & 2 deletions crates/wasm-pkg-loader/src/config/toml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ impl TryFrom<TomlRegistryConfig> for super::RegistryConfig {
config_file,
} => {
let client_config = match config_file {
Some(path) => warg_client::Config::from_file(path)?,
None => warg_client::Config::from_default_file()?.unwrap_or_default(),
Some(path) => Some(warg_client::Config::from_file(path)?),
None => Some(warg_client::Config::from_default_file()?.unwrap_or_default()),
};
Self::Warg(WargConfig {
auth_token,
Expand Down
27 changes: 17 additions & 10 deletions crates/wasm-pkg-loader/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use source::{
local::LocalSource,
oci::{OciConfig, OciSource},
warg::{WargConfig, WargSource},
PackageSource,
PackageSource, VersionInfo,
};

/// Re-exported to ease configuration.
Expand Down Expand Up @@ -54,7 +54,10 @@ impl Client {
}

/// Returns a list of all package [`Version`]s available for the given package.
pub async fn list_all_versions(&mut self, package: &PackageRef) -> Result<Vec<Version>, Error> {
pub async fn list_all_versions(
&mut self,
package: &PackageRef,
) -> Result<Vec<VersionInfo>, Error> {
let source = self.resolve_source(package).await?;
source.list_all_versions(package).await
}
Expand Down Expand Up @@ -103,36 +106,34 @@ impl Client {
let source: Box<dyn PackageSource> = match registry_config {
config::RegistryConfig::Local(config) => Box::new(LocalSource::new(config)),
config::RegistryConfig::Oci(config) => {
Box::new(self.build_oci_client(&registry, config).await?)
Box::new(self.build_oci_client(&registry, registry_meta, config)?)
}
config::RegistryConfig::Warg(config) => {
Box::new(self.build_warg_client(&registry, config).await?)
Box::new(self.build_warg_client(&registry, registry_meta, config)?)
}
};
self.sources.insert(registry.clone(), source);
}
Ok(self.sources.get_mut(&registry).unwrap().as_mut())
}

async fn build_oci_client(
fn build_oci_client(
&mut self,
registry: &str,
registry_meta: RegistryMeta,
config: OciConfig,
) -> Result<OciSource, Error> {
tracing::debug!("Building new OCI client for {registry:?}");
// Check registry metadata for OCI registry override
let registry_meta = RegistryMeta::fetch_or_default(registry).await;
OciSource::new(registry.to_string(), config, registry_meta)
}

async fn build_warg_client(
fn build_warg_client(
&mut self,
registry: &str,
registry_meta: RegistryMeta,
config: WargConfig,
) -> Result<WargSource, Error> {
tracing::debug!("Building new Warg client for {registry:?}");
// Check registry metadata for OCI registry override
let registry_meta = RegistryMeta::fetch_or_default(registry).await;
WargSource::new(registry.to_string(), config, registry_meta)
}
}
Expand Down Expand Up @@ -164,6 +165,12 @@ pub enum Error {
RegistryMeta(#[source] anyhow::Error),
#[error("invalid version: {0}")]
VersionError(#[from] semver::Error),
#[error("version not found: {0}")]
VersionNotFound(Version),
#[error("version yanked: {0}")]
VersionYanked(Version),
#[error("Warg error: {0}")]
WargError(#[from] warg_client::ClientError),
#[error("Warg error: {0}")]
WargAnyhowError(#[from] anyhow::Error),
}
13 changes: 10 additions & 3 deletions crates/wasm-pkg-loader/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ use wasm_pkg_loader::{Client, ClientConfig, PackageRef, Release, Version};

#[tokio::main(flavor = "current_thread")]
async fn main() -> anyhow::Result<()> {
tracing_subscriber::fmt::init();
tracing_subscriber::fmt()
.with_env_filter(tracing_subscriber::EnvFilter::from_default_env())
.init();

let mut args = std::env::args();
let arg0 = args.next().unwrap_or_else(|| "wasm-pkg-loader".into());
Expand All @@ -21,7 +23,7 @@ async fn main() -> anyhow::Result<()> {

let client = {
let mut config = ClientConfig::default();
config.namespace_registry("wasi", "bytecodealliance.org");
config.set_namespace_registry("wasi", "bytecodealliance.org");
if let Some(file_config) = ClientConfig::from_default_file()? {
config.merge_config(file_config);
}
Expand Down Expand Up @@ -65,7 +67,11 @@ async fn show_package_info(
println!("Package: {package}");
println!("Versions:");
for ver in versions {
println!(" {ver}");
println!(
" {ver}{yanked}",
ver = ver.version,
yanked = if ver.yanked { " - Yanked" } else { "" }
);
}
}
Ok(())
Expand All @@ -86,6 +92,7 @@ async fn fetch_package_content(
.with_context(|| format!("error listing {package} releases"))?;
versions
.into_iter()
.map(|v| v.version)
.max()
.with_context(|| format!("no releases found for {package}"))?
}
Expand Down
33 changes: 32 additions & 1 deletion crates/wasm-pkg-loader/src/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,47 @@ use async_trait::async_trait;
use bytes::Bytes;
use futures_util::{stream::BoxStream, StreamExt};
use semver::Version;
use std::cmp::Ordering;

use crate::{Error, PackageRef, Release};

pub mod local;
pub mod oci;
pub mod warg;

#[derive(Clone, Debug, Eq)]
pub struct VersionInfo {
pub version: Version,
pub yanked: bool,
}

impl Ord for VersionInfo {
fn cmp(&self, other: &Self) -> Ordering {
self.version.cmp(&other.version)
}
}

impl PartialOrd for VersionInfo {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.version.cmp(&other.version))
}
}

impl PartialEq for VersionInfo {
fn eq(&self, other: &Self) -> bool {
self.version == other.version
}
}

impl std::fmt::Display for VersionInfo {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{version}", version = self.version)
}
}

#[async_trait]
pub trait PackageSource: Send {
async fn list_all_versions(&mut self, package: &PackageRef) -> Result<Vec<Version>, Error>;
async fn list_all_versions(&mut self, package: &PackageRef) -> Result<Vec<VersionInfo>, Error>;

async fn get_release(
&mut self,
Expand Down
12 changes: 9 additions & 3 deletions crates/wasm-pkg-loader/src/source/local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ use futures_util::{stream::BoxStream, StreamExt, TryStreamExt};
use semver::Version;
use tokio_util::io::ReaderStream;

use crate::{source::PackageSource, ContentDigest, Error, PackageRef, Release};
use crate::{
source::{PackageSource, VersionInfo},
ContentDigest, Error, PackageRef, Release,
};

#[derive(Clone, Debug)]
pub struct LocalConfig {
Expand Down Expand Up @@ -38,7 +41,7 @@ impl LocalSource {

#[async_trait]
impl PackageSource for LocalSource {
async fn list_all_versions(&mut self, package: &PackageRef) -> Result<Vec<Version>, Error> {
async fn list_all_versions(&mut self, package: &PackageRef) -> Result<Vec<VersionInfo>, Error> {
let mut versions = vec![];
let package_dir = self.package_dir(package);
tracing::debug!("Reading versions from {package_dir:?}");
Expand All @@ -57,7 +60,10 @@ impl PackageSource for LocalSource {
tracing::warn!("invalid package file name at {path:?}");
continue;
};
versions.push(version);
versions.push(VersionInfo {
version,
yanked: false,
});
}
Ok(versions)
}
Expand Down
12 changes: 9 additions & 3 deletions crates/wasm-pkg-loader/src/source/oci.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ use secrecy::ExposeSecret;
use semver::Version;

use crate::{
config::BasicCredentials, meta::RegistryMeta, source::PackageSource, Error, PackageRef, Release,
config::BasicCredentials,
meta::RegistryMeta,
source::{PackageSource, VersionInfo},
Error, PackageRef, Release,
};

const WASM_LAYER_MEDIA_TYPES: &[&str] = &[
Expand Down Expand Up @@ -154,7 +157,7 @@ impl OciSource {

#[async_trait]
impl PackageSource for OciSource {
async fn list_all_versions(&mut self, package: &PackageRef) -> Result<Vec<Version>, Error> {
async fn list_all_versions(&mut self, package: &PackageRef) -> Result<Vec<VersionInfo>, Error> {
let reference = self.make_reference(package, None);

tracing::debug!("Listing tags for OCI reference {reference:?}");
Expand All @@ -167,7 +170,10 @@ impl PackageSource for OciSource {
.tags
.iter()
.flat_map(|tag| match Version::parse(tag) {
Ok(version) => Some(version),
Ok(version) => Some(VersionInfo {
version,
yanked: false,
}),
Err(err) => {
tracing::warn!("Ignoring invalid version tag {tag:?}: {err:?}");
None
Expand Down
Loading
Loading