-
Notifications
You must be signed in to change notification settings - Fork 618
Hunting - add generate-json command
#4613
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
Changes from all commits
c597277
6e770f6
7c42550
b39cf82
24b5cb4
f537ef9
3ae7742
a117622
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -115,3 +115,7 @@ exports/ | |
| ML-models/ | ||
| surveys/ | ||
| machine-learning/ | ||
|
|
||
|
|
||
| # hunting json output | ||
| hunting/*/json/*.json | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| # Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| # or more contributor license agreements. Licensed under the Elastic License | ||
| # 2.0; you may not use this file except in compliance with the Elastic License | ||
| # 2.0. | ||
|
|
||
| from dataclasses import asdict | ||
| import json | ||
| from pathlib import Path | ||
| import click | ||
| from .definitions import Hunt | ||
| from .utils import load_index_file, load_toml | ||
|
|
||
| class JSONGenerator: | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. there is really no need to create for a class here: the grouping of the logic is achieved by using a separate module (though, it's better to rename to avoid the confusion with default I suggest refactoring this as separate stateless functions |
||
| """Class to generate or update JSON documentation from TOML or YAML files.""" | ||
| def __init__(self, base_path: Path): | ||
| """Initialize with the base path and load the hunting index.""" | ||
| self.base_path = base_path | ||
| self.hunting_index = load_index_file() | ||
|
|
||
| def process_file(self, file_path: Path) -> None: | ||
| """Process a single TOML file and generate its JSON representation.""" | ||
| if not file_path.is_file() or file_path.suffix != '.toml': | ||
| raise ValueError(f"The provided path is not a valid TOML file: {file_path}") | ||
|
|
||
| click.echo(f"Processing specific TOML file: {file_path}") | ||
| hunt_config = load_toml(file_path) | ||
| json_content = self.convert_toml_to_json(hunt_config) | ||
|
|
||
| json_folder = self.create_json_folder(file_path) | ||
| json_path = json_folder / f"{file_path.stem}.json" | ||
| self.save_json(json_path, json_content) | ||
|
|
||
| def process_folder(self, folder: str) -> None: | ||
| """Process all TOML files in a specified folder and generate their JSON representations.""" | ||
| folder_path = self.base_path / folder / "queries" | ||
| json_folder = self.base_path / folder / "docs" | ||
|
|
||
| if not folder_path.is_dir() or not json_folder.is_dir(): | ||
| raise ValueError(f"Queries folder {folder_path} or docs folder {json_folder} does not exist.") | ||
|
|
||
| click.echo(f"Processing all TOML files in folder: {folder_path}") | ||
| toml_files = folder_path.rglob("*.toml") | ||
|
|
||
| for toml_file in toml_files: | ||
| self.process_file(toml_file) | ||
|
|
||
| def process_all_files(self) -> None: | ||
| """Process all TOML files in the base directory and subfolders.""" | ||
| click.echo("Processing all TOML files in the base directory and subfolders.") | ||
| toml_files = self.base_path.rglob("queries/*.toml") | ||
|
|
||
| for toml_file in toml_files: | ||
| self.process_file(toml_file) | ||
|
|
||
| def convert_toml_to_json(self, hunt_config: Hunt) -> dict: | ||
| """Convert a Hunt configuration to JSON format.""" | ||
| return json.dumps(asdict(hunt_config), indent=4) | ||
|
|
||
| def save_json(self, json_path: Path, content: dict) -> None: | ||
| """Save the JSON content to a file.""" | ||
| with open(json_path, 'w', encoding='utf-8') as f: | ||
| json.dump(content, f, indent=2, ensure_ascii=False) | ||
| click.echo(f"JSON generated: {json_path}") | ||
|
|
||
| def create_json_folder(self, file_path: Path) -> Path: | ||
| """Create the docs folder if it doesn't exist and return the path.""" | ||
| json_folder = file_path.parent.parent / "json" | ||
| json_folder.mkdir(parents=True, exist_ok=True) | ||
| return json_folder | ||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
would ensure the argument
pathhas the correct type, so there will be no need for forced conversion below: