Procházet zdrojové kódy

cli: Print not found message if the given program cannot be found during deployment (#2517)

Deep Mehta před 2 roky
rodič
revize
b8eda6980c
1 změnil soubory, kde provedl 74 přidání a 52 odebrání
  1. 74 52
      cli/src/lib.rs

+ 74 - 52
cli/src/lib.rs

@@ -3079,78 +3079,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;
+                } 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(())
     })