Formatting
This commit is contained in:
@@ -35,7 +35,10 @@ fn main() -> ExitCode {
|
||||
Ok(Mode::PatchFile { slug, file }) => match read_password_twice() {
|
||||
Ok(pw) => match patch_users_toml(&file, &slug, &hash(&pw)) {
|
||||
Ok(()) => {
|
||||
eprintln!("Updated web_password for slug \"{slug}\" in {}", file.display());
|
||||
eprintln!(
|
||||
"Updated web_password for slug \"{slug}\" in {}",
|
||||
file.display()
|
||||
);
|
||||
ExitCode::SUCCESS
|
||||
}
|
||||
Err(e) => {
|
||||
@@ -131,11 +134,8 @@ fn read_password_twice() -> Result<String, String> {
|
||||
/// Patch `web_password` for the `[[user]]` entry whose `slug` matches.
|
||||
/// Preserves comments and formatting via `toml_edit`. Writes atomically.
|
||||
fn patch_users_toml(path: &Path, slug: &str, new_hash: &str) -> Result<(), String> {
|
||||
let content = std::fs::read_to_string(path)
|
||||
.map_err(|e| format!("read: {e}"))?;
|
||||
let mut doc: toml_edit::DocumentMut = content
|
||||
.parse()
|
||||
.map_err(|e| format!("parse: {e}"))?;
|
||||
let content = std::fs::read_to_string(path).map_err(|e| format!("read: {e}"))?;
|
||||
let mut doc: toml_edit::DocumentMut = content.parse().map_err(|e| format!("parse: {e}"))?;
|
||||
|
||||
let users = doc
|
||||
.get_mut("user")
|
||||
@@ -158,8 +158,7 @@ fn patch_users_toml(path: &Path, slug: &str, new_hash: &str) -> Result<(), Strin
|
||||
// Atomic write: tmp file in same dir, then rename.
|
||||
let tmp = path.with_extension("toml.tmp");
|
||||
{
|
||||
let mut f = std::fs::File::create(&tmp)
|
||||
.map_err(|e| format!("create tmp: {e}"))?;
|
||||
let mut f = std::fs::File::create(&tmp).map_err(|e| format!("create tmp: {e}"))?;
|
||||
f.write_all(doc.to_string().as_bytes())
|
||||
.map_err(|e| format!("write tmp: {e}"))?;
|
||||
f.sync_all().map_err(|e| format!("sync tmp: {e}"))?;
|
||||
@@ -203,7 +202,10 @@ role = "doctor"
|
||||
patch_users_toml(&p, "dr_a", "$2b$12$NEW").unwrap();
|
||||
let out = std::fs::read_to_string(&p).unwrap();
|
||||
assert!(out.contains(r#"web_password = "$2b$12$NEW""#), "got: {out}");
|
||||
assert!(out.contains(r#"web_password = "$2b$12$KEEP""#), "dr_b changed: {out}");
|
||||
assert!(
|
||||
out.contains(r#"web_password = "$2b$12$KEEP""#),
|
||||
"dr_b changed: {out}"
|
||||
);
|
||||
assert!(out.contains("# header comment"), "comment lost: {out}");
|
||||
std::fs::remove_file(&p).ok();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user