feat(remote): list remote with verbose option
This commit is contained in:
parent
8ed86a05ea
commit
d8b2116aeb
@ -30,14 +30,13 @@ pub fn config_set(args: ConfigSetArgs) {
|
|||||||
|
|
||||||
|
|
||||||
pub fn find_option_in_cat(category: &str, option: &str) -> Option<String> {
|
pub fn find_option_in_cat(category: &str, option: &str) -> Option<String> {
|
||||||
let mut root = path::nextsync();
|
let mut config = path::nextsync();
|
||||||
root.push("config");
|
config.push("config");
|
||||||
|
|
||||||
let mut in_target_category = false;
|
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 {
|
for line in lines {
|
||||||
|
|
||||||
if let Ok(line) = line {
|
if let Ok(line) = line {
|
||||||
let trimmed_line = line.trim();
|
let trimmed_line = line.trim();
|
||||||
|
|
||||||
@ -58,14 +57,14 @@ pub fn find_option_in_cat(category: &str, option: &str) -> Option<String> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn write_option_in_cat(category: &str, option: &str, value: &str) -> io::Result<()> {
|
pub fn write_option_in_cat(category: &str, option: &str, value: &str) -> io::Result<()> {
|
||||||
let mut root = path::nextsync();
|
let mut config = path::nextsync();
|
||||||
root.push("config");
|
config.push("config");
|
||||||
|
|
||||||
let mut file = OpenOptions::new()
|
let mut file = OpenOptions::new()
|
||||||
.read(true)
|
.read(true)
|
||||||
.write(true)
|
.write(true)
|
||||||
.create(true)
|
.create(true)
|
||||||
.open(&root)?;
|
.open(&config)?;
|
||||||
|
|
||||||
let mut in_target_category = false;
|
let mut in_target_category = false;
|
||||||
let mut option_found = 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<()> {
|
pub fn add_remote(name: &str, url: &str) -> io::Result<()> {
|
||||||
let mut root = path::nextsync();
|
let mut config = path::config();
|
||||||
root.push("config");
|
|
||||||
|
|
||||||
// check if there is already a remote with this name
|
// check if there is already a remote with this name
|
||||||
if get_remote(name).is_some()
|
if get_remote(name).is_some()
|
||||||
@ -144,7 +142,7 @@ pub fn add_remote(name: &str, url: &str) -> io::Result<()> {
|
|||||||
.write(true)
|
.write(true)
|
||||||
.create(true)
|
.create(true)
|
||||||
.append(true)
|
.append(true)
|
||||||
.open(root)?;
|
.open(config)?;
|
||||||
|
|
||||||
writeln!(file, "[remote \"{}\"]", name)?;
|
writeln!(file, "[remote \"{}\"]", name)?;
|
||||||
writeln!(file, "\turl = {}", url)?;
|
writeln!(file, "\turl = {}", url)?;
|
||||||
@ -156,20 +154,55 @@ pub fn get_remote(name: &str) -> Option<String> {
|
|||||||
find_option_in_cat(&format!("remote \"{}\"", name), "url")
|
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<String> {
|
pub fn get_core(name: &str) -> Option<String> {
|
||||||
find_option_in_cat("core", name)
|
find_option_in_cat("core", name)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn add_core(name: &str, value: &str) -> io::Result<()> {
|
pub fn add_core(name: &str, value: &str) -> io::Result<()> {
|
||||||
let mut root = path::nextsync();
|
let mut config = path::nextsync();
|
||||||
root.push("config");
|
config.push("config");
|
||||||
|
|
||||||
let mut file = OpenOptions::new()
|
let mut file = OpenOptions::new()
|
||||||
.read(true)
|
.read(true)
|
||||||
.write(true)
|
.write(true)
|
||||||
.create(true)
|
.create(true)
|
||||||
.append(true)
|
.append(true)
|
||||||
.open(root)?;
|
.open(config)?;
|
||||||
|
|
||||||
writeln!(file, "[core]")?;
|
writeln!(file, "[core]")?;
|
||||||
writeln!(file, "\t{} = {}", name, value)?;
|
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<String> {
|
pub fn get(var: &str) -> Option<String> {
|
||||||
let mut root = path::nextsync();
|
let mut config = path::config();
|
||||||
root.push("config");
|
|
||||||
|
|
||||||
if let Ok(lines) = read::read_lines(root) {
|
if let Ok(lines) = read::read_lines(config) {
|
||||||
for line in lines {
|
for line in lines {
|
||||||
if let Ok(l) = line {
|
if let Ok(l) = line {
|
||||||
if l.starts_with(var) {
|
if l.starts_with(var) {
|
||||||
|
@ -2,6 +2,8 @@ use clap::Values;
|
|||||||
|
|
||||||
use crate::commands::config;
|
use crate::commands::config;
|
||||||
|
|
||||||
|
use super::config::get_all_remote;
|
||||||
|
|
||||||
pub struct RemoteArgs<'a> {
|
pub struct RemoteArgs<'a> {
|
||||||
pub name: Option<Values<'a>>,
|
pub name: Option<Values<'a>>,
|
||||||
pub url: Option<Values<'a>>,
|
pub url: Option<Values<'a>>,
|
||||||
@ -19,3 +21,17 @@ pub fn remote_add(args: RemoteArgs) {
|
|||||||
let _ = config::add_remote(name, url);
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
@ -22,6 +22,14 @@ pub fn create() -> App<'static, 'static> {
|
|||||||
)
|
)
|
||||||
.about("Add a new remote to this repository")
|
.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<'_>) {
|
pub fn handler(args: &ArgMatches<'_>) {
|
||||||
@ -32,6 +40,8 @@ pub fn handler(args: &ArgMatches<'_>) {
|
|||||||
url: add_matches.values_of("url"),
|
url: add_matches.values_of("url"),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
_ => println!("Invalid or missing subcommand for 'remote'"),
|
_ => {
|
||||||
|
commands::remote::remote_list(args.is_present("verbose"));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -114,6 +114,13 @@ pub fn nextsync() -> PathBuf {
|
|||||||
path
|
path
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn config() -> PathBuf {
|
||||||
|
let mut path = repo_root();
|
||||||
|
path.push(".nextsync");
|
||||||
|
path.push("config");
|
||||||
|
path
|
||||||
|
}
|
||||||
|
|
||||||
pub fn objects() -> PathBuf {
|
pub fn objects() -> PathBuf {
|
||||||
let mut path = repo_root();
|
let mut path = repo_root();
|
||||||
path.push(".nextsync");
|
path.push(".nextsync");
|
||||||
|
Loading…
Reference in New Issue
Block a user