add config commands

This commit is contained in:
grimhilt 2023-06-08 18:19:44 +02:00
parent 82102c4608
commit 1b63c86c1a
5 changed files with 76 additions and 3 deletions

View File

@ -4,3 +4,4 @@ pub mod add;
pub mod reset;
pub mod clone;
pub mod push;
pub mod config;

52
src/commands/config.rs Normal file
View File

@ -0,0 +1,52 @@
use crate::utils::path;
use crate::utils::read;
use std::fs::OpenOptions;
use std::io::Write;
pub fn set(var: &str, val: &str) {
let mut root = match path::nextsync() {
Some(path) => path,
None => {
eprintln!("fatal: not a nextsync repository (or any of the parent directories): .nextsync");
std::process::exit(1);
}
};
root.push("config");
// todo check if exist
let mut file = OpenOptions::new()
.read(true)
.write(true)
.create(true)
.append(true)
.open(root).unwrap();
let mut line = var.to_owned();
line.push_str(" ");
line.push_str(val);
writeln!(file, "{}", line);
}
pub fn get(var: &str) -> Option<String> {
let mut root = match path::nextsync() {
Some(path) => path,
None => {
eprintln!("fatal: not a nextsync repository (or any of the parent directories): .nextsync");
std::process::exit(1);
}
};
root.push("config");
if let Ok(lines) = read::read_lines(root) {
for line in lines {
if let Ok(l) = line {
dbg!(l.clone());
if l.starts_with(var.clone()) {
let (_, val) = l.split_once(" ").unwrap();
return Some(val.to_owned());
}
}
}
}
None
}

View File

@ -1,6 +1,7 @@
use crate::commands::status;
use crate::commands::{status, config};
pub fn push() {
dbg!(status::get_diff());
let (staged_obj, new_obj, del_obj) = status::get_diff();
dbg!(config::get("remote"));
}

View File

@ -29,8 +29,6 @@ pub struct Obj {
path: PathBuf,
}
// todo next time
// add relative path in command and in obj
pub fn get_diff() -> (Vec<String>, Vec<String>, Vec<Obj>) {
let mut hashes = HashMap::new();
let mut objs: Vec<String> = vec![];

View File

@ -55,6 +55,21 @@ fn main() {
.help("Files to add"),
)
)
.subcommand(
SubCommand::with_name("config")
.arg(
Arg::with_name("variable")
.required(true)
.takes_value(true)
.value_name("VARIABLE")
)
.arg(
Arg::with_name("value")
.required(true)
.takes_value(true)
.value_name("VALUE")
)
)
.get_matches();
if let Some(matches) = matches.subcommand_matches("init") {
@ -82,6 +97,12 @@ fn main() {
}
} else if let Some(matches) = matches.subcommand_matches("push") {
commands::push::push();
} else if let Some(matches) = matches.subcommand_matches("config") {
if let Some(mut var) = matches.values_of("variable") {
if let Some(mut val) = matches.values_of("value") {
commands::config::set(var.next().unwrap(), val.next().unwrap());
}
}
}
}