feat(remote): add new remote
This commit is contained in:
parent
7719e27fe8
commit
ef986305c0
@ -6,4 +6,5 @@ pub mod clone;
|
||||
pub mod push;
|
||||
pub mod config;
|
||||
pub mod remote_diff;
|
||||
pub mod remote;
|
||||
pub mod pull;
|
||||
|
@ -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<String> {
|
||||
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 {
|
||||
|
21
src/commands/remote.rs
Normal file
21
src/commands/remote.rs
Normal file
@ -0,0 +1,21 @@
|
||||
use clap::Values;
|
||||
|
||||
use crate::commands::config;
|
||||
|
||||
pub struct RemoteArgs<'a> {
|
||||
pub name: Option<Values<'a>>,
|
||||
pub url: Option<Values<'a>>,
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
@ -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),
|
||||
|
||||
(_, _) => {},
|
||||
};
|
||||
|
@ -7,3 +7,4 @@ pub mod push;
|
||||
pub mod config;
|
||||
pub mod remote_diff;
|
||||
pub mod pull;
|
||||
pub mod remote;
|
||||
|
@ -1,5 +1,4 @@
|
||||
use clap::{App, Arg, SubCommand, ArgMatches};
|
||||
use std::borrow::Cow;
|
||||
use textwrap::{fill, Options};
|
||||
|
||||
use crate::commands::clone::{self, CloneArgs};
|
||||
|
41
src/subcommands/remote.rs
Normal file
41
src/subcommands/remote.rs
Normal file
@ -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'"),
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user