Run All Scripts In A Directory Recursively In Rust
I'm expermenting with an idea of including executable scripts in the main content directory for my Neopolitan implementation. The idea is to use them for preflight by running them before each build. That allows functionality to be added without having to mess with the main binary.
This is the code I'm using to loop down the direcotry structure and run the files:
Code
use std::path::PathBuf;
use std::process::Command;
use walkdir::Error;
use walkdir::WalkDir;
pub fn run_preflight() -> Result<(), Error> {
for entry in WalkDir::new("./site/content").sort_by_file_name() {
let path = PathBuf::from(entry?.path());
if let Some(ext) = path.extension() {
if ext == "py" {
let path_string = path.display().to_string();
let args: Vec<&str> = vec![path_string.as_str()];
let cmd_output = Command::new("/opt/homebrew/bin/python3")
.args(args)
.output()
.unwrap();
}
}
}
Ok(())
}
Notes
This runs code. Take the nessary security precautions
This doesn't do error handing yet. If a script files the rust process just keeps going
This version is hard coded to python. The live one will be setup with a config that lets you define file extensions and the executable to run they if they're a script
It's possible to figure out if a file is executable on
Starting to think about config options or locations for files that should be executed that don't have extensions
Run through a directory. Run all the things