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

Allow to specify version of near-sandbox #63

Merged
merged 1 commit into from
Sep 25, 2023
Merged
Changes from all 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
64 changes: 49 additions & 15 deletions crate/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,40 +121,74 @@ fn installable(bin_path: &PathBuf) -> anyhow::Result<Option<std::fs::File>> {
}

pub fn ensure_sandbox_bin() -> anyhow::Result<PathBuf> {
ensure_sandbox_bin_with_version(DEFAULT_SANDBOX_COMMIT_HASH)
}

pub fn run_with_options(options: &[&str]) -> anyhow::Result<Child> {
let bin_path = crate::ensure_sandbox_bin()?;
Command::new(&bin_path)
.args(options)
.envs(crate::log_vars())
.spawn()
.with_context(|| format!("failed to run sandbox using '{}'", bin_path.display()))
}

pub fn run(home_dir: impl AsRef<Path>, rpc_port: u16, network_port: u16) -> anyhow::Result<Child> {
run_with_version(
home_dir,
rpc_port,
network_port,
DEFAULT_SANDBOX_COMMIT_HASH,
)
}

pub fn init(home_dir: impl AsRef<Path>) -> anyhow::Result<Child> {
init_with_version(home_dir, DEFAULT_SANDBOX_COMMIT_HASH)
}

pub fn ensure_sandbox_bin_with_version(version: &str) -> anyhow::Result<PathBuf> {
let mut bin_path = bin_path()?;
if let Some(lockfile) = installable(&bin_path)? {
bin_path = install()?;
bin_path = install_with_version(version)?;
println!("Installed near-sandbox into {}", bin_path.to_str().unwrap());
std::env::set_var("NEAR_SANDBOX_BIN_PATH", bin_path.as_os_str());
lockfile.unlock()?;
}
Ok(bin_path)
}

pub fn run_with_options(options: &[&str]) -> anyhow::Result<Child> {
let bin_path = crate::ensure_sandbox_bin()?;
pub fn run_with_options_with_version(options: &[&str], version: &str) -> anyhow::Result<Child> {
let bin_path = crate::ensure_sandbox_bin_with_version(version)?;
Command::new(&bin_path)
.args(options)
.envs(crate::log_vars())
.spawn()
.with_context(|| format!("failed to run sandbox using '{}'", bin_path.display()))
}

pub fn run(home_dir: impl AsRef<Path>, rpc_port: u16, network_port: u16) -> anyhow::Result<Child> {
pub fn run_with_version(
home_dir: impl AsRef<Path>,
rpc_port: u16,
network_port: u16,
version: &str,
) -> anyhow::Result<Child> {
let home_dir = home_dir.as_ref().to_str().unwrap();
run_with_options(&[
"--home",
home_dir,
"run",
"--rpc-addr",
&local_addr(rpc_port),
"--network-addr",
&local_addr(network_port),
])
run_with_options_with_version(
&[
"--home",
home_dir,
"run",
"--rpc-addr",
&local_addr(rpc_port),
"--network-addr",
&local_addr(network_port),
],
version,
)
}

pub fn init(home_dir: impl AsRef<Path>) -> anyhow::Result<Child> {
let bin_path = ensure_sandbox_bin()?;
pub fn init_with_version(home_dir: impl AsRef<Path>, version: &str) -> anyhow::Result<Child> {
let bin_path = ensure_sandbox_bin_with_version(version)?;
let home_dir = home_dir.as_ref().to_str().unwrap();
Command::new(&bin_path)
.envs(log_vars())
Expand Down