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

Display a msg if the given programme cannot be found during deployment. #2517

Merged
merged 11 commits into from
Jun 5, 2023
126 changes: 74 additions & 52 deletions cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3059,78 +3059,100 @@ fn deploy(
program_str: Option<String>,
program_keypair: Option<String>,
) -> Result<()> {
// Execute the code within the workspace
with_workspace(cfg_override, |cfg| {
let url = cluster_url(cfg, &cfg.test_validator);
let keypair = cfg.provider.wallet.to_string();

// Deploy the programs.
println!("Deploying workspace: {url}");
println!("Upgrade authority: {keypair}");
println!("Deploying cluster: {}", url);
println!("Upgrade authority: {}", keypair);

let mut program_found = true; // Flag to track if the specified program is found

for mut program in cfg.read_all_programs()? {
// If a program string is provided
if let Some(single_prog_str) = &program_str {
let program_name = program.path.file_name().unwrap().to_str().unwrap();

// Check if the provided program string matches the program name
if single_prog_str.as_str() != program_name {
continue;
program_found = false;
acheroncrypto marked this conversation as resolved.
Show resolved Hide resolved
} else {
program_found = true;
}
}
let binary_path = program.binary_path().display().to_string();

println!(
"Deploying program {:?}...",
program.path.file_name().unwrap().to_str().unwrap()
);
if program_found {
let binary_path = program.binary_path().display().to_string();

println!("Program path: {binary_path}...");

let (program_keypair_filepath, program_id) = match &program_keypair {
Some(path) => (
path.clone(),
solana_sdk::signature::read_keypair_file(path)
.map_err(|_| anyhow!("Unable to read keypair file"))?
.pubkey(),
),
None => (
program.keypair_file()?.path().display().to_string(),
program.pubkey()?,
),
};
println!(
"Deploying program {:?}...",
program.path.file_name().unwrap().to_str().unwrap()
);
println!("Program path: {}...", binary_path);

let (program_keypair_filepath, program_id) = match &program_keypair {
Some(path) => (
path.clone(),
solana_sdk::signature::read_keypair_file(path)
.map_err(|_| anyhow!("Unable to read keypair file"))?
.pubkey(),
),
None => (
program.keypair_file()?.path().display().to_string(),
program.pubkey()?,
),
};

// Send deploy transactions.
let exit = std::process::Command::new("solana")
.arg("program")
.arg("deploy")
.arg("--url")
.arg(&url)
.arg("--keypair")
.arg(&keypair)
.arg("--program-id")
.arg(strip_workspace_prefix(program_keypair_filepath))
.arg(strip_workspace_prefix(binary_path))
.stdout(Stdio::inherit())
.stderr(Stdio::inherit())
.output()
.expect("Must deploy");
if !exit.status.success() {
println!("There was a problem deploying: {exit:?}.");
std::process::exit(exit.status.code().unwrap_or(1));
}
// Send deploy transactions using the Solana CLI
let exit = std::process::Command::new("solana")
.arg("program")
.arg("deploy")
.arg("--url")
.arg(&url)
.arg("--keypair")
.arg(&keypair)
.arg("--program-id")
.arg(strip_workspace_prefix(program_keypair_filepath))
.arg(strip_workspace_prefix(binary_path))
.stdout(Stdio::inherit())
.stderr(Stdio::inherit())
.output()
.expect("Must deploy");

// Check if deployment was successful
if !exit.status.success() {
println!("There was a problem deploying: {exit:?}.");
std::process::exit(exit.status.code().unwrap_or(1));
}

if let Some(mut idl) = program.idl.as_mut() {
// Add program address to the IDL.
idl.metadata = Some(serde_json::to_value(IdlTestMetadata {
address: program_id.to_string(),
})?);
if let Some(mut idl) = program.idl.as_mut() {
// Add program address to the IDL.
idl.metadata = Some(serde_json::to_value(IdlTestMetadata {
address: program_id.to_string(),
})?);

// Persist it.
let idl_out = PathBuf::from("target/idl")
.join(&idl.name)
.with_extension("json");
write_idl(idl, OutFile::File(idl_out))?;
}
}

// Persist it.
let idl_out = PathBuf::from("target/idl")
.join(&idl.name)
.with_extension("json");
write_idl(idl, OutFile::File(idl_out))?;
// Break the loop if a specific programme is discovered and program_str is not None.
if program_str.is_some() && program_found {
break;
}
}

println!("Deploy success");
// If a program string is provided but not found
if program_str.is_some() && !program_found {
println!("Specified program not found");
} else {
println!("Deploy success");
}

Ok(())
})
Expand Down