Skip to content

Commit

Permalink
refactor: use derive interface of clap
Browse files Browse the repository at this point in the history
  • Loading branch information
ErichDonGubler committed Apr 4, 2024
1 parent 2919c34 commit dd28e6f
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 74 deletions.
19 changes: 19 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ version = "0.18"
default-features = false

[dependencies]
clap = { version = "4", features = ["cargo", "wrap_help"] }
clap = { version = "4", features = ["cargo", "wrap_help", "derive"] }
clap_complete = "4"
clap_complete_nushell = "4"
slog = "2.5"
Expand Down
125 changes: 52 additions & 73 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,77 +1,56 @@
#[macro_use]
extern crate clap;

#[macro_use]
extern crate slog;

use clap::ArgAction;
use clap::{CommandFactory, Parser as _};
use clap_complete::{generate, Shell};
use clap_complete_nushell::Nushell;
use slog::Drain;
use std::io;

#[derive(Debug, clap::Parser)]
#[clap(about = "Automatically absorb staged changes into your current branch")]
struct Cli {
/// Use this commit as the base of the absorb stack
#[clap(long, short, value_name = "base")]
base: Option<String>,
/// Don't make any actual changes
#[clap(long, short = 'n')]
dry_run: bool,
/// Skip safety checks
#[clap(long, short)]
force: bool,
/// Display more output
#[clap(long, short)]
verbose: bool,
/// Run rebase if successful
#[clap(long, short = 'r')]
and_rebase: bool,
/// Generate completions
#[clap(long, value_name = "gen-completions", value_parser = ["bash", "fish", "nushell", "zsh", "powershell", "elvish"])]
gen_completions: Option<String>,
/// Match the change against the complete file
#[clap(long, short)]
whole_file: bool,
/// Only generate one fixup per commit
#[clap(long, short = 'F')]
one_fixup_per_commit: bool,
}

fn main() {
let args = command!()
.about("Automatically absorb staged changes into your current branch")
.arg(
clap::Arg::new("base")
.help("Use this commit as the base of the absorb stack")
.short('b')
.long("base"),
)
.arg(
clap::Arg::new("dry-run")
.help("Don't make any actual changes")
.short('n')
.long("dry-run")
.action(ArgAction::SetTrue),
)
.arg(
clap::Arg::new("force")
.help("Skip safety checks")
.short('f')
.long("force")
.action(ArgAction::SetTrue),
)
.arg(
clap::Arg::new("verbose")
.help("Display more output")
.short('v')
.long("verbose")
.action(ArgAction::SetTrue),
)
.arg(
clap::Arg::new("and-rebase")
.help("Run rebase if successful")
.short('r')
.long("and-rebase")
.action(ArgAction::SetTrue),
)
.arg(
clap::Arg::new("gen-completions")
.help("Generate completions")
.long("gen-completions")
.value_parser(["bash", "fish", "nushell", "zsh", "powershell", "elvish"]),
)
.arg(
clap::Arg::new("whole-file")
.help("Match the change against the complete file ")
.short('w')
.long("whole-file")
.action(ArgAction::SetTrue),
)
.arg(
clap::Arg::new("one-fixup-per-commit")
.help("Only generate one fixup per commit")
.short('F')
.long("one-fixup-per-commit")
.action(ArgAction::SetTrue),
);
let mut args_clone = args.clone();
let args = args.get_matches();
let Cli {
base,
dry_run,
force,
verbose,
and_rebase,
gen_completions,
whole_file,
one_fixup_per_commit,
} = Cli::parse();

if let Some(shell) = args.get_one::<String>("gen-completions") {
if let Some(shell) = gen_completions {
let app_name = "git-absorb";
let mut args_clone = Cli::command();
match shell.as_str() {
"bash" => {
generate(Shell::Bash, &mut args_clone, app_name, &mut io::stdout());
Expand Down Expand Up @@ -106,28 +85,28 @@ fn main() {
let drain = slog_async::Async::new(drain).build().fuse();
let drain = slog::LevelFilter::new(
drain,
if args.get_flag("verbose") {
if verbose {
slog::Level::Debug
} else {
slog::Level::Info
},
)
.fuse();
let mut logger = slog::Logger::root(drain, o!());
if args.get_flag("verbose") {
if verbose {
logger = logger.new(o!(
"module" => slog::FnValue(|record| record.module()),
"line" => slog::FnValue(|record| record.line()),
"module" => slog::FnValue(|record| record.module()),
"line" => slog::FnValue(|record| record.line()),
));
}

if let Err(e) = git_absorb::run(&mut git_absorb::Config {
dry_run: args.get_flag("dry-run"),
force: args.get_flag("force"),
base: args.get_one::<String>("base").map(|s| s.as_str()),
and_rebase: args.get_flag("and-rebase"),
whole_file: args.get_flag("whole-file"),
one_fixup_per_commit: args.get_flag("one-fixup-per-commit"),
dry_run,
force,
base: base.as_ref().map(|s| s.as_str()),
and_rebase,
whole_file,
one_fixup_per_commit,
logger: &logger,
}) {
crit!(logger, "absorb failed"; "err" => e.to_string());
Expand Down

0 comments on commit dd28e6f

Please sign in to comment.