Skip to content

Commit

Permalink
Added whoami command to Wasmer
Browse files Browse the repository at this point in the history
  • Loading branch information
syrusakbary committed Aug 13, 2024
1 parent e88e69c commit 4775f71
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 32 deletions.
4 changes: 2 additions & 2 deletions src/registry/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ impl Wasmer {
.expect("app id needs to be a string");

let result: Result<(), anyhow::Error> =
wasmer_api::query::delete_app(client, app_id).await.into();
wasmer_api::query::delete_app(&client, app_id).await.into();
result.map_err(|e| utils::Error::Rust(anyhow!("while deleting the app: {e:?}")))
}
}
Expand All @@ -210,7 +210,7 @@ impl Wasmer {
let config = app_config.clone().to_yaml()?;

wasmer_api::query::publish_deploy_app(
client,
&client,
PublishDeployAppVars {
config,
name: app_config.name.into(),
Expand Down
80 changes: 54 additions & 26 deletions src/registry/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@ pub mod package;

use anyhow::anyhow;
use js_sys::Reflect::{get, has};
use wasm_bindgen::{convert::TryFromJsValue, JsValue};
use wasm_bindgen::{convert::TryFromJsValue, JsValue, prelude::wasm_bindgen};
use wasmer_api::WasmerClient;

use crate::{utils::Error, Wasmer};

static WASMER_CLIENT: std::sync::OnceLock<WasmerClient> = std::sync::OnceLock::new();

#[derive(Debug, Default, Clone)]
pub struct RegistryConfig {
pub registry_url: Option<String>,
Expand Down Expand Up @@ -58,30 +56,60 @@ impl TryFromJsValue for RegistryConfig {
}

impl Wasmer {
pub fn get_client() -> Result<&'static WasmerClient, Error> {
WASMER_CLIENT.get_or_try_init(|| {
let registry_input = if let Some(registry_info) =
js_sys::Reflect::get(&js_sys::global(), &"__WASMER_REGISTRY__".into()).ok()
{
RegistryConfig::try_from_js_value(registry_info.into())
.map_err(|e| anyhow!("while reading registry configuration: {e:?}"))?
} else {
RegistryConfig::default()
};
pub fn get_client() -> Result<WasmerClient, Error> {
let registry_input = if let Some(registry_info) =
js_sys::Reflect::get(&js_sys::global(), &"__WASMER_REGISTRY__".into()).ok()
{
RegistryConfig::try_from_js_value(registry_info.into())
.map_err(|e| anyhow!("while reading registry configuration: {e:?}"))?
} else {
RegistryConfig::default()
};

let mut client = wasmer_api::WasmerClient::new(
url::Url::parse(
&registry_input
.registry_url
.unwrap_or(crate::DEFAULT_REGISTRY.into()),
)?,
crate::USER_AGENT,
)?;
if let Some(token) = registry_input.token {
client = client.with_auth_token(token);
}
let mut client = wasmer_api::WasmerClient::new(
url::Url::parse(
&registry_input
.registry_url
.unwrap_or(crate::DEFAULT_REGISTRY.into()),
)?,
crate::USER_AGENT,
)?;
if let Some(token) = registry_input.token {
client = client.with_auth_token(token);
}

Ok(client)
})
Ok(client)
}
}


#[wasm_bindgen(getter_with_clone)]
#[derive(Debug, Clone)]
pub struct User {
pub id: String,
pub username: String,
}

impl From<wasmer_api::types::User> for User {
fn from(value: wasmer_api::types::User) -> Self {
Self {
id: value.id.inner().to_string(),
username: value.username,
}
}
}

#[wasm_bindgen]
impl Wasmer {
/// Deploy an app to the registry.
#[wasm_bindgen(js_name = "whoami")]
#[allow(non_snake_case)]
pub async fn whoami() -> Result<Option<User>, Error> {
let client = Wasmer::get_client()?;

let result: Result<Option<wasmer_api::types::User>, anyhow::Error> =
wasmer_api::query::current_user(&client).await;

Ok(result.map_err(|e| crate::utils::Error::Rust(anyhow!("while retrieving the user: {e:?}")))?.map(Into::into))
}
}
8 changes: 4 additions & 4 deletions src/registry/package/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,12 @@ impl Wasmer {
let client = Wasmer::get_client()?;

let (id, hash) = if let Some(release) =
wasmer_api::query::get_package_release(client, hash).await?
wasmer_api::query::get_package_release(&client, hash).await?
{
(release.id, release.webc_v3.map(|v| v.webc_sha256))
} else {
let signed_url = wasmer_api::query::get_signed_url_for_package_upload(
client,
&client,
Some(60 * 30),
Some(format!("js-{}", random()).replace('.', "-")).as_deref(),
None,
Expand Down Expand Up @@ -168,7 +168,7 @@ impl Wasmer {

tracing::debug!("Pushing package release...");
let out = wasmer_api::query::push_package_release(
client,
&client,
name.as_deref(),
&namespace,
&signed_url,
Expand Down Expand Up @@ -240,7 +240,7 @@ impl Wasmer {
let manifest_raw = Some(toml::to_string(&manifest)?);

let r = wasmer_api::query::tag_package_release(
client,
&client,
maybe_description.as_deref(),
maybe_homepage.as_deref(),
maybe_license.as_deref(),
Expand Down

0 comments on commit 4775f71

Please sign in to comment.