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

cli: set I/O stream to Stdio:null if the given solana_version is already installed #2757

Merged
merged 7 commits into from
Jan 6, 2024
Merged
Changes from 3 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
76 changes: 59 additions & 17 deletions cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -512,31 +512,73 @@ fn override_toolchain(cfg_override: &ConfigOverride) -> Result<RestoreToolchainC

Ok(version)
}

fn is_solana_version_installed(version: &str) -> Result<bool> {
let output = std::process::Command::new("ls")
.arg("-l")
.arg("~/.local/share/solana/install/releases")
.output()?;

if output.status.success() {
let output_versions = std::str::from_utf8(&output.stdout)?;
Ok(output_versions.lines().any(|line| line.contains(version)))
acheroncrypto marked this conversation as resolved.
Show resolved Hide resolved
} else {
Err(anyhow!("Failed to list Solana installed versions"))
}
}

fn override_solana_version(version: String) -> std::io::Result<bool> {
let is_installed = match is_solana_version_installed(&version) {
Ok(is_installed) => is_installed,
Err(err) => {
eprintln!("Error checking Solana version installation: {:?}", err);
return Err(std::io::Error::new(
std::io::ErrorKind::Other,
"Error checking installation",
));
}
};

if is_installed {
// Solana version is already installed, set standard I/O streams to null
std::process::Command::new("solana-install")
.arg("init")
.arg(&version)
.stderr(Stdio::null())
.stdout(Stdio::null())
.spawn()?
.wait()
.map(|status| status.success())
} else {
// Solana version is not installed, display installation progress
std::process::Command::new("solana-install")
.arg("init")
.arg(&version)
.spawn()?
.wait()
.map(|status| status.success())
.map_err(|err| {
eprintln!("Error during Solana installation: {:?}", err);
std::io::Error::new(std::io::ErrorKind::Other, "Error during installation")
})
}
}

if let Some(solana_version) = &cfg.toolchain.solana_version {
let current_version = get_current_version("solana")?;
if solana_version != &current_version {
// We are overriding with `solana-install` command instead of using the binaries
// from `~/.local/share/solana/install/releases` because we use multiple Solana
// binaries in various commands.
fn override_solana_version(version: String) -> std::io::Result<bool> {
std::process::Command::new("solana-install")
.arg("init")
.arg(&version)
.stderr(Stdio::null())
.stdout(Stdio::null())
.spawn()?
.wait()
.map(|status| status.success())
}

match override_solana_version(solana_version.to_owned())? {
true => restore_cbs.push(Box::new(|| {
match override_solana_version(current_version)? {
true => Ok(()),
false => Err(anyhow!("Failed to restore `solana` version")),
}
})),
true => {
restore_cbs.push(Box::new(|| {
match override_solana_version(current_version)? {
true => Ok(()),
false => Err(anyhow!("Failed to restore `solana` version")),
}
}));
}
false => {
eprintln!(
"Failed to override `solana` version to {solana_version}, \
Expand Down