summaryrefslogtreecommitdiffstats
path: root/src/shell_install/util.rs
blob: 34d11d14517e44f003e0a4528f29b4c296c8918b (plain)
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
use {
    crate::errors::*,
    std::{
        fs::{self, OpenOptions},
        io::{BufRead, BufReader, Write},
        path::Path,
    },
};
pub fn file_contains_line(path: &Path, searched_line: &str) -> Result<bool, ShellInstallError> {
    let file = fs::File::open(path)
        .context(&|| format!("opening {path:?}"))?;
    for line in BufReader::new(file).lines() {
        let line = line.context(&|| format!("reading line in {path:?}"))?;
        if line == searched_line {
            return Ok(true);
        }
    }
    Ok(false)
}

pub fn append_to_file<S: AsRef<str>>(path: &Path, content: S) -> Result<(), ShellInstallError> {
    let mut shellrc = OpenOptions::new()
        .write(true)
        .append(true)
        .open(path)
        .context(&|| format!("opening {path:?} for append"))?;
    shellrc.write_all(content.as_ref().as_bytes())
        .context(&|| format!("writing in {path:?}"))?;
    Ok(())
}