index : git-mirror-sync.git

ascending towards madness

author holly sparkles <sparkles@holly.sh> 2024-01-20 10:16:12.0 +00:00:00
committer holly sparkles <sparkles@holly.sh> 2024-01-20 10:16:12.0 +00:00:00
commit
c9054dc7ce3e7066e07cfe57bdfc29725f6b9b49 [patch]
tree
6ec2ec1f7e7e1d268ba80778d459c2d75e406502
parent
b36a30becd0fe7397dd850f1e7f07bc6b180ba3c
download
c9054dc7ce3e7066e07cfe57bdfc29725f6b9b49.tar.gz

refactor: move cli args to own module



Diff

 src/cli.rs  | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 src/main.rs | 70 +---------------------------------------------------------
 2 files changed, 77 insertions(+), 68 deletions(-)

diff --git a/src/cli.rs b/src/cli.rs
new file mode 100644
index 0000000..9b1e7d7
--- /dev/null
+++ b/src/cli.rs
@@ -0,0 +1,75 @@
use std::path::PathBuf;

use clap::Parser;
use humantime::Duration;

use crate::error::ParseError;

/// Command line arguments for the application.
///
/// Fields:
/// - `refresh_time`: Specifies the interval to wait before refreshing repositories.
/// (Default: 10m)
/// - `directory`: The directory path where git repositories are located.
#[derive(Debug, Parser)]
pub struct CliArgs {
    /// The time to wait before refreshing repositories.
    /// eg. 10m, 60s, etc.
    #[clap(short, long, default_value = "10m")]
    #[arg(value_parser = parse_refresh_time)]
    pub refresh_time: Duration,

    /// The directory to search for git repositories.
    /// This must be valid, and accessible by the application.
    #[clap(required = true)]
    #[arg(value_parser = parse_directory)]
    pub directory: PathBuf,
}

impl Default for CliArgs {
    fn default() -> Self {
        Self {
            refresh_time: parse_refresh_time("10m").unwrap(),
            directory: Default::default(),
        }
    }
}

/// Parses the given string argument into a `PathBuf` and checks if it is a valid directory.
///
/// # Arguments
///
/// - `arg` - A string containing the path to be parsed.
///
/// # Returns
///
/// This function returns `Ok(PathBuf)` if the path is a valid directory,
/// or `Err(ParseError)` if the path is not a valid directory.
fn parse_directory(arg: &str) -> Result<PathBuf, ParseError> {
    let directory = PathBuf::from(arg);

    if directory.exists() && directory.is_dir() {
        Ok(PathBuf::from(arg))
    } else {
        Err(ParseError::new(
            "path does not exist or is not a directory.",
        ))
    }
}

/// Parses a duration string into a `humantime::Duration`.
///
/// This function takes a duration string (e.g., "10m", "2h 15min")
/// and parses it to `humantime::Duration`.
///
/// # Arguments
///
/// - `arg` - A string representing the duration.
///
/// # Returns
///
/// A `Result` which is `Ok(humantime::Duration)` if the string is successfully parsed,
/// or `Err(ParseError)` if unsuccessful.
fn parse_refresh_time(arg: &str) -> Result<humantime::Duration, ParseError> {
    arg.parse::<humantime::Duration>().map_err(ParseError::from)
}
diff --git a/src/main.rs b/src/main.rs
index e55302a..f6a7496 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,37 +1,10 @@
use std::path::PathBuf;

use clap::Parser;
use humantime::Duration;
use cli::CliArgs;
use tokio::time::sleep;

use crate::error::ParseError;

mod cli;
mod error;

#[derive(Debug, Parser)]
struct CliArgs {
    /// The time to wait before refreshing repositories.
    /// eg. 10m, 60s, etc.
    #[clap(short, long, default_value = "10m")]
    #[arg(value_parser = parse_refresh_time)]
    pub refresh_time: Duration,

    /// The directory to search for git repositories.
    /// This must be valid, and accessible by the application.
    #[clap(required = true)]
    #[arg(value_parser = parse_directory)]
    pub directory: PathBuf,
}

impl Default for CliArgs {
    fn default() -> Self {
        Self {
            refresh_time: parse_refresh_time("10m").unwrap(),
            directory: Default::default(),
        }
    }
}

#[tokio::main]
async fn main() {
    loop {
@@ -45,42 +18,3 @@ async fn do_sync_task(args: CliArgs) {

    sleep(args.refresh_time.into()).await;
}

/// Parses the given string argument into a `PathBuf` and checks if it is a valid directory.
///
/// # Arguments
///
/// * `arg` - A string slice that holds the path to be parsed.
///
/// # Returns
///
/// This function returns `Ok(PathBuf)` if the path is a valid directory,
/// or `Err(Error)` if the path is not a valid directory.
fn parse_directory(arg: &str) -> Result<PathBuf, ParseError> {
    let directory = PathBuf::from(arg);

    if directory.exists() && directory.is_dir() {
        Ok(PathBuf::from(arg))
    } else {
        Err(ParseError::new(
            "path does not exist or is not a directory.",
        ))
    }
}

/// Parses a duration string into a `humantime::Duration`.
///
/// This function takes a duration string (e.g., "10m", "2h 15min")
/// and parses it to `humantime::Duration`.
///
/// # Arguments
///
/// * `arg` - A string representing the duration.
///
/// # Returns
///
/// A `Result` which is `Ok(humantime::Duration)` if the string is successfully parsed,
/// or `Err(ParseError)` if unsuccessful.
fn parse_refresh_time(arg: &str) -> Result<humantime::Duration, ParseError> {
    arg.parse::<humantime::Duration>().map_err(ParseError::from)
}