Skip to content

Commit

Permalink
refactor: split subcommands into separate modules
Browse files Browse the repository at this point in the history
  • Loading branch information
benpueschel committed Aug 7, 2024
1 parent a8edce0 commit 946ee70
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 104 deletions.
8 changes: 8 additions & 0 deletions gritty-clap/src/auth.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use clap::Parser;

#[derive(Debug, Clone, Parser)]
#[command(about = "Authenticate with a remote")]
pub struct Auth {
#[arg(help = "Name of the remote as defined in the config (ex: 'github')")]
pub remote: String,
}
13 changes: 13 additions & 0 deletions gritty-clap/src/clone.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use clap::Parser;

#[derive(Debug, Clone, Parser)]
#[command(about = "Clone a repository from a remote")]
pub struct Clone {
#[arg(help = "Name of the repository")]
pub name: String,
#[arg(help = "Name of the remote as defined in the config (ex: 'github')")]
pub remote: String,
#[arg(short, long, help = "Initialize and clone all submodules")]
pub recursive: bool,
}

54 changes: 54 additions & 0 deletions gritty-clap/src/create.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
use clap::Parser;

use super::OutputFormat;

#[derive(Debug, Clone, Parser)]
#[command(about = "Create a repository on a remote")]
pub struct Create {
#[arg(short, long, help = "Create a private repository")]
pub private: bool,
#[arg(short, long, help = "Clone the repository after creation")]
pub clone: bool,
#[arg(
short,
long,
help = "Initialize and clone all submodules. Only valid with --clone"
)]
pub recursive: bool,
#[arg(
short,
long,
help = "Add the remote to the local git repository as 'origin'. Ignored if --clone is specified",
long_help = "Add the remote to the local git repository as 'origin'.
If the current directory is not a git repository, it will be initialized as one.
Ignored if --clone is specified.
"
)]
pub add_remote: bool,
#[arg(short, long, help = "Description of the repository")]
pub description: Option<String>,
#[arg(short, long, help = "Initialize the repository with a README.md")]
pub init: bool,
#[arg(
short,
long,
help = concat!("License to use for the repository (ex: 'MIT'). ",
"If not provided, or --init is not specified, no license will be addeed.")
)]
pub license: Option<String>,
#[arg(help = "Name of the repository")]
pub name: String,
#[arg(help = "Name of the remote as defined in the config (ex: 'github')")]
pub remote: String,
#[arg(
long,
help = "Change the output format to the specified value (can be 'json')",
long_help = "\
Change the output format to the specified value.
This option is useful for parsing the output of gritty, such as in a script or another
tool integrating with gritty.
Note that the 'create-config' and 'auth' subcommands do not respect this option.
Currently, the only supported format is 'json'."
)]
pub format: Option<OutputFormat>,
}
16 changes: 16 additions & 0 deletions gritty-clap/src/delete.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use clap::Parser;

#[derive(Debug, Clone, Parser)]
#[command(about = "Delete a repository on a remote")]
pub struct Delete {
#[arg(help = "Name of the repository")]
pub name: String,
#[arg(help = "Name of the remote as defined in the config (ex: 'github')")]
pub remote: String,
#[arg(
short,
long,
help = "Force deletion without confirmation. Use with caution!"
)]
pub force: bool,
}
116 changes: 12 additions & 104 deletions gritty-clap/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
use std::env;

mod auth;
mod clone;
mod create;
mod delete;
mod list;

pub use auth::Auth;
pub use clone::Clone;
pub use create::Create;
pub use delete::Delete;
pub use list::List;

use clap::{
builder::styling::{AnsiColor, Effects, Styles},
Parser, Subcommand, ValueEnum,
Expand Down Expand Up @@ -68,107 +80,3 @@ pub enum Commands {
#[command(about = "List all configured remotes")]
ListRemotes,
}

#[derive(Debug, Clone, Parser)]
#[command(about = "Clone a repository from a remote")]
pub struct Clone {
#[arg(help = "Name of the repository")]
pub name: String,
#[arg(help = "Name of the remote as defined in the config (ex: 'github')")]
pub remote: String,
#[arg(short, long, help = "Initialize and clone all submodules")]
pub recursive: bool,
}

#[derive(Debug, Clone, Parser)]
#[command(about = "List repositories on a remote")]
pub struct List {
#[arg(help = "Name of the remote as defined in the config (ex: 'github')")]
pub remote: String,
#[arg(short, long, help = "Show private repositories")]
pub private: bool,
#[arg(short, long, help = "Show forks")]
pub forks: bool,
#[arg(
long,
help = "Change the output format to the specified value (can be 'json')",
long_help = "\
Change the output format to the specified value.
This option is useful for parsing the output of gritty, such as in a script or another
tool integrating with gritty.
Note that the 'create-config' and 'auth' subcommands do not respect this option.
Currently, the only supported format is 'json'."
)]
pub format: Option<OutputFormat>,
}

#[derive(Debug, Clone, Parser)]
#[command(about = "Create a repository on a remote")]
pub struct Create {
#[arg(short, long, help = "Create a private repository")]
pub private: bool,
#[arg(short, long, help = "Clone the repository after creation")]
pub clone: bool,
#[arg(
short,
long,
help = "Initialize and clone all submodules. Only valid with --clone"
)]
pub recursive: bool,
#[arg(
short,
long,
help = "Add the remote to the local git repository as 'origin'. Ignored if --clone is specified",
long_help = "Add the remote to the local git repository as 'origin'.
If the current directory is not a git repository, it will be initialized as one.
Ignored if --clone is specified.
"
)]
pub add_remote: bool,
#[arg(short, long, help = "Description of the repository")]
pub description: Option<String>,
#[arg(short, long, help = "Initialize the repository with a README.md")]
pub init: bool,
#[arg(
short,
long,
help = concat!("License to use for the repository (ex: 'MIT'). ",
"If not provided, or --init is not specified, no license will be addeed.")
)]
pub license: Option<String>,
#[arg(help = "Name of the repository")]
pub name: String,
#[arg(help = "Name of the remote as defined in the config (ex: 'github')")]
pub remote: String,
#[arg(
long,
help = "Change the output format to the specified value (can be 'json')",
long_help = "\
Change the output format to the specified value.
This option is useful for parsing the output of gritty, such as in a script or another
tool integrating with gritty.
Note that the 'create-config' and 'auth' subcommands do not respect this option.
Currently, the only supported format is 'json'."
)]
pub format: Option<OutputFormat>,
}
#[derive(Debug, Clone, Parser)]
#[command(about = "Delete a repository on a remote")]
pub struct Delete {
#[arg(help = "Name of the repository")]
pub name: String,
#[arg(help = "Name of the remote as defined in the config (ex: 'github')")]
pub remote: String,
#[arg(
short,
long,
help = "Force deletion without confirmation. Use with caution!"
)]
pub force: bool,
}
#[derive(Debug, Clone, Parser)]
#[command(about = "Authenticate with a remote")]
pub struct Auth {
#[arg(help = "Name of the remote as defined in the config (ex: 'github')")]
pub remote: String,
}
26 changes: 26 additions & 0 deletions gritty-clap/src/list.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use clap::Parser;

use super::OutputFormat;

#[derive(Debug, Clone, Parser)]
#[command(about = "List repositories on a remote")]
pub struct List {
#[arg(help = "Name of the remote as defined in the config (ex: 'github')")]
pub remote: String,
#[arg(short, long, help = "Show private repositories")]
pub private: bool,
#[arg(short, long, help = "Show forks")]
pub forks: bool,
#[arg(
long,
help = "Change the output format to the specified value (can be 'json')",
long_help = "\
Change the output format to the specified value.
This option is useful for parsing the output of gritty, such as in a script or another
tool integrating with gritty.
Note that the 'create-config' and 'auth' subcommands do not respect this option.
Currently, the only supported format is 'json'."
)]
pub format: Option<OutputFormat>,
}

0 comments on commit 946ee70

Please sign in to comment.