Skip to content
This repository has been archived by the owner on Sep 17, 2024. It is now read-only.

Commit

Permalink
Fix sed issue for good!!! (Not for evil)
Browse files Browse the repository at this point in the history
  • Loading branch information
Michal S committed Sep 10, 2022
1 parent 6c156d2 commit 392f682
Showing 1 changed file with 5 additions and 10 deletions.
15 changes: 5 additions & 10 deletions src/internal/files.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::internal::*;
use std::fs::{File, OpenOptions, read_to_string};
use std::fs::{self, File, OpenOptions};
use std::io::prelude::*;

pub fn create_file(path: &str) {
Expand Down Expand Up @@ -38,15 +38,10 @@ pub fn append_file(path: &str, content: &str) -> std::io::Result<()> {

pub fn sed_file(path: &str, find: &str, replace: &str) -> std::io::Result<()> {
log::info!("Sed '{}' to '{}' in file {}", find, replace, path);

let mut file = OpenOptions::new().read(true).write(true).open(path)?;

let contents = read_to_string(path)?;
let contents = contents.replace(find, replace);

file.set_len(0)?;
file.write_all(contents.as_bytes())?;

let contents = fs::read_to_string(path)?;
let new_contents = contents.replace(find, replace);
let mut file = OpenOptions::new().write(true).truncate(true).open(path)?;
file.write_all(new_contents.as_bytes())?;
Ok(())
}

Expand Down

0 comments on commit 392f682

Please sign in to comment.