diff --git a/rust/rhg/src/commands.rs b/rust/rhg/src/commands.rs --- a/rust/rhg/src/commands.rs +++ b/rust/rhg/src/commands.rs @@ -1,3 +1,4 @@ +pub mod files; pub mod root; use crate::error::CommandError; diff --git a/rust/rhg/src/commands/files.rs b/rust/rhg/src/commands/files.rs new file mode 100644 --- /dev/null +++ b/rust/rhg/src/commands/files.rs @@ -0,0 +1,68 @@ +use crate::commands::Command; +use crate::error::{CommandError, CommandErrorKind}; +use crate::ui::Ui; +use hg::operations::Operation; +use hg::operations::{FindRootError, FindRootErrorKind}; +use hg::operations::{ + ListTrackedFiles, ListTrackedFilesError, ListTrackedFilesErrorKind, +}; +use hg::utils::files::get_bytes_from_path; +use hg::utils::hg_path::HgPathBuf; +use std::path::PathBuf; + +pub const HELP_TEXT: &str = " +List tracked files. + +Returns 0 on success. +"; + +pub struct FilesCommand { + ui: Ui, +} + +impl FilesCommand { + pub fn new(ui: Ui) -> Self { + FilesCommand { ui } + } +} + +impl Command for FilesCommand { + fn run(&self) -> Result<(), CommandError> { + let files = + ListTrackedFiles::new() + .run() + .map_err(|err| match err.kind { + ListTrackedFilesErrorKind::FindRootError(err) => { + match err.kind { + FindRootErrorKind::RootNotFound(path) => { + CommandErrorKind::RootNotFound(path) + } + FindRootErrorKind::GetCurrentDirError(e) => { + CommandErrorKind::CurrentDirNotFound(e) + } + } + } + ListTrackedFilesErrorKind::IoError(e) => { + CommandErrorKind::Abort(Some( + [b"abort: ", e.to_string().as_bytes(), b"\n"] + .concat() + .to_vec(), + )) + } + ListTrackedFilesErrorKind::ParseError(_) => { + CommandErrorKind::Abort(Some( + // TODO find a better error message + b"abort: parse error\n".to_vec(), + )) + } + })?; + + for file in files { + let bytes = file.as_bytes(); + + // TODO use formating macro + self.ui.write_stdout(&[bytes, b"\n"].concat())?; + } + Ok(()) + } +} diff --git a/rust/rhg/src/error.rs b/rust/rhg/src/error.rs --- a/rust/rhg/src/error.rs +++ b/rust/rhg/src/error.rs @@ -15,6 +15,8 @@ StdoutError, /// The standard error stream cannot be written to StderrError, + /// The command aborted + Abort(Option>), } impl CommandErrorKind { @@ -24,6 +26,7 @@ CommandErrorKind::CurrentDirNotFound(_) => exitcode::ABORT, CommandErrorKind::StdoutError => exitcode::ABORT, CommandErrorKind::StderrError => exitcode::ABORT, + CommandErrorKind::Abort(_) => exitcode::ABORT, } } @@ -50,6 +53,7 @@ ] .concat(), ), + CommandErrorKind::Abort(message) => message.to_owned(), _ => None, } }