rust/rhg/src/commands/root.rs
author Antoine Cezar <antoine.cezar@octobus.net>
Tue, 04 Aug 2020 16:11:23 +0200
changeset 45438 ed95ccc94333
parent 45363 5dbf875b3275
child 45984 fada33872b5b
permissions -rw-r--r--
rhg: pass `ui` to `Command` `run` Allow implementation of `From<clap::ArgMatches> for Command` Differential Revision: https://phab.mercurial-scm.org/D8954

use crate::commands::Command;
use crate::error::CommandError;
use crate::ui::Ui;
use hg::operations::FindRoot;
use hg::utils::files::get_bytes_from_path;

pub const HELP_TEXT: &str = "
Print the root directory of the current repository.

Returns 0 on success.
";

pub struct RootCommand {}

impl RootCommand {
    pub fn new() -> Self {
        RootCommand {}
    }
}

impl Command for RootCommand {
    fn run(&self, ui: &Ui) -> Result<(), CommandError> {
        let path_buf = FindRoot::new().run()?;

        let bytes = get_bytes_from_path(path_buf);

        // TODO use formating macro
        ui.write_stdout(&[bytes.as_slice(), b"\n"].concat())?;

        Ok(())
    }
}