Skip to content

Commit baf60c8

Browse files
committed
improve gpm open command
1 parent 6912a43 commit baf60c8

File tree

1 file changed

+31
-11
lines changed

1 file changed

+31
-11
lines changed

src/main.rs

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use std::fs::File;
1515
use std::io::Read;
1616
use std::io::Write;
1717
use std::path::Path;
18+
use std::path::PathBuf;
1819
use std::process;
1920

2021
#[derive(Serialize, Deserialize, Debug)]
@@ -349,23 +350,42 @@ fn main() {
349350
}
350351
Some(("open", sub_matches)) => {
351352
let url = sub_matches.value_of("REMOTE").expect("required");
353+
let mut found: Vec<PathBuf> = vec![];
352354

353355
for gpm_root in rc.root {
354-
let dest_dir = git::url_to_path(&gpm_root, url).unwrap();
355-
356-
if dest_dir.exists() && dest_dir.is_dir() {
357-
println!(
358-
"Found the repository '{}'",
359-
dest_dir.as_os_str().to_str().unwrap()
360-
);
361-
file_explorer::open(&dest_dir);
362-
return;
356+
let repo_dir = git::url_to_path(&gpm_root, url).unwrap();
357+
358+
if repo_dir.exists() && repo_dir.is_dir() {
359+
found.push(repo_dir.to_path_buf());
363360
}
364361
}
365362

366-
println!("Did not found the cloned repository '{}'", url);
363+
if found.is_empty() {
364+
println!("Did not found the cloned repository '{}'", url);
365+
process::exit(0x1);
366+
}
367367

368-
process::exit(0x1);
368+
if found.len() == 1 {
369+
println!(
370+
"Found the repository '{}'",
371+
found[0].as_os_str().to_str().unwrap()
372+
);
373+
file_explorer::open(&found[0]);
374+
process::exit(0x0);
375+
}
376+
377+
let options: Vec<&str> = found
378+
.iter()
379+
.map(|s| s.as_os_str().to_str().unwrap())
380+
.collect();
381+
382+
let ans: Result<&str, InquireError> =
383+
Select::new("Select a repository to open:", options).prompt();
384+
385+
match ans {
386+
Ok(choice) => file_explorer::open(Path::new(choice)),
387+
Err(_) => process::exit(0x0),
388+
}
369389
}
370390
Some((ext, sub_matches)) => {
371391
let args = sub_matches

0 commit comments

Comments
 (0)