Repository: starship/starship
File: src/utils.rs (Rust)

Code before:
    }
}

/// Return the string contents of a file
pub fn read_file<P: AsRef<Path> + Debug>(file_name: P) -> Result<String> {
    log::trace!("Trying to read from {:?}", file_name);

    let result = read_to_string(file_name);

    if result.is_err() {
        log::debug!("Error reading file: {:?}", result);
    } else {
        log::trace!("File read successfully");
    };

    result
}

/// Returns the string contents of a file from the current working directory
pub fn read_file_from_pwd(context: &Context, file_name: &str) -> Option<String> {
    if !context.try_begin_scan()?.set_files(&[file_name]).is_match() {
        return None;
    }

    read_file(context.current_dir.join(file_name)).ok()
}

/// Reads command output from stderr or stdout depending on to which stream program streamed it's output
pub fn get_command_string_output(command: CommandOutput) -> String {
    if command.stdout.is_empty() {
        command.stderr
    } else {
        command.stdout
    }
}

/// Attempt to resolve `binary_name` from and creates a new `Command` pointing at it
/// This allows executing cmd files on Windows and prevents running executable from cwd on Windows
/// This function also initialises std{err,out,in} to protect against processes changing the console mode
pub fn create_command<T: AsRef<OsStr>>(binary_name: T) -> Result<Command> {
    let binary_name = binary_name.as_ref();
    log::trace!("Creating Command for binary {:?}", binary_name);

    let full_path = match which::which(binary_name) {
        Ok(full_path) => {
            log::trace!("Using {:?} as {:?}", full_path, binary_name);
            full_path
        }
        Err(error) => {
            log::trace!("Unable to find {:?} in PATH, {:?}", binary_name, error);
            return Err(Error::new(ErrorKind::NotFound, error));

Review comment (bug):
Might be a bit nice to put this into `Context` and have a log message when skipping a file.

```suggestion
impl Context {
    pub fn read_file_from_pwd(&self, file_name: &str) -> Option<String> {
        if !self.try_begin_scan()?.set_files(&[file_name]).is_match() {
            log::debug!("Not attempting to read {file_name} because, it was not included in scan.");
            return None;
        }
    
        read_file(context.current_dir.join(file_name)).ok()
    }
}
```

Code after:
    }
}

/// Return the string contents of a file
pub fn read_file<P: AsRef<Path> + Debug>(file_name: P) -> Result<String> {
    log::trace!("Trying to read from {:?}", file_name);

    let result = read_to_string(file_name);

    if result.is_err() {
        log::debug!("Error reading file: {:?}", result);
    } else {
        log::trace!("File read successfully");
    };

    result
}

/// Reads command output from stderr or stdout depending on to which stream program streamed it's output
pub fn get_command_string_output(command: CommandOutput) -> String {
    if command.stdout.is_empty() {
        command.stderr
    } else {
        command.stdout
    }
}

/// Attempt to resolve `binary_name` from and creates a new `Command` pointing at it
/// This allows executing cmd files on Windows and prevents running executable from cwd on Windows
/// This function also initialises std{err,out,in} to protect against processes changing the console mode
pub fn create_command<T: AsRef<OsStr>>(binary_name: T) -> Result<Command> {
    let binary_name = binary_name.as_ref();
    log::trace!("Creating Command for binary {:?}", binary_name);

    let full_path = match which::which(binary_name) {
        Ok(full_path) => {
            log::trace!("Using {:?} as {:?}", full_path, binary_name);
            full_path
        }
        Err(error) => {
            log::trace!("Unable to find {:?} in PATH, {:?}", binary_name, error);
            return Err(Error::new(ErrorKind::NotFound, error));
        }
    };

    #[allow(clippy::disallowed_methods)]
    let mut cmd = Command::new(full_path);
    cmd.stderr(Stdio::piped())
        .stdout(Stdio::piped())
        .stdin(Stdio::null());