Skip to content

Commit 4c88227

Browse files
jdelliotfacebook-github-bot
authored andcommitted
Wire up glob command to client library
Summary: # This Diff Wire up glob command to client library. # Context We are doing some Oxidation (Python->Rust) migration of the EdenFS CLI. This is an ongoing BE activity that will complete when all CLI commands have been moved. Differential Revision: D75325663 fbshipit-source-id: 5c2ec47b7f4db969cbb1019ff71f4ee2d2587964
1 parent 9b18431 commit 4c88227

File tree

1 file changed

+66
-9
lines changed
  • eden/fs/cli_rs/edenfs-commands/src

1 file changed

+66
-9
lines changed

eden/fs/cli_rs/edenfs-commands/src/glob.rs

Lines changed: 66 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@ use anyhow::Context;
1717
use anyhow::Result;
1818
use async_trait::async_trait;
1919
use clap::Parser;
20+
use edenfs_client::client;
21+
use edenfs_client::glob_files::Glob;
22+
use edenfs_client::glob_files::dtype_to_str;
2023
use edenfs_client::utils::locate_repo_root;
24+
use edenfs_utils::path_from_bytes;
2125
use hg_util::path::expand_path;
2226

2327
use crate::ExitCode;
@@ -105,10 +109,52 @@ pub struct GlobCmd {
105109
revision: Option<Vec<String>>,
106110
}
107111

112+
impl GlobCmd {
113+
fn print_result(&self, result: &Glob) -> Result<()> {
114+
if self.json {
115+
println!(
116+
"{}\n",
117+
serde_json::to_string(&result)
118+
.with_context(|| "Failed to serialize result to JSON.")?
119+
);
120+
} else {
121+
if result.matching_files.len() != result.origin_hashes.len()
122+
|| (self.dtype && result.matching_files.len() != result.dtypes.len())
123+
{
124+
println!("Error globbing files: mismatched results")
125+
}
126+
127+
for i in 0..result.matching_files.len() {
128+
print!(
129+
"{:?}",
130+
path_from_bytes(result.matching_files[i].as_ref())?
131+
.to_string_lossy()
132+
.to_string()
133+
);
134+
if self.list_origin_hash {
135+
print!("@{}", hex::encode(&result.origin_hashes[i]));
136+
}
137+
if self.dtype {
138+
print!(" {}", dtype_to_str(&result.dtypes[i]));
139+
}
140+
println!();
141+
}
142+
143+
if self.verbose {
144+
println!("Num matching files: {}", result.matching_files.len());
145+
println!("Num dtypes: {}", result.dtypes.len());
146+
println!("Num origin hashes: {}", result.origin_hashes.len());
147+
}
148+
}
149+
Ok(())
150+
}
151+
}
152+
108153
#[async_trait]
109154
impl crate::Subcommand for GlobCmd {
110155
async fn run(&self) -> Result<ExitCode> {
111-
let _instance = get_edenfs_instance();
156+
let instance = get_edenfs_instance();
157+
let client = instance.get_client();
112158

113159
// Get cwd mount_point if not provided.
114160
let current_dir: PathBuf;
@@ -125,17 +171,28 @@ impl crate::Subcommand for GlobCmd {
125171
let repo_root = locate_repo_root(mount_point);
126172

127173
// Get relative root (search root)
128-
let prefix = repo_root.unwrap_or_else(|| Path::new(""));
129-
let search_root = mount_point.strip_prefix(prefix);
174+
let repo_root = repo_root.unwrap_or_else(|| Path::new(""));
175+
let search_root = mount_point.strip_prefix(repo_root)?;
130176

131177
// Load patterns
132-
let patterns = self.common.load_patterns();
178+
let patterns = self.common.load_patterns()?;
179+
180+
let glob = client
181+
.glob_files(
182+
repo_root,
183+
patterns,
184+
self.common.include_dot_files,
185+
false,
186+
false,
187+
self.dtype,
188+
search_root,
189+
false,
190+
self.common.list_only_files,
191+
)
192+
.await?;
193+
194+
self.print_result(&glob)?;
133195

134-
// TEMP: debugging code
135-
println!(
136-
"mount_point = {:?}\nrepo_root = {:?}\nprefix = {:?}\nsearch_root = {:?}\npatterns = {:?}",
137-
mount_point, repo_root, prefix, search_root, patterns
138-
);
139196
Ok(0)
140197
}
141198
}

0 commit comments

Comments
 (0)