Mercurial > public > mercurial-scm > hg-stable
comparison rust/rhg/src/commands/files.rs @ 45384:5fe25f8ef5d9
rhg: add a `Files` `Command` to prepare the `rhg files` subcommand
Differential Revision: https://phab.mercurial-scm.org/D8868
author | Antoine Cezar <antoine.cezar@octobus.net> |
---|---|
date | Wed, 29 Jul 2020 10:21:17 +0200 |
parents | |
children | 1b3197047f5c |
comparison
equal
deleted
inserted
replaced
45383:5dbf875b3275 | 45384:5fe25f8ef5d9 |
---|---|
1 use crate::commands::Command; | |
2 use crate::error::{CommandError, CommandErrorKind}; | |
3 use crate::ui::Ui; | |
4 use hg::operations::{ListTrackedFiles, ListTrackedFilesErrorKind}; | |
5 | |
6 pub const HELP_TEXT: &str = " | |
7 List tracked files. | |
8 | |
9 Returns 0 on success. | |
10 "; | |
11 | |
12 pub struct FilesCommand<'a> { | |
13 ui: &'a Ui, | |
14 } | |
15 | |
16 impl<'a> FilesCommand<'a> { | |
17 pub fn new(ui: &'a Ui) -> Self { | |
18 FilesCommand { ui } | |
19 } | |
20 } | |
21 | |
22 impl<'a> Command<'a> for FilesCommand<'a> { | |
23 fn run(&self) -> Result<(), CommandError> { | |
24 let operation_builder = ListTrackedFiles::new()?; | |
25 let operation = operation_builder.load().map_err(|err| { | |
26 CommandErrorKind::Abort(Some( | |
27 [b"abort: ", err.to_string().as_bytes(), b"\n"] | |
28 .concat() | |
29 .to_vec(), | |
30 )) | |
31 })?; | |
32 let files = operation.run().map_err(|err| match err.kind { | |
33 ListTrackedFilesErrorKind::ParseError(_) => { | |
34 CommandErrorKind::Abort(Some( | |
35 // TODO find a better error message | |
36 b"abort: parse error\n".to_vec(), | |
37 )) | |
38 } | |
39 })?; | |
40 | |
41 let mut stdout = self.ui.stdout_buffer(); | |
42 for file in files { | |
43 stdout.write_all(file.as_bytes())?; | |
44 stdout.write_all(b"\n")?; | |
45 } | |
46 stdout.flush()?; | |
47 | |
48 Ok(()) | |
49 } | |
50 } |