Skip to content

Commit

Permalink
Rename WasmerDir to WasmerEnv
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael-F-Bryan committed Jun 21, 2023
1 parent 8f957eb commit da504ab
Show file tree
Hide file tree
Showing 11 changed files with 98 additions and 111 deletions.
6 changes: 3 additions & 3 deletions lib/cli/src/commands/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ use anyhow::{Context, Error};
use clap::Parser;
use wasmer_registry::{Bindings, ProgrammingLanguage};

use crate::WasmerDir;
use crate::WasmerEnv;

/// Add a Wasmer package's bindings to your application.
#[derive(Debug, Parser)]
pub struct Add {
#[clap(flatten)]
wasmer_dir: WasmerDir,
env: WasmerEnv,
/// Add the JavaScript bindings using "npm install".
#[clap(long, groups = &["bindings", "js"])]
npm: bool,
Expand All @@ -33,7 +33,7 @@ impl Add {
anyhow::ensure!(!self.packages.is_empty(), "No packages specified");

let registry = self
.wasmer_dir
.env
.registry_endpoint()
.context("Unable to determine which registry to use")?;

Expand Down
16 changes: 8 additions & 8 deletions lib/cli/src/commands/config.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{WasmerDir, VERSION};
use crate::{WasmerEnv, VERSION};
use anyhow::{Context, Result};
use clap::Parser;
use std::str::ParseBoolError;
Expand All @@ -8,7 +8,7 @@ use wasmer_registry::WasmerConfig;
/// The options for the `wasmer config` subcommand: `wasmer config get --OPTION` or `wasmer config set [FLAG]`
pub struct Config {
#[clap(flatten)]
wasmer_dir: WasmerDir,
env: WasmerEnv,

#[clap(flatten)]
flags: Flags,
Expand Down Expand Up @@ -169,12 +169,12 @@ impl Config {

fn inner_execute(&self) -> Result<()> {
if let Some(s) = self.set.as_ref() {
return s.execute(&self.wasmer_dir);
return s.execute(&self.env);
}

let flags = &self.flags;

let prefix = self.wasmer_dir.dir();
let prefix = self.env.dir();

let prefixdir = prefix.display().to_string();
let bindir = prefix.join("bin").display().to_string();
Expand Down Expand Up @@ -217,7 +217,7 @@ impl Config {
}

if flags.config_path {
let path = WasmerConfig::get_file_location(self.wasmer_dir.dir());
let path = WasmerConfig::get_file_location(self.env.dir());
println!("{}", path.display());
}

Expand All @@ -226,9 +226,9 @@ impl Config {
}

impl GetOrSet {
fn execute(&self, wasmer_dir: &WasmerDir) -> Result<()> {
let config_file = WasmerConfig::get_file_location(wasmer_dir.dir());
let mut config = wasmer_dir.config()?;
fn execute(&self, env: &WasmerEnv) -> Result<()> {
let config_file = WasmerConfig::get_file_location(env.dir());
let mut config = env.config()?;

match self {
GetOrSet::Get(g) => match g {
Expand Down
6 changes: 3 additions & 3 deletions lib/cli/src/commands/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::collections::HashMap;
use std::path::Path;
use std::path::PathBuf;

use crate::WasmerDir;
use crate::WasmerEnv;

static NOTE: &str = "# See more keys and definitions at https://docs.wasmer.io/registry/manifest";

Expand All @@ -16,7 +16,7 @@ const NEWLINE: &str = if cfg!(windows) { "\r\n" } else { "\n" };
#[derive(Debug, Parser)]
pub struct Init {
#[clap(flatten)]
wasmer_dir: WasmerDir,
env: WasmerEnv,

/// Initialize wasmer.toml for a library package
#[clap(long, group = "crate-type")]
Expand Down Expand Up @@ -140,7 +140,7 @@ impl Init {
self.template.as_ref(),
self.include.as_slice(),
self.quiet,
self.wasmer_dir.dir(),
self.env.dir(),
)?;

if let Some(parent) = target_file.parent() {
Expand Down
36 changes: 18 additions & 18 deletions lib/cli/src/commands/login.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use clap::Parser;
#[cfg(not(test))]
use dialoguer::Input;

use crate::{Registry, WasmerDir};
use crate::{Registry, WasmerEnv};

/// Subcommand for listing packages
#[derive(Debug, Clone, Parser)]
Expand All @@ -22,12 +22,12 @@ pub struct Login {
}

impl Login {
fn get_token_or_ask_user(&self, wasmer_dir: &WasmerDir) -> Result<String, anyhow::Error> {
fn get_token_or_ask_user(&self, env: &WasmerEnv) -> Result<String, anyhow::Error> {
if let Some(token) = &self.token {
return Ok(token.clone());
}

let registry_host = wasmer_dir.registry_endpoint()?;
let registry_host = env.registry_endpoint()?;
let registry_tld = tldextract::TldExtractor::new(tldextract::TldOption::default())
.extract(registry_host.as_str())
.map_err(|e| {
Expand Down Expand Up @@ -56,8 +56,8 @@ impl Login {
}
}

fn wasmer_dir(&self) -> WasmerDir {
WasmerDir::new(
fn wasmer_env(&self) -> WasmerEnv {
WasmerEnv::new(
self.wasmer_dir.clone(),
self.registry.clone(),
self.token.clone(),
Expand All @@ -66,15 +66,11 @@ impl Login {

/// execute [List]
pub fn execute(&self) -> Result<(), anyhow::Error> {
let wasmer_dir = self.wasmer_dir();
let token = self.get_token_or_ask_user(&wasmer_dir)?;

let registry = wasmer_dir.registry_endpoint()?;
match wasmer_registry::login::login_and_save_token(
wasmer_dir.dir(),
registry.as_str(),
&token,
)? {
let env = self.wasmer_env();
let token = self.get_token_or_ask_user(&env)?;

let registry = env.registry_endpoint()?;
match wasmer_registry::login::login_and_save_token(env.dir(), registry.as_str(), &token)? {
Some(s) => println!("Login for Wasmer user {:?} saved", s),
None => println!(
"Error: no user found on registry {:?} with token {:?}. Token saved regardless.",
Expand All @@ -99,10 +95,12 @@ mod tests {
wasmer_dir: temp.path().to_path_buf(),
token: None,
};
let wasmer_dir = login.wasmer_dir();
let env = login.wasmer_env();

let token = login.get_token_or_ask_user(&env).unwrap();

assert_eq!(
login.get_token_or_ask_user(&wasmer_dir).unwrap(),
token,
"Please paste the login token from https://wasmer.wtf/settings/access-tokens"
);
}
Expand All @@ -115,8 +113,10 @@ mod tests {
wasmer_dir: temp.path().to_path_buf(),
token: Some("abc".to_string()),
};
let wasmer_dir = login.wasmer_dir();
let env = login.wasmer_env();

let token = login.get_token_or_ask_user(&env).unwrap();

assert_eq!(login.get_token_or_ask_user(&wasmer_dir).unwrap(), "abc");
assert_eq!(token, "abc");
}
}
14 changes: 5 additions & 9 deletions lib/cli/src/commands/publish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ use std::path::Path;
use clap::Parser;
use wasmer_wasix::runtime::resolver::WapmSource;

use crate::WasmerDir;
use crate::WasmerEnv;

/// Publish a package to the package registry.
#[derive(Debug, Parser)]
pub struct Publish {
#[clap(flatten)]
wasmer_dir: WasmerDir,
env: WasmerEnv,
/// Run the publish logic without sending anything to the registry server
#[clap(long, name = "dry-run")]
pub dry_run: bool,
Expand All @@ -36,22 +36,18 @@ impl Publish {
/// Executes `wasmer publish`
pub fn execute(&self) -> Result<(), anyhow::Error> {
let publish = wasmer_registry::package::builder::Publish {
registry: self
.wasmer_dir
.registry_endpoint()
.map(|u| u.to_string())
.ok(),
registry: self.env.registry_endpoint().map(|u| u.to_string()).ok(),
dry_run: self.dry_run,
quiet: self.quiet,
package_name: self.package_name.clone(),
version: self.version.clone(),
token: self.wasmer_dir.token(),
token: self.env.token(),
no_validate: self.no_validate,
package_path: self.package_path.clone(),
};
publish.execute().map_err(on_error)?;

if let Err(e) = invalidate_graphql_query_cache(self.wasmer_dir.dir()) {
if let Err(e) = invalidate_graphql_query_cache(self.env.dir()) {
tracing::warn!(
error = &*e,
"Unable to invalidate the cache used for package version queries",
Expand Down
12 changes: 6 additions & 6 deletions lib/cli/src/commands/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ use wasmer_wasix::{
use webc::{metadata::Manifest, Container};

use crate::{
commands::run::wasi::Wasi, error::PrettyError, logging::Output, store::StoreOptions, WasmerDir,
commands::run::wasi::Wasi, error::PrettyError, logging::Output, store::StoreOptions, WasmerEnv,
};

const TICK: Duration = Duration::from_millis(250);
Expand All @@ -59,7 +59,7 @@ const TICK: Duration = Duration::from_millis(250);
#[derive(Debug, Parser)]
pub struct Run {
#[clap(flatten)]
wasmer_dir: WasmerDir,
env: WasmerEnv,
#[clap(flatten)]
store: StoreOptions,
#[clap(flatten)]
Expand Down Expand Up @@ -106,9 +106,9 @@ impl Run {
}

let (store, _) = self.store.get_store()?;
let runtime =
self.wasi
.prepare_runtime(store.engine().clone(), &self.wasmer_dir, handle)?;
let runtime = self
.wasi
.prepare_runtime(store.engine().clone(), &self.env, handle)?;

// This is a slow operation, so let's temporarily wrap the runtime with
// something that displays progress
Expand Down Expand Up @@ -352,7 +352,7 @@ impl Run {
};
let store = StoreOptions::default();
Ok(Run {
wasmer_dir: WasmerDir::default(),
env: WasmerEnv::default(),
store,
wasi: Wasi::for_binfmt_interpreter()?,
wcgi: WcgiOptions::default(),
Expand Down
24 changes: 12 additions & 12 deletions lib/cli/src/commands/run/wasi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ use wasmer_wasix::{

use crate::{
utils::{parse_envvar, parse_mapdir},
WasmerDir,
WasmerEnv,
};

const WAPM_SOURCE_CACHE_TIMEOUT: Duration = Duration::from_secs(10 * 60);
Expand Down Expand Up @@ -248,7 +248,7 @@ impl Wasi {
pub fn prepare_runtime(
&self,
engine: Engine,
wasmer_dir: &WasmerDir,
env: &WasmerEnv,
handle: Handle,
) -> Result<impl Runtime + Send + Sync> {
let mut rt = PluggableRuntime::new(Arc::new(TokioTaskManager::new(handle)));
Expand All @@ -270,12 +270,12 @@ impl Wasi {
let client = Arc::new(client);

let package_loader = self
.prepare_package_loader(wasmer_dir.dir(), client.clone())
.prepare_package_loader(env.dir(), client.clone())
.context("Unable to prepare the package loader")?;

let registry = self.prepare_source(wasmer_dir, client)?;
let registry = self.prepare_source(env, client)?;

let cache_dir = FileSystemCache::default_cache_dir(wasmer_dir.dir());
let cache_dir = FileSystemCache::default_cache_dir(env.dir());
let module_cache = wasmer_wasix::runtime::module_cache::in_memory()
.with_fallback(FileSystemCache::new(cache_dir));

Expand Down Expand Up @@ -327,7 +327,7 @@ impl Wasi {

fn prepare_source(
&self,
wasmer_dir: &WasmerDir,
env: &WasmerEnv,
client: Arc<dyn HttpClient + Send + Sync>,
) -> Result<impl Source + Send + Sync> {
let mut source = MultiSource::new();
Expand All @@ -342,26 +342,26 @@ impl Wasi {
}
source.add_source(preloaded);

let graphql_endpoint = self.graphql_endpoint(wasmer_dir)?;
let cache_dir = WapmSource::default_cache_dir(wasmer_dir.dir());
let graphql_endpoint = self.graphql_endpoint(env)?;
let cache_dir = WapmSource::default_cache_dir(env.dir());
let wapm_source = WapmSource::new(graphql_endpoint, Arc::clone(&client))
.with_local_cache(cache_dir, WAPM_SOURCE_CACHE_TIMEOUT);
source.add_source(wapm_source);

let cache_dir = WebSource::default_cache_dir(wasmer_dir.dir());
let cache_dir = WebSource::default_cache_dir(env.dir());
source.add_source(WebSource::new(cache_dir, client));

source.add_source(FileSystemSource::default());

Ok(source)
}

fn graphql_endpoint(&self, wasmer_dir: &WasmerDir) -> Result<Url> {
if let Ok(endpoint) = wasmer_dir.registry_endpoint() {
fn graphql_endpoint(&self, env: &WasmerEnv) -> Result<Url> {
if let Ok(endpoint) = env.registry_endpoint() {
return Ok(endpoint);
}

let config = wasmer_dir.config()?;
let config = env.config()?;
let graphql_endpoint = config.registry.get_graphql_url();
let graphql_endpoint = graphql_endpoint
.parse()
Expand Down
8 changes: 4 additions & 4 deletions lib/cli/src/commands/whoami.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
use clap::Parser;

use crate::WasmerDir;
use crate::WasmerEnv;

#[derive(Debug, Parser)]
/// The options for the `wasmer whoami` subcommand
pub struct Whoami {
#[clap(flatten)]
wasmer_dir: WasmerDir,
env: WasmerEnv,
}

impl Whoami {
/// Execute `wasmer whoami`
pub fn execute(&self) -> Result<(), anyhow::Error> {
let registry = self.wasmer_dir.registry_endpoint()?;
let registry = self.env.registry_endpoint()?;
let (registry, username) =
wasmer_registry::whoami(self.wasmer_dir.dir(), Some(registry.as_str()), None)?;
wasmer_registry::whoami(self.env.dir(), Some(registry.as_str()), None)?;
println!("logged into registry {registry:?} as user {username:?}");
Ok(())
}
Expand Down
4 changes: 2 additions & 2 deletions lib/cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ pub mod package_source;
pub mod store;
pub mod suggestions;
pub mod utils;
mod wasmer_dir;
mod wasmer_env;

/// Version number of this crate.
pub const VERSION: &str = env!("CARGO_PKG_VERSION");

pub use crate::wasmer_dir::{Registry, WasmerDir, WASMER_DIR};
pub use crate::wasmer_env::{Registry, WasmerEnv, WASMER_DIR};
Loading

0 comments on commit da504ab

Please sign in to comment.