From 5f44cb2f4e622d2bfe45e1bc016df3505a64a90f Mon Sep 17 00:00:00 2001 From: Charles Date: Sat, 29 Mar 2025 22:44:06 -0700 Subject: [PATCH] add: rewriter logic and tests --- .gitignore | 1 + Cargo.lock | 559 +++++++++++++++++++++++ Cargo.toml | 13 + README.md | 19 + src/main.rs | 44 ++ src/rewriter.rs | 121 +++++ src/testdata/testdst/hello.txt | 5 + src/testdata/testdst/recursive/world.txt | 4 + src/testdata/testsrc/hello.txt | 4 + src/testdata/testsrc/recursive/world.txt | 3 + 10 files changed, 773 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 src/main.rs create mode 100644 src/rewriter.rs create mode 100644 src/testdata/testdst/hello.txt create mode 100644 src/testdata/testdst/recursive/world.txt create mode 100644 src/testdata/testsrc/hello.txt create mode 100644 src/testdata/testsrc/recursive/world.txt diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..ac7b031 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,559 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "aho-corasick" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916" +dependencies = [ + "memchr", +] + +[[package]] +name = "anstream" +version = "0.6.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8acc5369981196006228e28809f761875c0327210a891e941f4c683b3a99529b" +dependencies = [ + "anstyle", + "anstyle-parse", + "anstyle-query", + "anstyle-wincon", + "colorchoice", + "is_terminal_polyfill", + "utf8parse", +] + +[[package]] +name = "anstyle" +version = "1.0.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "55cc3b69f167a1ef2e161439aa98aed94e6028e5f9a59be9a6ffb47aef1651f9" + +[[package]] +name = "anstyle-parse" +version = "0.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3b2d16507662817a6a20a9ea92df6652ee4f94f914589377d69f3b21bc5798a9" +dependencies = [ + "utf8parse", +] + +[[package]] +name = "anstyle-query" +version = "1.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "79947af37f4177cfead1110013d678905c37501914fba0efea834c3fe9a8d60c" +dependencies = [ + "windows-sys", +] + +[[package]] +name = "anstyle-wincon" +version = "3.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ca3534e77181a9cc07539ad51f2141fe32f6c3ffd4df76db8ad92346b003ae4e" +dependencies = [ + "anstyle", + "once_cell", + "windows-sys", +] + +[[package]] +name = "anyhow" +version = "1.0.97" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dcfed56ad506cb2c684a14971b8861fdc3baaaae314b9e5f9bb532cbe3ba7a4f" + +[[package]] +name = "clap" +version = "4.5.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e958897981290da2a852763fe9cdb89cd36977a5d729023127095fa94d95e2ff" +dependencies = [ + "clap_builder", + "clap_derive", +] + +[[package]] +name = "clap_builder" +version = "4.5.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "83b0f35019843db2160b5bb19ae09b4e6411ac33fc6a712003c33e03090e2489" +dependencies = [ + "anstream", + "anstyle", + "clap_lex", + "strsim", +] + +[[package]] +name = "clap_derive" +version = "4.5.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09176aae279615badda0765c0c0b3f6ed53f4709118af73cf4655d85d1530cd7" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "clap_lex" +version = "0.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f46ad14479a25103f283c0f10005961cf086d8dc42205bb44c46ac563475dca6" + +[[package]] +name = "colorchoice" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b63caa9aa9397e2d9480a9b13673856c78d8ac123288526c37d7839f2a86990" + +[[package]] +name = "dir-diff" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a7ad16bf5f84253b50d6557681c58c3ab67c47c77d39fed9aeb56e947290bd10" +dependencies = [ + "walkdir", +] + +[[package]] +name = "env_filter" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "186e05a59d4c50738528153b83b0b0194d3a29507dfec16eccd4b342903397d0" +dependencies = [ + "log", + "regex", +] + +[[package]] +name = "env_logger" +version = "0.11.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c3716d7a920fb4fac5d84e9d4bce8ceb321e9414b4409da61b07b75c1e3d0697" +dependencies = [ + "anstream", + "anstyle", + "env_filter", + "jiff", + "log", +] + +[[package]] +name = "fuchsia-cprng" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a06f77d526c1a601b7c4cdd98f54b5eaabffc14d5f2f0296febdc7f357c6d3ba" + +[[package]] +name = "heck" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" + +[[package]] +name = "include_directory" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fc51bf21d9c8c76d0d55b3926add7fde9b595719ee0d5710d46f8ee66131cca9" +dependencies = [ + "include_directory_macros", + "mime", +] + +[[package]] +name = "include_directory_macros" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "35011b5de7391d94ea631aa584d09a88b2e877505a614e4952970214f2fd1b90" +dependencies = [ + "mime", + "new_mime_guess", + "proc-macro2", + "quote", +] + +[[package]] +name = "is_terminal_polyfill" +version = "1.70.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf" + +[[package]] +name = "jiff" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c102670231191d07d37a35af3eb77f1f0dbf7a71be51a962dcd57ea607be7260" +dependencies = [ + "jiff-static", + "log", + "portable-atomic", + "portable-atomic-util", + "serde", +] + +[[package]] +name = "jiff-static" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4cdde31a9d349f1b1f51a0b3714a5940ac022976f4b49485fc04be052b183b4c" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "libc" +version = "0.2.171" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c19937216e9d3aa9956d9bb8dfc0b0c8beb6058fc4f7a4dc4d850edf86a237d6" + +[[package]] +name = "log" +version = "0.4.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "13dc2df351e3202783a1fe0d44375f7295ffb4049267b0f3018346dc122a1d94" + +[[package]] +name = "memchr" +version = "2.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" + +[[package]] +name = "mime" +version = "0.3.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6877bb514081ee2a7ff5ef9de3281f14a4dd4bceac4c09388074a6b5df8a139a" + +[[package]] +name = "new_mime_guess" +version = "4.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "02a2dfb3559d53e90b709376af1c379462f7fb3085a0177deb73e6ea0d99eff4" +dependencies = [ + "mime", + "unicase", +] + +[[package]] +name = "once_cell" +version = "1.21.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "42f5e15c9953c5e4ccceeb2e7382a716482c34515315f7b03532b8b4e8393d2d" + +[[package]] +name = "portable-atomic" +version = "1.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "350e9b48cbc6b0e028b0473b114454c6316e57336ee184ceab6e53f72c178b3e" + +[[package]] +name = "portable-atomic-util" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d8a2f0d8d040d7848a709caf78912debcc3f33ee4b3cac47d73d1e1069e83507" +dependencies = [ + "portable-atomic", +] + +[[package]] +name = "proc-macro2" +version = "1.0.94" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a31971752e70b8b2686d7e46ec17fb38dad4051d94024c88df49b667caea9c84" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.40" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1885c039570dc00dcb4ff087a89e185fd56bae234ddc7f056a945bf36467248d" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "rand" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "552840b97013b1a26992c11eac34bdd778e464601a4c2054b5f0bff7c6761293" +dependencies = [ + "fuchsia-cprng", + "libc", + "rand_core 0.3.1", + "rdrand", + "winapi", +] + +[[package]] +name = "rand_core" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a6fdeb83b075e8266dcc8762c22776f6877a63111121f5f8c7411e5be7eed4b" +dependencies = [ + "rand_core 0.4.2", +] + +[[package]] +name = "rand_core" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c33a3c44ca05fa6f1807d8e6743f3824e8509beca625669633be0acbdf509dc" + +[[package]] +name = "rdrand" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "678054eb77286b51581ba43620cc911abf02758c91f93f479767aed0f90458b2" +dependencies = [ + "rand_core 0.3.1", +] + +[[package]] +name = "regex" +version = "1.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b544ef1b4eac5dc2db33ea63606ae9ffcfac26c1416a2806ae0bf5f56b201191" +dependencies = [ + "aho-corasick", + "memchr", + "regex-automata", + "regex-syntax", +] + +[[package]] +name = "regex-automata" +version = "0.4.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "809e8dc61f6de73b46c85f4c96486310fe304c434cfa43669d7b40f711150908" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c" + +[[package]] +name = "remove_dir_all" +version = "0.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3acd125665422973a33ac9d3dd2df85edad0f4ae9b00dafb1a05e43a9f5ef8e7" +dependencies = [ + "winapi", +] + +[[package]] +name = "same-file" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502" +dependencies = [ + "winapi-util", +] + +[[package]] +name = "serde" +version = "1.0.219" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5f0e2c6ed6606019b4e29e69dbaba95b11854410e5347d525002456dbbb786b6" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.219" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b0276cf7f2c73365f7157c8123c21cd9a50fbbd844757af28ca1f5925fc2a00" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "skubelb" +version = "0.1.0" +dependencies = [ + "anyhow", + "clap", + "dir-diff", + "env_logger", + "include_directory", + "log", + "tempdir", +] + +[[package]] +name = "strsim" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" + +[[package]] +name = "syn" +version = "2.0.100" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b09a44accad81e1ba1cd74a32461ba89dee89095ba17b32f5d03683b1b1fc2a0" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "tempdir" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "15f2b5fb00ccdf689e0149d1b1b3c03fead81c2b37735d812fa8bddbbf41b6d8" +dependencies = [ + "rand", + "remove_dir_all", +] + +[[package]] +name = "unicase" +version = "2.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "75b844d17643ee918803943289730bec8aac480150456169e647ed0b576ba539" + +[[package]] +name = "unicode-ident" +version = "1.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a5f39404a5da50712a4c1eecf25e90dd62b613502b7e925fd4e4d19b5c96512" + +[[package]] +name = "utf8parse" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" + +[[package]] +name = "walkdir" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "29790946404f91d9c5d06f9874efddea1dc06c5efe94541a7d6863108e3a5e4b" +dependencies = [ + "same-file", + "winapi-util", +] + +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-util" +version = "0.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf221c93e13a30d793f7645a0e7762c55d169dbb0a49671918a2319d289b10bb" +dependencies = [ + "windows-sys", +] + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" + +[[package]] +name = "windows-sys" +version = "0.59.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e38bc4d79ed67fd075bcc251a1c39b32a1776bbe92e5bef1f0bf1f8c531853b" +dependencies = [ + "windows-targets", +] + +[[package]] +name = "windows-targets" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" +dependencies = [ + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_gnullvm", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" + +[[package]] +name = "windows_i686_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" + +[[package]] +name = "windows_i686_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" + +[[package]] +name = "windows_i686_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..f1174a2 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "skubelb" +version = "0.1.0" +edition = "2024" + +[dependencies] +anyhow = "1.0.97" +clap = { version = "4.5.34", features = ["derive"] } +dir-diff = "0.3.3" +env_logger = "0.11.7" +include_directory = "0.1.1" +log = "0.4.27" +tempdir = "0.3.7" diff --git a/README.md b/README.md index e69de29..10c9d82 100644 --- a/README.md +++ b/README.md @@ -0,0 +1,19 @@ +# A simple Kubernetes load balancer + +Configures nginx to forward connections to your node IPs. +Services should be declared as NodePort, which means that they +open a port on all nodes. When the request lands on any node, +it is forwarded to the correct pod via the network mesh kubernetes +is using. In theory, there is one a hop penalty. + +But lets be honest. You're running with a single LB, probably a GCE free +tier N1 VM. That extra hop doesn't matter. + +## Config + +Configure nginx to do what you want, test it. Use any Node IP for your testing. +This tool accepts an argument (rewrite_string) which will be replaced using these rules: + +1. For each line that contains rewrite string: +2. Copy the line once per node IP, replacing the string with host IPs + diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..f65e955 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,44 @@ +use clap::Parser; + +mod rewriter; + +use env_logger::Env; +use log::info; +use rewriter::Rewriter; + +/// Rewrites nginx configs when new nodes show up in a kubernetes cluster. +/// This package implements the 'proxy end'; another binary is run on the +/// Kubernetes cluster which executes when a node goes down or comes up. +#[derive(Parser, Debug)] +#[command(version, about, long_about = None)] +struct Args { + /// The needle that will be replaced. Anytime a line + /// is encountered with this needle, the line is dropped + /// and instead N lines (one per replacement) is added to + /// the output. + #[arg(short, long)] + rewrite_string: String, + + /// The folder which contains the templates that + /// will be be searched for the needle. + #[arg(short, long)] + source_folder: String, + + /// Where to write the replaced lines. + #[arg(short, long)] + dest_folder: String, +} + +fn main() { + // Log info and above by default + env_logger::Builder::from_env(Env::default().default_filter_or("info")).init(); + let args = Args::parse(); + + let mut rewriter = Rewriter::new(args.rewrite_string); + + rewriter.add_replacement(String::from("abc")); + rewriter + .rewrite_folder(&args.source_folder, &args.dest_folder) + .unwrap(); + info!("Finished writing new config to {}", args.dest_folder); +} diff --git a/src/rewriter.rs b/src/rewriter.rs new file mode 100644 index 0000000..2e4ea8b --- /dev/null +++ b/src/rewriter.rs @@ -0,0 +1,121 @@ +use anyhow::Result; +use std::collections::HashSet; +use std::path::Path; +use std::{ + fs::{self, File}, + io::{BufReader, prelude::*}, +}; + +pub struct Rewriter { + source: String, + replacements: HashSet, +} + +impl Rewriter { + pub fn new(source: String) -> Self { + Self { + source, + replacements: HashSet::new(), + } + } + + pub fn add_replacement(&mut self, replacement: String) { + self.replacements.insert(replacement); + } + + pub fn remove_replacement(&mut self, replacement: &str) { + self.replacements.remove(replacement); + } + + pub fn rewrite_folder(&self, src: &str, dst: &str) -> Result<()> { + // Make sure we are deterministic; construct a list of strings and sort + // them + let mut replacements: Vec = self.replacements.iter().map(|s| s.clone()).collect(); + replacements.sort(); + let dst_base = Path::new(dst); + let mut to_visit = vec![String::from(src)]; + while to_visit.len() > 0 { + let dir = to_visit.pop().unwrap(); + for entry in fs::read_dir(&dir)? { + let entry = entry?; + let metadata = entry.metadata()?; + + // We need to find the destination this should be written into + // First, calculate the 'relative' path after we trim the + // the src prefix, then join that with the dst prefix + let src_path = entry.path(); + let src_part_path = src_path.strip_prefix(&src)?; + let dst_path = dst_base.join(src_part_path); + + if metadata.is_dir() { + // Create the directory, then carry on. Note that we explore the + // src_path after creating dst_path. + fs::create_dir(&dst_path)?; + println!("mkdir {:?}", dst_path); + to_visit.push(src_path.into_os_string().into_string().unwrap()); + continue; + } + + // Open 2 files; one to read and translate, and one to write. + let source_file = File::open(&src_path)?; + println!("touch {:?}", dst_path); + let mut dest_file = File::create(&dst_path)?; + let reader = BufReader::new(source_file); + + for line in reader.lines() { + let line = line?; + // If the line is not subject to replacement, copy it and + // carry on. + if !line.contains(&self.source) { + println!("{}", line); + writeln!(dest_file, "{}", line)?; + continue; + } + + // Else, repeat the line multiple times, replacing the string + // in question + for replacement in &replacements { + let new_line = line.replace(&self.source, &replacement); + println!("{}", new_line); + writeln!(dest_file, "{}", new_line)?; + } + } + } + } + Ok(()) + } +} + +#[cfg(test)] +mod tests { + use include_directory::{Dir, include_directory}; + use tempdir::TempDir; + + use super::Rewriter; + + static TEST_FILES: Dir<'_> = include_directory!("$CARGO_MANIFEST_DIR/src/testdata"); + #[test] + fn basic_test() { + let testdata = TempDir::new("").unwrap(); + TEST_FILES.extract(testdata.path()).unwrap(); + let src = testdata.path().join("testsrc"); + let dst = TempDir::new("").unwrap(); + + let mut rewriter = Rewriter::new("to_be_replaced".into()); + rewriter.add_replacement("abc".into()); + rewriter.add_replacement("def".into()); + rewriter.add_replacement("zyx".into()); + rewriter.remove_replacement("zyx"); + rewriter + .rewrite_folder( + src.as_os_str().to_str().unwrap(), + dst.path().as_os_str().to_str().unwrap(), + ) + .unwrap(); + + // Validate that everything matches + assert!( + dir_diff::is_different(testdata.path().join("testdst"), dst.path()).unwrap() == false + ); + } +} diff --git a/src/testdata/testdst/hello.txt b/src/testdata/testdst/hello.txt new file mode 100644 index 0000000..b795c8a --- /dev/null +++ b/src/testdata/testdst/hello.txt @@ -0,0 +1,5 @@ +This is a line +This is abc line +This is def line + +This is another line diff --git a/src/testdata/testdst/recursive/world.txt b/src/testdata/testdst/recursive/world.txt new file mode 100644 index 0000000..a52a07c --- /dev/null +++ b/src/testdata/testdst/recursive/world.txt @@ -0,0 +1,4 @@ +This is a abc line. +This is a def line. + +In a nested directory. diff --git a/src/testdata/testsrc/hello.txt b/src/testdata/testsrc/hello.txt new file mode 100644 index 0000000..444809a --- /dev/null +++ b/src/testdata/testsrc/hello.txt @@ -0,0 +1,4 @@ +This is a line +This is to_be_replaced line + +This is another line \ No newline at end of file diff --git a/src/testdata/testsrc/recursive/world.txt b/src/testdata/testsrc/recursive/world.txt new file mode 100644 index 0000000..e1baabd --- /dev/null +++ b/src/testdata/testsrc/recursive/world.txt @@ -0,0 +1,3 @@ +This is a to_be_replaced line. + +In a nested directory. \ No newline at end of file