Skip to content

Commit 2822496

Browse files
jdelliotfacebook-github-bot
authored andcommitted
Refactor glob command and common code to prepare for prefetch
Summary: # This Diff Refactor glob command and common code to prepare for prefetch. # 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. Reviewed By: genevievehelsel Differential Revision: D75331379 fbshipit-source-id: 81973aa8ecbe662289225f6611b0b2ef8ce8392e
1 parent 4c88227 commit 2822496

File tree

4 files changed

+97
-75
lines changed

4 files changed

+97
-75
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This software may be used and distributed according to the terms of the
5+
* GNU General Public License version 2.
6+
*/
7+
8+
mod common;
9+
pub mod glob;
10+
11+
pub use glob::*;
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This software may be used and distributed according to the terms of the
5+
* GNU General Public License version 2.
6+
*/
7+
8+
use std::fs::File;
9+
use std::io::BufRead;
10+
use std::io::BufReader;
11+
use std::path::PathBuf;
12+
13+
use anyhow::Result;
14+
use clap::Parser;
15+
use hg_util::path::expand_path;
16+
17+
#[derive(Parser, Debug)]
18+
pub(crate) struct CommonArgs {
19+
#[clap(
20+
long,
21+
alias = "repo",
22+
help = "Specify path to mount point (default: root of cwd)",
23+
parse(from_str = expand_path)
24+
)]
25+
pub(crate) mount_point: Option<PathBuf>,
26+
27+
#[clap(
28+
long,
29+
help = "Obtain patterns to match from FILE, one per line. If FILE is - , read patterns from standard input."
30+
)]
31+
pub(crate) pattern_file: Option<PathBuf>,
32+
33+
// Technically, we use fnmatch, but it uses glob for pattern strings.
34+
// source: https://man7.org/linux/man-pages/man3/fnmatch.3.html
35+
#[clap(
36+
help = "Filename patterns (relative to repo root) to match via glob, see: https://man7.org/linux/man-pages/man7/glob.7.html"
37+
)]
38+
pub(crate) pattern: Vec<String>,
39+
40+
#[clap(
41+
long,
42+
help = "When printing the list of matching files, exclude directories."
43+
)]
44+
pub(crate) list_only_files: bool,
45+
46+
#[clap(
47+
long,
48+
help = "When printing the list of matching files, exclude directories."
49+
)]
50+
pub(crate) include_dot_files: bool,
51+
}
52+
53+
impl CommonArgs {
54+
pub(crate) fn load_patterns(&self) -> Result<Vec<String>> {
55+
let mut pattern = self
56+
.pattern
57+
.iter()
58+
.map(|p| clean_pattern(p.to_string()))
59+
.collect::<Vec<String>>();
60+
match &self.pattern_file {
61+
Some(pattern_file) => {
62+
let file = File::open(pattern_file)?;
63+
let reader = BufReader::new(file);
64+
for line in reader.lines() {
65+
pattern.push(clean_pattern(line?));
66+
}
67+
68+
Ok(pattern)
69+
}
70+
None => Ok(pattern),
71+
}
72+
}
73+
}
74+
75+
#[cfg(target_os = "windows")]
76+
fn clean_pattern(pattern: String) -> String {
77+
pattern.replace("\\", "/")
78+
}
79+
80+
#[cfg(not(target_os = "windows"))]
81+
fn clean_pattern(pattern: String) -> String {
82+
pattern
83+
}

eden/fs/cli_rs/edenfs-commands/src/glob.rs renamed to eden/fs/cli_rs/edenfs-commands/src/glob_and_prefetch/glob.rs

Lines changed: 1 addition & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -7,83 +7,21 @@
77

88
//! edenfsctl glob
99
10-
use std::fs::File;
11-
use std::io::BufRead;
12-
use std::io::BufReader;
1310
use std::path::Path;
1411
use std::path::PathBuf;
1512

1613
use anyhow::Context;
1714
use anyhow::Result;
1815
use async_trait::async_trait;
1916
use clap::Parser;
20-
use edenfs_client::client;
2117
use edenfs_client::glob_files::Glob;
2218
use edenfs_client::glob_files::dtype_to_str;
2319
use edenfs_client::utils::locate_repo_root;
2420
use edenfs_utils::path_from_bytes;
25-
use hg_util::path::expand_path;
2621

2722
use crate::ExitCode;
2823
use crate::get_edenfs_instance;
29-
30-
#[derive(Parser, Debug)]
31-
pub struct CommonArgs {
32-
#[clap(
33-
long,
34-
alias = "repo",
35-
help = "Specify path to mount point (default: root of cwd)",
36-
parse(from_str = expand_path)
37-
)]
38-
mount_point: Option<PathBuf>,
39-
40-
#[clap(
41-
long,
42-
help = "Obtain patterns to match from FILE, one per line. If FILE is - , read patterns from standard input."
43-
)]
44-
pattern_file: Option<PathBuf>,
45-
46-
// Technically, we use fnmatch, but it uses glob for pattern strings.
47-
// source: https://man7.org/linux/man-pages/man3/fnmatch.3.html
48-
#[clap(
49-
help = "Filename patterns (relative to repo root) to match via glob, see: https://man7.org/linux/man-pages/man7/glob.7.html"
50-
)]
51-
pattern: Vec<String>,
52-
53-
#[clap(
54-
long,
55-
help = "When printing the list of matching files, exclude directories."
56-
)]
57-
list_only_files: bool,
58-
59-
#[clap(
60-
long,
61-
help = "When printing the list of matching files, exclude directories."
62-
)]
63-
include_dot_files: bool,
64-
}
65-
66-
impl CommonArgs {
67-
fn load_patterns(&self) -> Result<Vec<String>> {
68-
let mut pattern = self
69-
.pattern
70-
.iter()
71-
.map(|p| clean_pattern(p.to_string()))
72-
.collect::<Vec<String>>();
73-
match &self.pattern_file {
74-
Some(pattern_file) => {
75-
let file = File::open(pattern_file)?;
76-
let reader = BufReader::new(file);
77-
for line in reader.lines() {
78-
pattern.push(clean_pattern(line?));
79-
}
80-
81-
Ok(pattern)
82-
}
83-
None => Ok(pattern),
84-
}
85-
}
86-
}
24+
use crate::glob_and_prefetch::common::CommonArgs;
8725

8826
#[derive(Parser, Debug)]
8927
#[clap(
@@ -196,13 +134,3 @@ impl crate::Subcommand for GlobCmd {
196134
Ok(0)
197135
}
198136
}
199-
200-
#[cfg(target_os = "windows")]
201-
fn clean_pattern(pattern: String) -> String {
202-
pattern.replace("\\", "/")
203-
}
204-
205-
#[cfg(not(target_os = "windows"))]
206-
fn clean_pattern(pattern: String) -> String {
207-
pattern
208-
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ mod du;
3131
#[cfg(target_os = "macos")]
3232
mod file_access_monitor;
3333
mod gc;
34-
mod glob;
34+
mod glob_and_prefetch;
3535
mod handles;
3636
mod list;
3737
mod minitop;
@@ -153,7 +153,7 @@ pub enum TopLevelSubcommand {
153153
Uptime(crate::uptime::UptimeCmd),
154154
#[cfg(target_os = "macos")]
155155
FileAccessMonitor(crate::file_access_monitor::FileAccessMonitorCmd),
156-
Glob(crate::glob::GlobCmd),
156+
Glob(crate::glob_and_prefetch::GlobCmd),
157157
}
158158

159159
impl TopLevelSubcommand {

0 commit comments

Comments
 (0)