1use std::{env::current_dir, path::PathBuf};
2
3use clap::{Parser, Subcommand};
4
5use crate::config_dir;
6
7#[derive(Parser, Debug)]
8#[command()]
9pub struct Args {
10 #[command(subcommand)]
11 pub commands: Option<Subcommands>,
12
13 remote_name: Option<String>,
14
15 url: Option<String>,
16
17 #[arg(long = "git-dir", env = "GIT_DIR", default_value = get_default_git_dir().into_os_string())]
18 pub git_dir: PathBuf,
19
20 #[arg(long = "config-dir", env = "CONFIG_DIR")]
21 pub config_dir: Option<PathBuf>,
22}
23
24fn get_default_git_dir() -> PathBuf {
25 current_dir().unwrap().join(".git")
26}
27
28pub struct ClientArgs {
29 pub remote_name: String,
30 pub url: String,
31 pub git_dir: PathBuf,
32 pub config_dir: PathBuf,
33}
34
35impl Args {
36 pub fn client_args(&self) -> Result<Option<ClientArgs>, anyhow::Error> {
37 Ok(match (&self.remote_name, &self.url) {
38 (Some(remote_name), Some(url)) => Some(ClientArgs {
39 remote_name: remote_name.to_string(),
40 url: url.to_string(),
41 git_dir: self.git_dir.clone(),
42 config_dir: self.config_dir()?,
43 }),
44 _ => None,
45 })
46 }
47
48 pub fn config_dir(&self) -> Result<PathBuf, anyhow::Error> {
49 Ok(match self.config_dir {
50 Some(ref config_dir) => config_dir.clone(),
51 None => config_dir()?,
52 })
53 }
54}
55
56#[derive(Subcommand, Debug)]
57pub enum Subcommands {
58 Whoami,
59 Peer {
60 #[command(subcommand)]
61 commands: Option<PeerCommands>,
62
63 #[arg(default_value = "default")]
64 ring: Option<String>,
65 },
66 Ring,
67}
68
69#[derive(Subcommand, Debug)]
70pub enum PeerCommands {
71 Add {
72 #[arg(skip = "default")]
73 ring: String,
74 name: String,
75 id: String,
76 },
77}