From ff04bb7d5fa1739bd2c93e5c78b9002da1760317 Mon Sep 17 00:00:00 2001 From: "Christopher L. Crutchfield" Date: Sun, 24 Nov 2024 14:49:42 -0800 Subject: [PATCH] feat: start of main subcommand to handle requests from git --- src/git_lfs/custom_transfer_agent.rs | 5 ++++ src/git_lfs/git_lfs_parser.rs | 31 +++++++++++++++++++++++ src/git_lfs/mod.rs | 5 ++++ src/main.rs | 17 +++++++++---- src/subcommands/login_subcommand.rs | 5 ++-- src/subcommands/logout_subcommand.rs | 2 +- src/subcommands/main_subcommand.rs | 38 ++++++++++++++++++++++++++++ src/subcommands/mod.rs | 4 ++- src/subcommands/subcommand.rs | 2 +- 9 files changed, 99 insertions(+), 10 deletions(-) create mode 100644 src/git_lfs/custom_transfer_agent.rs create mode 100644 src/git_lfs/git_lfs_parser.rs create mode 100644 src/git_lfs/mod.rs create mode 100644 src/subcommands/main_subcommand.rs diff --git a/src/git_lfs/custom_transfer_agent.rs b/src/git_lfs/custom_transfer_agent.rs new file mode 100644 index 0000000..4f67292 --- /dev/null +++ b/src/git_lfs/custom_transfer_agent.rs @@ -0,0 +1,5 @@ +use anyhow::Result; + +pub trait CustomTransferAgent { + async fn init(&mut self) -> Result<()>; +} \ No newline at end of file diff --git a/src/git_lfs/git_lfs_parser.rs b/src/git_lfs/git_lfs_parser.rs new file mode 100644 index 0000000..e7ea35a --- /dev/null +++ b/src/git_lfs/git_lfs_parser.rs @@ -0,0 +1,31 @@ +use anyhow::Result; +use serde::{Deserialize, Serialize}; + +use super::CustomTransferAgent; + +#[derive(Debug, Serialize, Deserialize, Clone)] +#[serde[rename_all = "snake_case"]] +struct Event { + event: String, + operation: String, + remote: String, + concurrent: bool +} + +pub struct GitLfsParser<'custom_transfer_agent, T: CustomTransferAgent> { + custom_transfer_agent: &'custom_transfer_agent mut T +} + +impl<'custom_transfer_agent, T: CustomTransferAgent> GitLfsParser<'custom_transfer_agent, T> { + pub fn new(custom_transfer_agent: &mut T) -> GitLfsParser:: { + GitLfsParser:: { + custom_transfer_agent + } + } + + pub async fn listen(&mut self) -> Result<()> { + self.custom_transfer_agent.init().await?; + + Ok(()) + } +} \ No newline at end of file diff --git a/src/git_lfs/mod.rs b/src/git_lfs/mod.rs new file mode 100644 index 0000000..621fea3 --- /dev/null +++ b/src/git_lfs/mod.rs @@ -0,0 +1,5 @@ +mod custom_transfer_agent; +mod git_lfs_parser; + +pub use custom_transfer_agent::CustomTransferAgent; +pub use git_lfs_parser::GitLfsParser; \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index ed045eb..61f09c7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,11 +5,12 @@ use tracing_appender::rolling; use tracing_subscriber::fmt::writer::MakeWriterExt; mod config_dir; -mod subcommands; mod credential_manager; +mod git_lfs; +mod subcommands; mod synology_api; -use subcommands::{LoginSubcommand, LogoutSubcommand, Subcommand}; +use subcommands::{LoginSubcommand, LogoutSubcommand, MainSubcommand, Subcommand}; fn setup_logging() -> Result<()> { let config_path = get_config_dir()?; @@ -81,17 +82,23 @@ async fn main() -> Result<()> { match matches.subcommand() { Some(("login", sub_matches)) => { - let login_command = LoginSubcommand { }; + let mut login_command = LoginSubcommand { }; login_command.execute(sub_matches).await?; Ok(()) }, Some(("logout", sub_matches)) => { - let logout_command = LogoutSubcommand { }; + let mut logout_command = LogoutSubcommand { }; logout_command.execute(sub_matches).await?; Ok(()) } - _ => Ok(()) + _ => { + // This is the subcommand that handles being called from git. + let mut main_command = MainSubcommand::new(); + main_command.execute(&matches).await?; + + Ok(()) + } } } diff --git a/src/subcommands/login_subcommand.rs b/src/subcommands/login_subcommand.rs index 33fe695..bfc2ad7 100644 --- a/src/subcommands/login_subcommand.rs +++ b/src/subcommands/login_subcommand.rs @@ -1,17 +1,18 @@ use anyhow::{Context, Result}; use clap::ArgMatches; -use crate::subcommands::Subcommand; use crate::credential_manager::{Credential, CredentialManager}; use crate::synology_api::SynologyFileStation; +use super::Subcommand; + #[derive(Debug)] pub struct LoginSubcommand { } impl Subcommand for LoginSubcommand { #[tracing::instrument] - async fn execute(&self, arg_matches: &ArgMatches) -> Result<()> { + async fn execute(&mut self, arg_matches: &ArgMatches) -> Result<()> { let url = arg_matches.get_one::("URL").context("URL not provided.")?; let user = arg_matches.get_one::("USER").context("USER not provided.")?; let totp_command = arg_matches.get_one::("TOTP_COMMAND"); diff --git a/src/subcommands/logout_subcommand.rs b/src/subcommands/logout_subcommand.rs index d3be083..511c95d 100644 --- a/src/subcommands/logout_subcommand.rs +++ b/src/subcommands/logout_subcommand.rs @@ -10,7 +10,7 @@ pub struct LogoutSubcommand { impl Subcommand for LogoutSubcommand { #[tracing::instrument] - async fn execute(&self, arg_matches: &ArgMatches) -> Result<()> { + async fn execute(&mut self, arg_matches: &ArgMatches) -> Result<()> { let url = arg_matches.get_one::("URL").context("URL not provided.")?; let mut credential_manager = CredentialManager::new()?; diff --git a/src/subcommands/main_subcommand.rs b/src/subcommands/main_subcommand.rs new file mode 100644 index 0000000..e3ce9c9 --- /dev/null +++ b/src/subcommands/main_subcommand.rs @@ -0,0 +1,38 @@ +use anyhow::Result; +use clap::ArgMatches; + +use crate::{credential_manager::CredentialManager, git_lfs::{CustomTransferAgent, GitLfsParser}}; + +use super::Subcommand; + +#[derive(Debug)] +pub struct MainSubcommand { + credential_manager: Option +} + +impl CustomTransferAgent for MainSubcommand { + async fn init(&mut self) -> Result<()> { + // Init credential manager + // Init synology api + + Ok(()) + } +} + +impl Subcommand for MainSubcommand { + #[tracing::instrument] + async fn execute(&mut self, arg_matches: &ArgMatches) -> Result<()> { + let mut parser = GitLfsParser::::new(self); + parser.listen().await?; + + Ok(()) + } +} + +impl MainSubcommand { + pub fn new() -> MainSubcommand { + MainSubcommand { + credential_manager: None + } + } +} \ No newline at end of file diff --git a/src/subcommands/mod.rs b/src/subcommands/mod.rs index 593897f..66fe1e3 100644 --- a/src/subcommands/mod.rs +++ b/src/subcommands/mod.rs @@ -1,7 +1,9 @@ -mod subcommand; mod login_subcommand; mod logout_subcommand; +mod main_subcommand; +mod subcommand; pub use login_subcommand::LoginSubcommand; pub use logout_subcommand::LogoutSubcommand; +pub use main_subcommand::MainSubcommand; pub use subcommand::Subcommand; diff --git a/src/subcommands/subcommand.rs b/src/subcommands/subcommand.rs index 99ef5b8..dd0bb2e 100644 --- a/src/subcommands/subcommand.rs +++ b/src/subcommands/subcommand.rs @@ -2,5 +2,5 @@ use anyhow::Result; use clap::ArgMatches; pub trait Subcommand { - async fn execute(&self, arg_matches: &ArgMatches) -> Result<()>; + async fn execute(&mut self, arg_matches: &ArgMatches) -> Result<()>; } \ No newline at end of file