add config commands
This commit is contained in:
52
src/commands/config.rs
Normal file
52
src/commands/config.rs
Normal 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
|
||||
}
|
||||
@@ -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"));
|
||||
}
|
||||
|
||||
@@ -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![];
|
||||
|
||||
Reference in New Issue
Block a user