view rust/rhg/src/commands/files.rs @ 45367:53af26aa5951

rhg: handle broken pipe error for stderr Differential Revision: https://phab.mercurial-scm.org/D8871
author Antoine Cezar <antoine.cezar@octobus.net>
date Fri, 24 Jul 2020 10:34:04 +0200
parents 5fe25f8ef5d9
children 1b3197047f5c
line wrap: on
line source

use crate::commands::Command;
use crate::error::{CommandError, CommandErrorKind};
use crate::ui::Ui;
use hg::operations::{ListTrackedFiles, ListTrackedFilesErrorKind};

pub const HELP_TEXT: &str = "
List tracked files.

Returns 0 on success.
";

pub struct FilesCommand<'a> {
    ui: &'a Ui,
}

impl<'a> FilesCommand<'a> {
    pub fn new(ui: &'a Ui) -> Self {
        FilesCommand { ui }
    }
}

impl<'a> Command<'a> for FilesCommand<'a> {
    fn run(&self) -> Result<(), CommandError> {
        let operation_builder = ListTrackedFiles::new()?;
        let operation = operation_builder.load().map_err(|err| {
            CommandErrorKind::Abort(Some(
                [b"abort: ", err.to_string().as_bytes(), b"\n"]
                    .concat()
                    .to_vec(),
            ))
        })?;
        let files = operation.run().map_err(|err| match err.kind {
            ListTrackedFilesErrorKind::ParseError(_) => {
                CommandErrorKind::Abort(Some(
                    // TODO find a better error message
                    b"abort: parse error\n".to_vec(),
                ))
            }
        })?;

        let mut stdout = self.ui.stdout_buffer();
        for file in files {
            stdout.write_all(file.as_bytes())?;
            stdout.write_all(b"\n")?;
        }
        stdout.flush()?;

        Ok(())
    }
}