Skip to content

Commit

Permalink
feat: start of main subcommand to handle requests from git
Browse files Browse the repository at this point in the history
  • Loading branch information
ccrutchf committed Nov 24, 2024
1 parent 4369738 commit ff04bb7
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 10 deletions.
5 changes: 5 additions & 0 deletions src/git_lfs/custom_transfer_agent.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
use anyhow::Result;

pub trait CustomTransferAgent {
async fn init(&mut self) -> Result<()>;
}
31 changes: 31 additions & 0 deletions src/git_lfs/git_lfs_parser.rs
Original file line number Diff line number Diff line change
@@ -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::<T> {
GitLfsParser::<T> {
custom_transfer_agent
}
}

pub async fn listen(&mut self) -> Result<()> {
self.custom_transfer_agent.init().await?;

Ok(())
}
}
5 changes: 5 additions & 0 deletions src/git_lfs/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
mod custom_transfer_agent;
mod git_lfs_parser;

pub use custom_transfer_agent::CustomTransferAgent;
pub use git_lfs_parser::GitLfsParser;
17 changes: 12 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()?;
Expand Down Expand Up @@ -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(())
}
}
}
5 changes: 3 additions & 2 deletions src/subcommands/login_subcommand.rs
Original file line number Diff line number Diff line change
@@ -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::<String>("URL").context("URL not provided.")?;
let user = arg_matches.get_one::<String>("USER").context("USER not provided.")?;
let totp_command = arg_matches.get_one::<String>("TOTP_COMMAND");
Expand Down
2 changes: 1 addition & 1 deletion src/subcommands/logout_subcommand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<String>("URL").context("URL not provided.")?;

let mut credential_manager = CredentialManager::new()?;
Expand Down
38 changes: 38 additions & 0 deletions src/subcommands/main_subcommand.rs
Original file line number Diff line number Diff line change
@@ -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<CredentialManager>
}

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::<MainSubcommand>::new(self);
parser.listen().await?;

Ok(())
}
}

impl MainSubcommand {
pub fn new() -> MainSubcommand {
MainSubcommand {
credential_manager: None
}
}
}
4 changes: 3 additions & 1 deletion src/subcommands/mod.rs
Original file line number Diff line number Diff line change
@@ -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;
2 changes: 1 addition & 1 deletion src/subcommands/subcommand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<()>;
}

0 comments on commit ff04bb7

Please sign in to comment.