Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(cli): Switch to clap_derive #77

Merged
merged 1 commit into from
Aug 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 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 @@ -15,7 +15,7 @@ categories = ["command-line-utilities", "development-tools::cargo-plugins"]
[dependencies]
trustfall_core = "0.0.4"
rustdoc-types = "0.14.0"
clap = { version = "3.2.8", features = ["cargo"] }
clap = { version = "3.2.8", features = ["derive", "cargo"] }
serde_json = "1.0.82"
anyhow = "1.0.58"
ron = "0.7.1"
Expand Down
148 changes: 48 additions & 100 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@ mod query;
mod templating;
mod util;

use std::env;
use std::path::PathBuf;

use clap::{crate_version, value_parser, Arg, ArgAction, Command};
use clap::{Args, Parser, Subcommand};
use termcolor::{ColorChoice, StandardStream};

use crate::{
Expand Down Expand Up @@ -52,117 +51,66 @@ impl GlobalConfig {
}

fn main() -> anyhow::Result<()> {
let matches = cmd().get_matches();

// Descend one level: from `cargo semver-checks` to just `semver-checks`.
let semver_check = matches
.subcommand_matches("semver-checks")
.expect("semver-checks is missing");
let Cargo::SemverChecks(args) = Cargo::parse();

let config = GlobalConfig::new();

match semver_check.subcommand() {
Some(("diff-files", diff_files)) => {
let current_rustdoc_path = diff_files
.get_one::<PathBuf>("current_rustdoc_path")
.expect("current_rustdoc_path is required but was not present")
.as_path();
let baseline_rustdoc_path = diff_files
.get_one::<PathBuf>("baseline_rustdoc_path")
.expect("baseline_rustdoc_path is required but was not present")
.as_path();

let current_crate = load_rustdoc_from_file(current_rustdoc_path)?;
let baseline_crate = load_rustdoc_from_file(baseline_rustdoc_path)?;
match args {
SemverChecks::DiffFiles(args) => {
let current_crate = load_rustdoc_from_file(&args.current_rustdoc_path)?;
let baseline_crate = load_rustdoc_from_file(&args.baseline_rustdoc_path)?;

return run_check_release(config, current_crate, baseline_crate);
}
Some(("check-release", check_release)) => {
let current_rustdoc_path = check_release
.get_one::<PathBuf>("current_rustdoc_path")
.expect("current_rustdoc_path is required but was not present")
.as_path();
let baseline_rustdoc_path = check_release
.get_one::<PathBuf>("baseline_rustdoc_path")
.expect("baseline_rustdoc_path is required but was not present")
.as_path();

let current_crate = load_rustdoc_from_file(current_rustdoc_path)?;
let baseline_crate = load_rustdoc_from_file(baseline_rustdoc_path)?;
SemverChecks::CheckRelease(args) => {
let current_crate = load_rustdoc_from_file(&args.current_rustdoc_path)?;
let baseline_crate = load_rustdoc_from_file(&args.baseline_rustdoc_path)?;

return run_check_release(config, current_crate, baseline_crate);
}
Some(_) => {
unreachable!("external subcommands were not enabled with clap")
}
None => {
unreachable!("arg_required_else_help is set with clap")
}
}
}

fn cmd() -> Command<'static> {
Command::new("cargo-semver-checks")
.bin_name("cargo")
.version(crate_version!())
.propagate_version(true)
.subcommand(
Command::new("semver-checks")
.about("Check your crate for semver violations.")
.subcommand_required(true)
.arg_required_else_help(true)
.subcommand(
Command::new("diff-files")
.arg_required_else_help(true)
.arg(
Arg::new("current_rustdoc_path")
.short('c')
.long("current")
.value_name("CURRENT_RUSTDOC_JSON")
.help("The current rustdoc json output to test for semver violations. Required.")
.action(ArgAction::Set)
.value_parser(value_parser!(PathBuf))
.required(true)
)
.arg(
Arg::new("baseline_rustdoc_path")
.short('b')
.long("baseline")
.value_name("BASELINE_RUSTDOC_JSON")
.help("The rustdoc json file to use as a semver baseline. Required.")
.action(ArgAction::Set)
.value_parser(value_parser!(PathBuf))
.required(true)
)
)
.subcommand(
Command::new("check-release")
.arg_required_else_help(true)
.arg(
Arg::new("current_rustdoc_path")
.short('c')
.long("current")
.value_name("CURRENT_RUSTDOC_JSON")
.help("The current rustdoc json output to test for semver violations. Required.")
.action(ArgAction::Set)
.value_parser(value_parser!(PathBuf))
.required(true)
)
.arg(
Arg::new("baseline_rustdoc_path")
.short('b')
.long("baseline")
.value_name("BASELINE_RUSTDOC_JSON")
.help("The rustdoc json file to use as a semver baseline. Required.")
.action(ArgAction::Set)
.value_parser(value_parser!(PathBuf))
.required(true)
)
)
)
#[derive(Parser)]
#[clap(name = "cargo")]
#[clap(bin_name = "cargo")]
#[clap(version, propagate_version = true)]
enum Cargo {
#[clap(subcommand)]
SemverChecks(SemverChecks),
}

/// Check your crate for semver violations.
#[derive(Subcommand)]
enum SemverChecks {
DiffFiles(DiffFiles),
CheckRelease(CheckRelease),
}

#[derive(Args)]
struct DiffFiles {
/// The current rustdoc json output to test for semver violations. Required.
#[clap(short, long = "current", value_name = "CURRENT_RUSTDOC_JSON")]
current_rustdoc_path: PathBuf,

/// The rustdoc json file to use as a semver baseline. Required.
#[clap(short, long = "baseline", value_name = "BASELINE_RUSTDOC_JSON")]
baseline_rustdoc_path: PathBuf,
}

#[derive(Args)]
struct CheckRelease {
/// The current rustdoc json output to test for semver violations. Required.
#[clap(short, long = "current", value_name = "CURRENT_RUSTDOC_JSON")]
current_rustdoc_path: PathBuf,

/// The rustdoc json file to use as a semver baseline. Required.
#[clap(short, long = "baseline", value_name = "BASELINE_RUSTDOC_JSON")]
baseline_rustdoc_path: PathBuf,
Comment on lines +90 to +109
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since these two subcommands are identical (though poorly documented as such), is there a way to have them use an identical struct instead of two different ones?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#78 is the follow up to this comment

}

#[test]
fn verify_cmd() {
cmd().debug_assert();
fn verify_cli() {
use clap::CommandFactory;
Cargo::command().debug_assert()
}