Skip to content

Commit

Permalink
feat: add shell completion and manpage support
Browse files Browse the repository at this point in the history
Signed-off-by: Rui Chen <rui@chenrui.dev>
  • Loading branch information
chenrui333 committed Dec 28, 2024
1 parent 6573ff1 commit e41a9d9
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[submodule "src/schemastore"]
path = src/schemastore
url = https://github.com/SchemaStore/schemastore
url = https://github.com/SchemaStore/schemastore.git
39 changes: 33 additions & 6 deletions Cargo.lock

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

8 changes: 7 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ authors = ["Matt Palmer <matt@hezmatt.org>"]
edition = "2021"
# If this is changed, .github/workflows/qa.yml build matrix needs updating as well
rust-version = "1.60.0"
build = "build.rs"

[lib]
crate-type = ["cdylib", "rlib"]
Expand All @@ -23,7 +24,7 @@ crate-type = ["cdylib", "rlib"]
js = ["console_error_panic_hook", "valico/js"]

[dependencies]
clap = { version = "4.0", features = ["derive"] }
clap = { version = "4.5.23", features = ["derive"] }
glob = "0.3"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
Expand All @@ -41,6 +42,11 @@ console_error_panic_hook = { version = "0.1.6", optional = true }

serde-wasm-bindgen = "0.4.5"

[build-dependencies]
clap = { version = "4.5", features = ["derive"] }
clap_complete = "4.2"
clap_mangen = "0.2"

[dev-dependencies]
wasm-bindgen-test = "0.3.13"

Expand Down
40 changes: 40 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use std::{fs, io::Result, path::PathBuf};

use clap::{CommandFactory, ValueEnum};
use clap_complete::{generate_to, Shell};
use clap_mangen::generate_to as generate_manpage;

mod cli {
include!("src/cli.rs");
}

fn main() -> Result<()> {
println!("cargo:rerun-if-changed=src/main.rs");
println!("cargo:rerun-if-changed=src/config.rs");

if let Some(out_path) = std::env::var_os("GEN_DIR").or(std::env::var_os("OUT_DIR")) {
let out_dir = PathBuf::from(out_path);

// Generate manpage
let man_dir = out_dir.join("man");
fs::create_dir_all(&man_dir)?;
let mut cmd = cli::CliConfig::command();
generate_manpage(cmd.clone(), &man_dir)?;

// Generate shell completions
let completions_dir = out_dir.join("completions");
fs::create_dir_all(&completions_dir)?;

for shell in Shell::value_variants() {
generate_to(*shell, &mut cmd, "action-validator", &completions_dir)?;
}
}

println!(
"cargo:rustc-env=TARGET={}",
std::env::var("TARGET").unwrap()
);
println!("cargo:rerun-if-env-changed=GEN_DIR");

Ok(())
}
19 changes: 19 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use clap::Parser;
use std::path::PathBuf;

/// Minimal struct for Clap usage, without Serde
#[derive(Parser, Debug)]
#[command(
name = "action-validator",
about = "A validator for GitHub Action and Workflow YAML files",
version
)]
pub struct CliConfig {
/// Be more verbose
#[arg(short, long)]
pub verbose: bool,

/// Input file(s)
#[arg(name = "path_to_action_yaml")]
pub src: Vec<PathBuf>,
}
5 changes: 3 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mod cli;
mod config;
mod log;
mod schemas;
Expand All @@ -9,7 +10,7 @@ use config::{ActionType, RunConfig};
use validation_error::ValidationError;
use validation_state::ValidationState;

pub use crate::config::CliConfig;
pub use crate::cli::CliConfig;
use crate::schemas::{validate_as_action, validate_as_workflow};

#[cfg(feature = "js")]
Expand Down Expand Up @@ -54,7 +55,7 @@ mod js {
}

#[cfg(not(feature = "js"))]
pub mod cli {
pub mod cli_runtime {
use std::fs;

use crate::{
Expand Down
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ fn main() {
let config = CliConfig::parse();

if matches!(
action_validator::cli::run(&config),
action_validator::cli::RunResult::Failure
action_validator::cli_runtime::run(&config),
action_validator::cli_runtime::RunResult::Failure
) {
process::exit(1);
}
Expand Down
2 changes: 1 addition & 1 deletion src/schemastore
Submodule schemastore updated 2252 files

0 comments on commit e41a9d9

Please sign in to comment.