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

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![];