notes 0.2.0-20200824

A simple tool for taking notes. Work in progress. See: https://gitlab.com/remipassmoilesel/notes
use crate::config::Config;
use crate::default_error::DefaultError;
use crate::note::Note;
use crate::shell::Shell;

pub trait Git {
    fn init(&self) -> Result<(), DefaultError>;
    fn commit(&self, note: &Note, message: String) -> Result<(), DefaultError>;
    fn push(&self) -> Result<(), DefaultError>;
    fn pull(&self) -> Result<(), DefaultError>;
}

pub struct GitImpl<'a> {
    config: &'a Config,
    shell: &'a dyn Shell,
}

impl<'a> GitImpl<'a> {
    pub fn new(shell: &'a dyn Shell, config: &'a Config) -> GitImpl<'a> {
        GitImpl { shell, config }
    }
}

impl<'a> Git for GitImpl<'a> {
    fn init(&self) -> Result<(), DefaultError> {
        self.shell.execute(format!("git init"), &self.config.storage_directory)
    }

    fn commit(&self, note: &Note, message: String) -> Result<(), DefaultError> {
        let path = &note.path.to_str().unwrap();
        self.shell.execute_in_repo(format!("git add '{}'", path))?;
        self.shell.execute_in_repo(format!("git commit -m '{}' {}", message, path))
    }

    fn push(&self) -> Result<(), DefaultError> {
        self.shell.execute_in_repo(format!("git push"))
    }

    fn pull(&self) -> Result<(), DefaultError> {
        self.shell.execute_in_repo(format!("git pull"))
    }
}