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

feat: add check functionality to spec generator #1

Merged
merged 1 commit into from
Nov 14, 2023
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
8 changes: 8 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,11 @@ jobs:
channel: nightly
components: rustfmt
- run: make format

spech-check:
name: OpenAPI Spec Check
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: moonrepo/setup-rust@v1
- run: make spec-check
7 changes: 7 additions & 0 deletions Cargo.lock

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

6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,9 @@ format:

lint:
cargo clippy --all-features --workspace -- -D warnings

spec:
cargo run -p cs2kz-api-spec-generator -- json

spec-check:
cargo run -p cs2kz-api-spec-generator -- check
12 changes: 0 additions & 12 deletions api-spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -1696,18 +1696,6 @@
"MissingFilter"
]
},
{
"type": "string",
"enum": [
"MissingApiKey"
]
},
{
"type": "string",
"enum": [
"InvalidApiKey"
]
},
{
"type": "string",
"enum": [
Expand Down
3 changes: 3 additions & 0 deletions cs2kz-api-spec-generator/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@ edition = "2021"
color-eyre = { workspace = true }
clap = { workspace = true }
cs2kz-api = { path = "../cs2kz-api" }

[dependencies.similar]
version = "2.3.0"
31 changes: 30 additions & 1 deletion cs2kz-api-spec-generator/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use {
clap::{Parser, Subcommand},
color_eyre::{eyre::Context, Result},
cs2kz_api::API,
similar::TextDiff,
std::path::PathBuf,
};

Expand All @@ -13,8 +14,22 @@ struct Args {

#[derive(Subcommand)]
enum Output {
/// Do not write the output anywhere, just check against the existing output.
Check {
/// The path to the existing spec file.
#[clap(default_value = "./api-spec.json")]
path: PathBuf,
},

/// Write the generated spec to STDOUT.
Stdout,
Json { path: PathBuf },

/// Write the generated spec to a JSON file.
Json {
/// The path to the target spec file.
#[clap(default_value = "./api-spec.json")]
path: PathBuf,
},
}

fn main() -> Result<()> {
Expand All @@ -24,6 +39,20 @@ fn main() -> Result<()> {
let json = API::json()?;

match args.output {
Output::Check { path } => {
let check = std::fs::read_to_string(path).context("Failed to read spec file.")?;
let diff = TextDiff::from_lines(&check, &json);
let mut error = false;

for hunk in diff.unified_diff().iter_hunks() {
error = true;
eprintln!("{hunk}");
}

if error {
std::process::exit(1);
}
}
Output::Stdout => println!("{json}"),
Output::Json { path } => {
std::fs::write(path, json.into_bytes()).context("Failed to write JSON to disk.")?;
Expand Down