Skip to content

Commit 1810746

Browse files
committed
🩹 Refactor: separate files argument collection and run function arguments
This commit separates the collection of file arguments from the arguments passed to the `run` function. The `run` function now takes a reference to a vector of file names instead of a reference to a vector of all arguments. This makes the code more modular and easier to test.
1 parent d181107 commit 1810746

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

‎src/main.rs

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ async fn main() {
3030
dotenv::dotenv().ok();
3131

3232
let args = std::env::args().collect::<Vec<String>>()[1..].to_vec();
33+
let files = args
34+
.iter()
35+
.filter(|arg| !arg.starts_with("-"))
36+
.map(|arg| arg.to_owned())
37+
.collect::<Vec<String>>();
3338

3439
if args.contains(&"--help".to_owned()) || args.contains(&"-h".to_owned()) {
3540
let usage_str = format!(
@@ -235,7 +240,12 @@ async fn main() {
235240
Err(err) => return println!("{} {}", "Error:".red(), err),
236241
}
237242

238-
run(&args, "Created README.md".to_owned(), push, &git);
243+
run(
244+
&vec!["README.md".to_owned()],
245+
"Created README.md".to_owned(),
246+
push,
247+
&git,
248+
);
239249
std::process::exit(0);
240250
}
241251

@@ -259,11 +269,11 @@ async fn main() {
259269
}
260270
};
261271

262-
run(&args, result, push, &git);
272+
run(&files, result, push, &git);
263273
}
264274

265-
fn run(args: &Vec<String>, result: String, push: bool, git: &Git) {
266-
let command = build_commands(&result, push, &args);
275+
fn run(files: &Vec<String>, result: String, push: bool, git: &Git) {
276+
let command = build_commands(&result, push, &files);
267277

268278
let parsed_command = parse_commands(&command, true);
269279

@@ -278,7 +288,7 @@ fn run(args: &Vec<String>, result: String, push: bool, git: &Git) {
278288
std::io::stdin().read_line(&mut input).unwrap();
279289
println!("");
280290
if input.trim() == "y" || input.trim() == "Y" || input.trim() == "" {
281-
git.add_old(Some(&args));
291+
git.add_old(Some(&files));
282292
git.commit_old(&result);
283293

284294
if push {
@@ -299,7 +309,7 @@ fn run(args: &Vec<String>, result: String, push: bool, git: &Git) {
299309

300310
match prompt {
301311
"Run" => {
302-
git.add_old(Some(&args));
312+
git.add_old(Some(&files));
303313
git.commit_old(&result);
304314

305315
if push {
@@ -311,7 +321,7 @@ fn run(args: &Vec<String>, result: String, push: bool, git: &Git) {
311321
}
312322
"Edit" => {
313323
let result = edit(result);
314-
run(&args, result, push, git);
324+
run(&files, result, push, git);
315325
}
316326
"Abort" => {
317327
println!("{}", "Aborted".red());

0 commit comments

Comments
 (0)