From d8b2116aeb976caf089889b98aac529fc65f398a Mon Sep 17 00:00:00 2001 From: grimhilt Date: Fri, 1 Mar 2024 15:35:38 +0100 Subject: [PATCH] feat(remote): list remote with verbose option --- src/commands/config.rs | 64 +++++++++++++++++++++++++++++---------- src/commands/remote.rs | 16 ++++++++++ src/subcommands/remote.rs | 12 +++++++- src/utils/path.rs | 7 +++++ 4 files changed, 82 insertions(+), 17 deletions(-) diff --git a/src/commands/config.rs b/src/commands/config.rs index 54f4aea..ca79f08 100644 --- a/src/commands/config.rs +++ b/src/commands/config.rs @@ -30,14 +30,13 @@ pub fn config_set(args: ConfigSetArgs) { pub fn find_option_in_cat(category: &str, option: &str) -> Option { - let mut root = path::nextsync(); - root.push("config"); + let mut config = path::nextsync(); + config.push("config"); let mut in_target_category = false; - if let Ok(mut lines) = read::read_lines(root) { + if let Ok(mut lines) = read::read_lines(config) { for line in lines { - if let Ok(line) = line { let trimmed_line = line.trim(); @@ -58,14 +57,14 @@ pub fn find_option_in_cat(category: &str, option: &str) -> Option { } pub fn write_option_in_cat(category: &str, option: &str, value: &str) -> io::Result<()> { - let mut root = path::nextsync(); - root.push("config"); + let mut config = path::nextsync(); + config.push("config"); let mut file = OpenOptions::new() .read(true) .write(true) .create(true) - .open(&root)?; + .open(&config)?; let mut in_target_category = false; let mut option_found = false; @@ -129,8 +128,7 @@ pub fn write_option_in_cat(category: &str, option: &str, value: &str) -> io::Res } pub fn add_remote(name: &str, url: &str) -> io::Result<()> { - let mut root = path::nextsync(); - root.push("config"); + let mut config = path::config(); // check if there is already a remote with this name if get_remote(name).is_some() @@ -144,7 +142,7 @@ pub fn add_remote(name: &str, url: &str) -> io::Result<()> { .write(true) .create(true) .append(true) - .open(root)?; + .open(config)?; writeln!(file, "[remote \"{}\"]", name)?; writeln!(file, "\turl = {}", url)?; @@ -156,20 +154,55 @@ pub fn get_remote(name: &str) -> Option { find_option_in_cat(&format!("remote \"{}\"", name), "url") } +/// return a vector of remote found in config file (e.g: ("origin", "https://example.com")) +pub fn get_all_remote() -> Vec<(String, String)> { + let config = path::config(); + + let mut remotes: Vec<(String, String)> = vec![]; + + let mut in_remote = false; + let mut remote_name = String::new(); + if let Ok(lines) = read::read_lines(config) { + + for line in lines { + if let Ok(line) = line { + let trimmed_line = line.trim(); + + if trimmed_line.starts_with("[remote ") { + in_remote = true; + remote_name = trimmed_line.strip_prefix("[remote \"").unwrap().strip_suffix("\"]").unwrap().to_string(); + } + else if trimmed_line.starts_with('[') + { + in_remote = false; + } + else if in_remote { + let parts: Vec<&str> = trimmed_line.splitn(2, '=').collect(); + if parts.len() == 2 { + remotes.push((remote_name.to_string(), parts[1].trim().to_string())) + } + } + + } + } + } + remotes +} + pub fn get_core(name: &str) -> Option { find_option_in_cat("core", name) } pub fn add_core(name: &str, value: &str) -> io::Result<()> { - let mut root = path::nextsync(); - root.push("config"); + let mut config = path::nextsync(); + config.push("config"); let mut file = OpenOptions::new() .read(true) .write(true) .create(true) .append(true) - .open(root)?; + .open(config)?; writeln!(file, "[core]")?; writeln!(file, "\t{} = {}", name, value)?; @@ -178,10 +211,9 @@ pub fn add_core(name: &str, value: &str) -> io::Result<()> { } pub fn get(var: &str) -> Option { - let mut root = path::nextsync(); - root.push("config"); + let mut config = path::config(); - if let Ok(lines) = read::read_lines(root) { + if let Ok(lines) = read::read_lines(config) { for line in lines { if let Ok(l) = line { if l.starts_with(var) { diff --git a/src/commands/remote.rs b/src/commands/remote.rs index 5a1c50a..acf71f0 100644 --- a/src/commands/remote.rs +++ b/src/commands/remote.rs @@ -2,6 +2,8 @@ use clap::Values; use crate::commands::config; +use super::config::get_all_remote; + pub struct RemoteArgs<'a> { pub name: Option>, pub url: Option>, @@ -19,3 +21,17 @@ pub fn remote_add(args: RemoteArgs) { let _ = config::add_remote(name, url); } +pub fn remote_list(verbose: bool) { + let remotes = get_all_remote(); + for remote in remotes { + if verbose + { + println!("{} {}", remote.0, remote.1); + } + else + { + println!("{}", remote.0); + } + } + +} diff --git a/src/subcommands/remote.rs b/src/subcommands/remote.rs index f1021aa..1f388e5 100644 --- a/src/subcommands/remote.rs +++ b/src/subcommands/remote.rs @@ -22,6 +22,14 @@ pub fn create() -> App<'static, 'static> { ) .about("Add a new remote to this repository") ) + .arg( + Arg::with_name("verbose") + .short("v") + .long("verbose") + .required(false) + .takes_value(false) + .help("Be a little more verbose and show remote url after name.") + ) } pub fn handler(args: &ArgMatches<'_>) { @@ -32,6 +40,8 @@ pub fn handler(args: &ArgMatches<'_>) { url: add_matches.values_of("url"), }); } - _ => println!("Invalid or missing subcommand for 'remote'"), + _ => { + commands::remote::remote_list(args.is_present("verbose")); + } } } diff --git a/src/utils/path.rs b/src/utils/path.rs index 830dac5..ee3f516 100644 --- a/src/utils/path.rs +++ b/src/utils/path.rs @@ -114,6 +114,13 @@ pub fn nextsync() -> PathBuf { path } +pub fn config() -> PathBuf { + let mut path = repo_root(); + path.push(".nextsync"); + path.push("config"); + path +} + pub fn objects() -> PathBuf { let mut path = repo_root(); path.push(".nextsync");