The words Under construction in black text on a yellow background with diagonal black stipes surrounding it
I'm in the process of moving my site. It's still a work in progress. Please excuse the mess and broken links.

Update All Files Recursively In A Directory With A Give Extension With Rust

TODO: Pull subtitle into page object
Code
use std::fs;
use std::path::PathBuf;
use walkdir::WalkDir;

fn main() {
    update_files("some/path");
}

fn update_file(file_path: PathBuf) {
    let contents = fs::read_to_string(&file_path).unwrap();
    if !contents.contains("-- site:") {
        let mut new_contents = contents.trim_end().to_string();
        new_contents.push_str("\n-- site: aws\n");
        let _ = fs::write(file_path, new_contents);
    }
}

fn update_files(dir: &str) {
    for entry in WalkDir::new(dir) {
        let pb = entry.unwrap().into_path();
        if match pb.extension() {
            Some(extwrap) => match extwrap.to_str() {
                Some(ext) => match ext {
                    "org" => true,
                    _ => false,
                },
                None => false,
            },
            None => false,
        } {
            update_file(pb);
        }
    }
}

Notes

Debugging Stuff

I'm moving stuff around right now. All this below is helping me figure out where to put stuff

        -- title

Update All Files Recursively In A Directory With A Give Extension With Rust

This is a very simple process that recursively loops over a 
directory and updates any files with a given extension:


-- code
-- rust

use std::fs;
use std::path::PathBuf;
use walkdir::WalkDir;

fn main() {
    update_files("some/path");
}

fn update_file(file_path: PathBuf) {
    let contents = fs::read_to_string(&file_path).unwrap();
    if !contents.contains("-- site:") {
        let mut new_contents = contents.trim_end().to_string();
        new_contents.push_str("\n-- site: aws\n");
        let _ = fs::write(file_path, new_contents);
    }
}

fn update_files(dir: &str) {
    for entry in WalkDir::new(dir) {
        let pb = entry.unwrap().into_path();
        if match pb.extension() {
            Some(extwrap) => match extwrap.to_str() {
                Some(ext) => match ext {
                    "org" => true,
                    _ => false,
                },
                None => false,
            },
            None => false,
        } {
            update_file(pb);
        }
    }
}

-- notes

- The `update_files`` function is responsibe for finding
the filew with the extension. It passes them to `update_file``

- The `update_file`` function does the work. In this example
I'm looking for the string `-- site:`` in the file. I add it
to the end of the file (after truncating it) if it's not
already there

- This code uses the `walkdir`` create that can be installed with
`cargo add walkdir``

- One imporvement would be to move the file extension into an
argument that's passed to the function 

- Adding more error handling is also possible, but I'm fine
with this panicing if something goes wrong. It's effectively
a one-off script. 

-- ref
-- title: walkdir
-- subtitle: An efficient and cross platform implementation of recursive directory traversal
-- url: https://docs.rs/walkdir/latest/walkdir/


-- categories
-- Rust 

-- metadata
-- date: 2023-07-10 23:14:07
-- id: 2sppt8ib
-- site: aws
-- type: post
-- status: published