From bed16c4c0ead67c7c20cdaf9e0ea3ea903947af4 Mon Sep 17 00:00:00 2001 From: Daniel Alley Date: Sat, 6 Jan 2024 12:22:08 -0500 Subject: [PATCH] Allow adding files to a package without needing a file on disk closes #207 --- CHANGELOG.md | 1 + src/rpm/builder.rs | 34 ++++++++++++++++++++++++++++++++++ tests/compat.rs | 3 +-- 3 files changed, 36 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b041e3da..981fcd55 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,6 +23,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Added support for the automatic user/group creation feature in rpm 4.19 - `PackageBuilder::group()` and `PackageBuilder::packager()` - `PackageBuilder::verify_script()` +- `PackageBuilder::with_file_contents()` - `FileOptions::is_artifact()` and `FileOptions::is_missingok()` ## 0.13.1 diff --git a/src/rpm/builder.rs b/src/rpm/builder.rs index 8998e5ed..f6f23912 100644 --- a/src/rpm/builder.rs +++ b/src/rpm/builder.rs @@ -343,6 +343,40 @@ impl PackageBuilder { Ok(self) } + /// Add a file to the package without needing an existing file. + /// + /// Helpful if files are being generated on-demand, and you don't want to write them to disk. + /// + /// ``` + /// # fn foo() -> Result<(), Box> { + /// + /// let pkg = rpm::PackageBuilder::new("foo", "1.0.0", "Apache-2.0", "x86_64", "some baz package") + /// .with_file_contents( + /// " + /// [check] + /// date = true + /// time = true + /// ", + /// rpm::FileOptions::new("/etc/awesome/config.toml").is_config(), + /// )? + /// .with_file_contents( + /// "./awesome-config.toml", + /// // you can set a custom mode, capabilities and custom user too + /// rpm::FileOptions::new("/etc/awesome/second.toml").mode(0o100744).caps("cap_sys_admin=pe")?.user("hugo"), + /// )? + /// .build()?; + /// # Ok(()) + /// # } + /// ``` + pub fn with_file_contents( + mut self, + content: impl Into>, + options: impl Into, + ) -> Result { + self.add_data(content.into(), Timestamp::now(), options.into())?; + Ok(self) + } + fn add_data( &mut self, content: Vec, diff --git a/tests/compat.rs b/tests/compat.rs index fdb6b17c..e2ed0d51 100644 --- a/tests/compat.rs +++ b/tests/compat.rs @@ -51,8 +51,7 @@ mod pgp { cargo_file.to_str().unwrap(), FileOptions::new("/etc/foobar/hugo/bazz.toml") .mode(0o100_777) - .is_config() - .is_no_replace(), + .is_config_noreplacei(), )? .with_file( cargo_file.to_str().unwrap(),