feat(remote): add new remote
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user