From c6534cfd40042cf20864026001c7e5d61e0afd81 Mon Sep 17 00:00:00 2001 From: grimhilt Date: Tue, 20 Feb 2024 15:45:01 +0100 Subject: [PATCH] feat(remote): add new remote --- src/commands.rs | 1 + src/commands/config.rs | 48 +++++++++++++++++++++++++++++++++++++-- src/commands/remote.rs | 21 +++++++++++++++++ src/main.rs | 2 ++ src/subcommands.rs | 1 + src/subcommands/clone.rs | 1 - src/subcommands/remote.rs | 41 +++++++++++++++++++++++++++++++++ 7 files changed, 112 insertions(+), 3 deletions(-) create mode 100644 src/commands/remote.rs create mode 100644 src/subcommands/remote.rs diff --git a/src/commands.rs b/src/commands.rs index e49e518..12ac4be 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -6,4 +6,5 @@ pub mod clone; pub mod push; pub mod config; pub mod remote_diff; +pub mod remote; pub mod pull; diff --git a/src/commands/config.rs b/src/commands/config.rs index dad853a..edf6153 100644 --- a/src/commands/config.rs +++ b/src/commands/config.rs @@ -2,10 +2,54 @@ use std::fs::OpenOptions; use std::io::{self, Write}; use crate::utils::{path, read}; +pub fn add_remote(name: &str, url: &str) -> io::Result<()> { + let mut root = path::nextsync(); + root.push("config"); + + // check if there is already a remote with this name + if get_remote(name) + { + eprintln!("error: remote {} already exists.", name); + std::process::exit(3); + } + + let mut file = OpenOptions::new() + .read(true) + .write(true) + .create(true) + .append(true) + .open(root)?; + + writeln!(file, "[remote \"{}\"]", name)?; + writeln!(file, "\turl = {}", url)?; + + Ok(()) +} + +pub fn get_remote(name: &str) -> bool { + let mut root = path::nextsync(); + root.push("config"); + + let target = String::from(format!("[remote \"{}\"]", name)); + + if let Ok(lines) = read::read_lines(root) { + for line_result in lines { + if let Ok(line) = line_result { + if line == target { + return true; + } + } + } + } + + return false; + +} + pub fn set(var: &str, val: &str) -> io::Result<()> { let mut root = path::nextsync(); root.push("config"); - + // todo check if exist let mut file = OpenOptions::new() .read(true) @@ -24,7 +68,7 @@ pub fn set(var: &str, val: &str) -> io::Result<()> { pub fn get(var: &str) -> Option { let mut root = path::nextsync(); root.push("config"); - + if let Ok(lines) = read::read_lines(root) { for line in lines { if let Ok(l) = line { diff --git a/src/commands/remote.rs b/src/commands/remote.rs new file mode 100644 index 0000000..5a1c50a --- /dev/null +++ b/src/commands/remote.rs @@ -0,0 +1,21 @@ +use clap::Values; + +use crate::commands::config; + +pub struct RemoteArgs<'a> { + pub name: Option>, + pub url: Option>, +} + +pub fn remote_add(args: RemoteArgs) { + if args.name.is_none() || args.url.is_none() { + eprintln!("Missing argument: remote add command need a name and an url"); + return; + } + + let name = args.name.unwrap().next().unwrap(); + let url = args.url.unwrap().next().unwrap(); + + let _ = config::add_remote(name, url); +} + diff --git a/src/main.rs b/src/main.rs index 88d7f75..54bb9e0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -20,6 +20,7 @@ fn main() { .subcommand(subcommands::add::create()) .subcommand(subcommands::push::create()) .subcommand(subcommands::reset::create()) + .subcommand(subcommands::remote::create()) .subcommand(subcommands::config::create()) .subcommand(subcommands::remote_diff::create()) .subcommand(subcommands::pull::create()) @@ -39,6 +40,7 @@ fn main() { ("config", Some(args)) => subcommands::config::handler(args), ("remote-diff", Some(args)) => subcommands::remote_diff::handler(args), ("pull", Some(args)) => subcommands::pull::handler(args), + ("remote", Some(args)) => subcommands::remote::handler(args), (_, _) => {}, }; diff --git a/src/subcommands.rs b/src/subcommands.rs index e49e518..fe0cef4 100644 --- a/src/subcommands.rs +++ b/src/subcommands.rs @@ -7,3 +7,4 @@ pub mod push; pub mod config; pub mod remote_diff; pub mod pull; +pub mod remote; diff --git a/src/subcommands/clone.rs b/src/subcommands/clone.rs index e785017..23e7d92 100644 --- a/src/subcommands/clone.rs +++ b/src/subcommands/clone.rs @@ -1,5 +1,4 @@ use clap::{App, Arg, SubCommand, ArgMatches}; -use std::borrow::Cow; use textwrap::{fill, Options}; use crate::commands::clone::{self, CloneArgs}; diff --git a/src/subcommands/remote.rs b/src/subcommands/remote.rs new file mode 100644 index 0000000..49099d2 --- /dev/null +++ b/src/subcommands/remote.rs @@ -0,0 +1,41 @@ +use clap::{App, Arg, SubCommand, ArgMatches}; + +use crate::commands; +use crate::commands::remote::RemoteArgs; + +pub fn create() -> App<'static, 'static> { + SubCommand::with_name("remote") + .about("Manage set of tracked repositories") + .subcommand( + SubCommand::with_name("add") + .arg( + Arg::with_name("name") + .required(true) + .multiple(false) + .takes_value(true) + .value_name("NAME") + .help("The name of the remote"), + ) + .arg( + Arg::with_name("url") + .required(true) + .multiple(false) + .takes_value(true) + .value_name("URL") + .help("The url of the remote"), + ) + .about("Add a new remote to this repository") + ) +} + +pub fn handler(args: &ArgMatches<'_>) { + match args.subcommand() { + ("add", Some(add_matches)) => { + commands::remote::remote_add(RemoteArgs { + name: add_matches.values_of("name"), + url: add_matches.values_of("url"), + }); + } + _ => println!("Invalid or missing subcommand for 'remote'"), + } +}