diff --git a/src/commands.rs b/src/commands.rs index fe0ae79..c346a7e 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -4,3 +4,4 @@ pub mod add; pub mod reset; pub mod clone; pub mod push; +pub mod config; diff --git a/src/commands/config.rs b/src/commands/config.rs new file mode 100644 index 0000000..2536f75 --- /dev/null +++ b/src/commands/config.rs @@ -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 { + 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 +} diff --git a/src/commands/push.rs b/src/commands/push.rs index 1addb4b..a253c81 100644 --- a/src/commands/push.rs +++ b/src/commands/push.rs @@ -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")); } diff --git a/src/commands/status.rs b/src/commands/status.rs index 713eb99..5655b31 100644 --- a/src/commands/status.rs +++ b/src/commands/status.rs @@ -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, Vec, Vec) { let mut hashes = HashMap::new(); let mut objs: Vec = vec![]; diff --git a/src/main.rs b/src/main.rs index 0ae3af0..aa80cbe 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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()); + } + } } }