-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
write_changelog_file.rs
36 lines (27 loc) · 1.12 KB
/
write_changelog_file.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
use std::fs;
use std::fs::File;
use std::io::{BufRead, BufReader, Write};
use anyhow::{Context, Result};
pub fn write_changelog(changelog_path: &str, version_changelog: &[u8]) -> Result<()> {
let old_changelog = File::open(changelog_path)
.context(format!("could not open {} for reading", changelog_path))?;
let old_changelog_reader = BufReader::new(old_changelog);
let changelog_path_new = &format!("../{}.new", changelog_path);
let mut new_changelog = fs::OpenOptions::new()
.write(true)
.create(true)
.truncate(true)
.open(changelog_path_new)
.context(format!("could not open {} for writing", changelog_path_new))?;
new_changelog.write_all(version_changelog)?;
for old_line in old_changelog_reader.lines().skip(2) {
writeln!(new_changelog, "{}", old_line?)?;
}
drop(new_changelog);
fs::remove_file(changelog_path).context(format!("Could not delete {}", changelog_path))?;
fs::rename(changelog_path_new, changelog_path).context(format!(
"Could not replace {} with {}",
changelog_path, changelog_path_new
))?;
Ok(())
}