Skip to content

Commit

Permalink
fix wizards
Browse files Browse the repository at this point in the history
  • Loading branch information
andreas-stuerz committed Apr 24, 2024
1 parent 00679d5 commit 32d3bd3
Show file tree
Hide file tree
Showing 11 changed files with 7,499 additions and 302 deletions.
52 changes: 18 additions & 34 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -336,55 +336,39 @@ Commands:
command Generate code for a new command
```
#### API code core
This generates all necessary code to implement api calls to a core module.
#### API code generator
This generates all necessary code to implement api calls to a module.
For a list of all plugin api endpoints see:
https://github.com/opnsense/docs/tree/master/source/development/api/core
Or:
See a list of all compatible modules:
```
# core modules
$ opn-cli new api list --module-type core
```
Example that generates code for the core module 'cron':
```
$ opn-cli new api core cron
```
This generates a class for every controller of the core module.
Every class contains methodes to call all api endpoints of the corresponding controller.
Please move the file from the output dir to the destination folders under opnsense_cli/.
The default output path is opnsense_cli/output/api/core/ .

#### API code plugin
This generates all necessary code to implement api calls to a plugin module.
For a list of all plugin api endpoints see:
https://github.com/opnsense/docs/tree/master/source/development/api/plugins
Or:
# plugin modules
$ opn-cli new api list --module-type plugins

```
$ opn-cli new api list --module-type plugin
```
Example that generates code for the plugin module 'haproxy':
Examples:
```
# generate all api code for the module core cron
$ opn-cli new api core cron

# generate all api code for the module plugins haproxy
$ opn-cli new api plugin haproxy

# generate all api code for the module plugins unbound
$ opn-cli new api plugin unbound

```
This generates a class for every controller of the plugin module.
Every class contains methodes to call all api endpoints of the corresponding controller.
This generates a class for every controller of the core module.
Every class contains methods to call all api endpoints of the corresponding controller.
Please move the file from the output dir to the destination folders under opnsense_cli/.
The default output path is opnsense_cli/output/api/plugins/ .
The default output path is opnsense_cli/output/api/[core|plugins]/ .
#### Core command code
This generates all necessary code and tests to implement a new command from a core module.
Expand Down
60 changes: 30 additions & 30 deletions opnsense_cli/commands/new/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
import os
from opnsense_cli.commands.new import new
from opnsense_cli.code_generators.opnsense_api.codegenerator import OpnsenseApiCodeGenerator
from opnsense_cli.parser.html_parser import HtmlParser
from opnsense_cli.parser.opnsense_api_reference_parser import OpnsenseApiReferenceParser
from opnsense_cli.parser.opnsense_module_list_parser import OpnsenseModuleListParser
from opnsense_cli.parser.rst_parser import RstParser
from opnsense_cli.template_engines.jinja2 import Jinja2TemplateEngine


Expand All @@ -15,13 +17,13 @@ def api(**kwargs):


@api.command()
@click.argument("plugin_module_name", required=True)
@click.argument("module_name", required=True)
@click.option(
"--api-reference-url",
"-aru",
help=("The url to the api reference in the official Opnsense documentation"),
show_default=True,
default="https://github.com/opnsense/docs/tree/master/source/development/api/plugins",
default="https://raw.githubusercontent.com/opnsense/docs/master/source/development/api/plugins",
required=True,
)
@click.option(
Expand Down Expand Up @@ -49,10 +51,6 @@ def plugin(**kwargs):
"""
Generate new api code for a plugin module.
List all plugin module names:
$ opn-cli new api list --module-type plugin
Example:
Generate api code for the 'haproxy' plugin module
Expand All @@ -61,17 +59,17 @@ def plugin(**kwargs):
Default output path is opn-cli/opnsense_cli/output/api/plugins/
"""
generate_api_files(kwargs["plugin_module_name"], **kwargs)
generate_api_files(**kwargs)


@api.command()
@click.argument("core_module_name", required=True)
@click.argument("module_name", required=True)
@click.option(
"--api-reference-url",
"-aru",
help=("The url to the core module api reference in the official Opnsense documentation."),
show_default=True,
default="https://github.com/opnsense/docs/tree/master/source/development/api/core",
default="https://raw.githubusercontent.com/opnsense/docs/master/source/development/api/core",
required=True,
)
@click.option(
Expand Down Expand Up @@ -111,7 +109,7 @@ def core(**kwargs):
Default output path is opn-cli/opnsense_cli/output/api/core/
"""
generate_api_files(kwargs["core_module_name"], **kwargs)
generate_api_files(**kwargs)


@api.command()
Expand All @@ -120,14 +118,14 @@ def core(**kwargs):
"-bu",
help=("The url to the api reference in the official Opnsense documentation."),
show_default=True,
default="https://github.com/opnsense/docs/tree/master/source/development/api/",
default="https://github.com/opnsense/docs/tree/master/source/development/api",
required=True,
)
@click.option(
"--module-type",
"-mt",
help=("The url to the api reference in the official Opnsense documentation."),
type=click.Choice(["core", "plugin"], case_sensitive=True),
help=("What type of modules to list. core or plugins"),
type=click.Choice(["core", "plugins"], case_sensitive=True),
required=True,
)
def list(**kwargs):
Expand All @@ -139,18 +137,27 @@ def list(**kwargs):
$ opn-cli new api list --module-type plugin
"""
list_modules(kwargs["module_type"], kwargs["base_url"])

list_modules(f"{kwargs['base_url']}/{kwargs['module_type']}")

def generate_api_files(module_name, **kwargs):
controller_parser = OpnsenseApiReferenceParser(
kwargs["api_reference_url"],
"table",
module_name,
)
controller_html_tables = controller_parser.parse()
def list_modules(url):
github_html_parser = HtmlParser(url, 'script[type="application/json"][data-target="react-app.embeddedData"]', False)
parsed_github_html_tag = github_html_parser.parse()
module_list_parser = OpnsenseModuleListParser(parsed_github_html_tag)
module_list = module_list_parser.parse()

for module in module_list:
click.echo(module)

def generate_api_files(**kwargs):
rst_url = f"{kwargs['api_reference_url']}/{kwargs['module_name']}.rst"
rst_parser = RstParser(rst_url, 'table')
api_reference_tables = rst_parser.parse()

api_reference_parser = OpnsenseApiReferenceParser(api_reference_tables)
controller_html_tables = api_reference_parser.parse()
template_engine = Jinja2TemplateEngine(kwargs["api_template_basedir"])
write_api(template_engine, controller_html_tables, module_name, **kwargs)
write_api(template_engine, controller_html_tables, kwargs['plugin_module_name'], **kwargs)


def write_api(template_engine, controllers, module_name, **kwargs):
Expand All @@ -162,11 +169,4 @@ def write_api(template_engine, controllers, module_name, **kwargs):
click.echo(api_code_generator.write_code(output_path))


def list_modules(type, url):
if type == "core":
url += "core"
elif type == "plugin":
url += "plugins"
module_list = OpnsenseModuleListParser(url).module_list
for module in module_list:
click.echo(module)

14 changes: 7 additions & 7 deletions opnsense_cli/commands/new/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def core(**kwargs):

@command.command()
@click.argument("click_group")
@click.argument("opn_cli")
@click.argument("click_group")
@click.option(
"--model-url",
"-m",
Expand Down Expand Up @@ -225,7 +225,7 @@ def write_command(type, model_tag: Tag, template_engine, option_factory, **kwarg
option_factory,
kwargs["template_command"],
kwargs["click_group"],
kwargs["opn_cli"],
kwargs["click_group"],
kwargs["tag"],
type,
)
Expand All @@ -234,7 +234,7 @@ def write_command(type, model_tag: Tag, template_engine, option_factory, **kwarg
form_parser = OpnsenseFormParser(kwargs["form_url"], "form")
command_code_generator.help_messages = form_parser.parse()

output_path = f"{kwargs['command_output_dir']}/{kwargs['click_group']}/{kwargs['opn_cli']}.py"
output_path = f"{kwargs['command_output_dir']}/{kwargs['click_group']}/{kwargs['click_group']}.py"

click.echo(command_code_generator.write_code(output_path))

Expand All @@ -246,11 +246,11 @@ def write_command_service(type, model_tag: Tag, template_engine, option_factory,
option_factory,
kwargs["template_service"],
kwargs["click_group"],
kwargs["opn_cli"],
kwargs["click_group"],
kwargs["tag"],
type,
)
output_path = f"{kwargs['command_output_dir']}/{kwargs['click_group']}/services/{kwargs['click_group']}_{kwargs['opn_cli']}_service.py" # noqa: E501
output_path = f"{kwargs['command_output_dir']}/{kwargs['click_group']}/services/{kwargs['click_group']}_{kwargs['click_group']}_service.py" # noqa: E501

click.echo(command_service_generator.write_code(output_path))

Expand All @@ -262,13 +262,13 @@ def write_command_test(type, model_tag: Tag, template_engine, option_factory, **
option_factory,
kwargs["template_test"],
kwargs["click_group"],
kwargs["opn_cli"],
kwargs["click_group"],
kwargs["tag"],
type,
)

output_path = (
f"{kwargs['command_output_dir']}/{kwargs['click_group']}/tests/test_{kwargs['click_group']}_{kwargs['opn_cli']}.py"
f"{kwargs['command_output_dir']}/{kwargs['click_group']}/tests/test_{kwargs['click_group']}_{kwargs['click_group']}.py"
)

click.echo(command_test_generator.write_code(output_path))
Expand Down
Loading

0 comments on commit 32d3bd3

Please sign in to comment.