Skip to content

Commit 2b7fc21

Browse files
committed
Updated grep subcommand
Can now search directories for patterns but cannot search recursively yet
1 parent c46ed56 commit 2b7fc21

File tree

3 files changed

+35
-14
lines changed

3 files changed

+35
-14
lines changed

Cargo.lock

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "zeus"
3-
version = "1.3.0"
3+
version = "1.3.1"
44
edition = "2021"
55
authors = ["Jesse Amarquaye <[email protected]>"]
66
license = "MIT"

src/main.rs

+33-12
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ enum Commands {
8888
},
8989
/// Display file or file system status.
9090
Stat {
91-
/// File or file system to display status.
91+
/// File or file system to display status.
9292
#[arg(required = true)]
9393
file: Vec<path::PathBuf>,
9494
},
@@ -111,11 +111,12 @@ fn main() -> Result<()> {
111111
Some(Commands::Create { file }) => {
112112
// Create files.
113113
for f in file {
114-
fs::File::create_new(f).with_context(|| format!("Failed to create file: {:?}", f))?;
114+
fs::File::create_new(f)
115+
.with_context(|| format!("Failed to create file: {:?}", f))?;
115116
}
116117
Ok(())
117118
}
118-
Some (Commands::Echo { string, n }) => {
119+
Some(Commands::Echo { string, n }) => {
119120
// Echo strings
120121
if *n {
121122
for s in string {
@@ -128,50 +129,70 @@ fn main() -> Result<()> {
128129
println!();
129130
}
130131
Ok(())
131-
}
132-
Some (Commands::Grep { patterns, file }) => {
133-
// Search for patterns
132+
} // TODO: Add color to patterns that get matched and craete a recursive function to search directories for patterns and display the name of each file that matched.
133+
Some(Commands::Grep { patterns, file }) => {
134134
for f in file {
135135
if f.is_file() {
136-
let contents = fs::read_to_string(f)?;
136+
let contents = fs::read_to_string(f).with_context(|| format!("Failed to read the contents of {:?}", f))?;
137137
for (number, line) in contents.lines().enumerate() {
138138
if line.contains(patterns) {
139139
println!("{}: {}", number + 1, line);
140140
}
141141
}
142+
} else if f.is_dir() {
143+
if let Ok(entries) = fs::read_dir(f) {
144+
for entry in entries {
145+
if let Ok(entry) = entry {
146+
if entry.path().is_file() {
147+
let contents = fs::read_to_string(entry.path()).with_context(|| format!("Failed to read the contents of {:?}", entry.path()))?;
148+
for (number, line) in contents.lines().enumerate() {
149+
if line.contains(patterns) {
150+
println!("{}: {}", number + 1, line);
151+
}
152+
}
153+
}
154+
}
155+
}
156+
}
142157
}
143158
}
144159
Ok(())
145160
}
146161
Some(Commands::Mkdir { dir }) => {
147162
// Create directories
148163
for d in dir {
149-
fs::create_dir_all(d).with_context(|| format!("Failed to create directory: {:?}", d))?;
164+
fs::create_dir_all(d)
165+
.with_context(|| format!("Failed to create directory: {:?}", d))?;
150166
}
151167
Ok(())
152168
}
153169
Some(Commands::Rm { file, recursive }) => {
154170
// Remove files.
155171
for f in file {
156172
if *recursive && fs::metadata(f)?.is_dir() {
157-
fs::remove_dir_all(f).with_context(|| format!("Failed to remove directory recursively: {:?}", f))?;
173+
fs::remove_dir_all(f).with_context(|| {
174+
format!("Failed to remove directory recursively: {:?}", f)
175+
})?;
158176
} else {
159-
fs::remove_file(f).with_context(|| format!("Failed to remove file: {:?}", f))?;
177+
fs::remove_file(f)
178+
.with_context(|| format!("Failed to remove file: {:?}", f))?;
160179
}
161180
}
162181
Ok(())
163182
}
164183
Some(Commands::Rmdir { dir }) => {
165184
// Remove directories.
166185
for d in dir {
167-
fs::remove_dir(d).with_context(|| format!("Failed to remove directory: {:?}", d))?;
186+
fs::remove_dir(d)
187+
.with_context(|| format!("Failed to remove directory: {:?}", d))?;
168188
}
169189
Ok(())
170190
}
171191
Some(Commands::Stat { file }) => {
172192
// Display the stats for a given file or directory
173193
for f in file {
174-
let stats = fs::metadata(f).with_context(|| format!("Cannot get stats for {:?}", f))?;
194+
let stats =
195+
fs::metadata(f).with_context(|| format!("Cannot get stats for {:?}", f))?;
175196
println!("{:#?}\n", stats);
176197
}
177198
Ok(())

0 commit comments

Comments
 (0)