From 3b231c5fea2ef80e87fcb05ca7a0cc96b33735e3 Mon Sep 17 00:00:00 2001 From: Tom Kralidis Date: Tue, 21 Jun 2022 21:03:19 -0400 Subject: [PATCH] add OpenAPI generate option to write to file (#919) (#925) --- docker/entrypoint.sh | 2 +- docs/source/administration.rst | 6 ++++++ pygeoapi/openapi.py | 14 +++++++++++--- tests/cite/ogcapi-features/README.md | 2 +- 4 files changed, 19 insertions(+), 5 deletions(-) diff --git a/docker/entrypoint.sh b/docker/entrypoint.sh index f1c4fa5..6f0289b 100755 --- a/docker/entrypoint.sh +++ b/docker/entrypoint.sh @@ -60,7 +60,7 @@ function error() { cd ${PYGEOAPI_HOME} echo "Trying to generate openapi.yml" -pygeoapi openapi generate ${PYGEOAPI_CONFIG} > ${PYGEOAPI_OPENAPI} +pygeoapi openapi generate ${PYGEOAPI_CONFIG} --output-file ${PYGEOAPI_OPENAPI} [[ $? -ne 0 ]] && error "openapi.yml could not be generated ERROR" diff --git a/docs/source/administration.rst b/docs/source/administration.rst index 0089eae..a671e1d 100644 --- a/docs/source/administration.rst +++ b/docs/source/administration.rst @@ -28,6 +28,12 @@ This will dump the OpenAPI document as YAML to your system's ``stdout``. To sav pygeoapi openapi generate /path/to/my-pygeoapi-config.yml > /path/to/my-pygeoapi-openapi.yml +You can also write to a file explicitly via the ``--output-file`` option:: + +.. code-block:: bash + + pygeoapi openapi generate /path/to/my-pygeoapi-config.yml --output-file /path/to/my-pygeoapi-openapi.yml + To generate the OpenAPI document as JSON, run: .. code-block:: bash diff --git a/pygeoapi/openapi.py b/pygeoapi/openapi.py index 4b0bd15..5879217 100644 --- a/pygeoapi/openapi.py +++ b/pygeoapi/openapi.py @@ -1076,7 +1076,9 @@ def openapi(): @click.argument('config_file', type=click.File()) @click.option('--format', '-f', 'format_', type=click.Choice(['json', 'yaml']), default='yaml', help='output format (json|yaml)') -def generate(ctx, config_file, format_='yaml'): +@click.option('--output-file', '-of', type=click.File('w', encoding='utf-8'), + help='Name of output file') +def generate(ctx, config_file, output_file, format_='yaml'): """Generate OpenAPI Document""" if config_file is None: @@ -1084,10 +1086,16 @@ def generate(ctx, config_file, format_='yaml'): s = yaml_load(config_file) pretty_print = s['server'].get('pretty_print', False) + if format_ == 'yaml': - click.echo(yaml.safe_dump(get_oas(s), default_flow_style=False)) + content = yaml.safe_dump(get_oas(s), default_flow_style=False) else: - click.echo(to_json(get_oas(s), pretty=pretty_print)) + content = to_json(get_oas(s), pretty=pretty_print) + + if output_file is None: + click.echo(content) + else: + output_file.write(content) @click.command() diff --git a/tests/cite/ogcapi-features/README.md b/tests/cite/ogcapi-features/README.md index 90d4671..9697aed 100644 --- a/tests/cite/ogcapi-features/README.md +++ b/tests/cite/ogcapi-features/README.md @@ -14,6 +14,6 @@ pip install gunicorn cd tests/cite/ogcapi-features . cite.env python ../../load_es_data.py ./canada-hydat-daily-mean-02hc003.geojson IDENTIFIER -pygeoapi openapi generate $PYGEOAPI_CONFIG > $PYGEOAPI_OPENAPI +pygeoapi openapi generate $PYGEOAPI_CONFIG --output-file $PYGEOAPI_OPENAPI gunicorn pygeoapi.flask_app:APP -b 0.0.0.0:5001 --access-logfile '-' ```