Matching Against Specific Filenames

⚓ Rust    📅 2026-06-21    👤 surdeus    👁️ 1      

surdeus

I have a working for-loop in my program, but it iterates over the files it creates, which use the same filename extension as the input files. How can I specify the filename extension while excluding specific characters before the extension?

In the following for-loop, *.pub is targeted for input, and the outputted files are suffixed with -cert.pub, causing an error (and potential endless loop):

let mut user_dir_pubkey = [&config.key.user_dir, "/*.pub"].join("");

for user in glob(&user_dir_pubkey).expect("Failed to read glob pattern") {

                let pub_key = user.expect("Failed to read glob result");

                let output = Command::new("ssh-keygen")
                        .args([
                                "s", &config.key.ca_priv_key,
                                "i", "name",
                                "n", &config.cert.principle,
                                "v", &config.cert.validity,
                                "O", "clear",
                                "O", "permit-pty",
                                "z", &serial_read,
                                &pub_key.display().to_string(),
                        ])
                        .output()
                        .unwrap_or_else(|_| panic!("Failed to generate OpenSSH certificate for {}", pub_key.display()));

                if !output.status.success() {
                        eprintln!("Command failed: {}", String::from_utf8_lossy(&output.stderr));
                        panic!("Certificate generation failed");
                }
        }

The outputted files should not be included in the loop, by excluding *-cert.pub, while including only *.pub.

3 posts - 2 participants

Read full topic

🏷️ Rust_feed