git-remote-iroh 0.2.0

Git remote protocol support for https://www.iroh.computer
Documentation
use std::{env::current_dir, path::PathBuf};

use clap::{Parser, Subcommand};

use crate::config_dir;

#[derive(Parser, Debug)]
#[command()]
pub struct Args {
    #[command(subcommand)]
    pub commands: Option<Subcommands>,

    remote_name: Option<String>,

    url: Option<String>,

    #[arg(long = "git-dir", env = "GIT_DIR", default_value = get_default_git_dir().into_os_string())]
    pub git_dir: PathBuf,

    #[arg(long = "config-dir", env = "CONFIG_DIR")]
    pub config_dir: Option<PathBuf>,
}

fn get_default_git_dir() -> PathBuf {
    current_dir().unwrap().join(".git")
}

pub struct ClientArgs {
    pub remote_name: String,
    pub url: String,
    pub git_dir: PathBuf,
    pub config_dir: PathBuf,
}

impl Args {
    pub fn client_args(&self) -> Result<Option<ClientArgs>, anyhow::Error> {
        Ok(match (&self.remote_name, &self.url) {
            (Some(remote_name), Some(url)) => Some(ClientArgs {
                remote_name: remote_name.to_string(),
                url: url.to_string(),
                git_dir: self.git_dir.clone(),
                config_dir: self.config_dir()?,
            }),
            _ => None,
        })
    }

    pub fn config_dir(&self) -> Result<PathBuf, anyhow::Error> {
        Ok(match self.config_dir {
            Some(ref config_dir) => config_dir.clone(),
            None => config_dir()?,
        })
    }
}

#[derive(Subcommand, Debug)]
pub enum Subcommands {
    Whoami,
    Peer {
        #[command(subcommand)]
        commands: Option<PeerCommands>,

        #[arg(default_value = "default")]
        ring: Option<String>,
    },
    Ring,
}

#[derive(Subcommand, Debug)]
pub enum PeerCommands {
    Add {
        #[arg(skip = "default")]
        ring: String,
        name: String,
        id: String,
    },
}