Skip to content

Commit

Permalink
Allow adding files to a package without needing a file on disk
Browse files Browse the repository at this point in the history
closes #207
  • Loading branch information
dralley committed Jan 6, 2024
1 parent c258427 commit a60f577
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
34 changes: 34 additions & 0 deletions src/rpm/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<dyn std::error::Error>> {
///
/// 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<Vec<u8>>,
options: impl Into<FileOptions>,
) -> Result<Self, Error> {
self.add_data(content.into(), Timestamp::now(), options.into())?;
Ok(self)
}

fn add_data(
&mut self,
content: Vec<u8>,
Expand Down

0 comments on commit a60f577

Please sign in to comment.