diff --git a/dbterd/adapters/base.py b/dbterd/adapters/base.py index d7f073f..766694a 100644 --- a/dbterd/adapters/base.py +++ b/dbterd/adapters/base.py @@ -179,8 +179,9 @@ def __save_result(self, path, data): click.FileError: Can not save the file """ try: - with open(f"{path}/{data[0]}", "w") as f: - logger.info(path) + file_path = f"{path}/{data[0]}" + with open(file_path, "w") as f: + logger.info(f"Output saved to {file_path}") f.write(data[1]) except Exception as e: logger.error(str(e)) diff --git a/dbterd/adapters/targets/d2/d2_test_relationship.py b/dbterd/adapters/targets/d2/d2_test_relationship.py index ecfcce4..c918f3a 100644 --- a/dbterd/adapters/targets/d2/d2_test_relationship.py +++ b/dbterd/adapters/targets/d2/d2_test_relationship.py @@ -14,7 +14,8 @@ def run(manifest: Manifest, catalog: Catalog, **kwargs) -> Tuple[str, str]: Returns: Tuple(str, str): File name and the D2 content """ - return ("output.d2", parse(manifest, catalog, **kwargs)) + output_file_name = kwargs.get("output_file_name", "output.d2") + return (output_file_name, parse(manifest, catalog, **kwargs)) def parse(manifest: Manifest, catalog: Catalog, **kwargs) -> str: diff --git a/dbterd/adapters/targets/dbml/dbml_test_relationship.py b/dbterd/adapters/targets/dbml/dbml_test_relationship.py index 6ac5b63..2acdf3c 100644 --- a/dbterd/adapters/targets/dbml/dbml_test_relationship.py +++ b/dbterd/adapters/targets/dbml/dbml_test_relationship.py @@ -15,7 +15,8 @@ def run(manifest: Manifest, catalog: Catalog, **kwargs) -> Tuple[str, str]: Returns: Tuple(str, str): File name and the DBML content """ - return ("output.dbml", parse(manifest, catalog, **kwargs)) + output_file_name = kwargs.get("output_file_name", "output.dbml") + return (output_file_name, parse(manifest, catalog, **kwargs)) def parse(manifest: Manifest, catalog: Catalog, **kwargs) -> str: diff --git a/dbterd/adapters/targets/graphviz/graphviz_test_relationship.py b/dbterd/adapters/targets/graphviz/graphviz_test_relationship.py index 2a2afd6..d3aa458 100644 --- a/dbterd/adapters/targets/graphviz/graphviz_test_relationship.py +++ b/dbterd/adapters/targets/graphviz/graphviz_test_relationship.py @@ -14,7 +14,8 @@ def run(manifest: Manifest, catalog: Catalog, **kwargs) -> Tuple[str, str]: Returns: Tuple(str, str): File name and the GraphViz content """ - return ("output.graphviz", parse(manifest, catalog, **kwargs)) + output_file_name = kwargs.get("output_file_name", "output.graphviz") + return (output_file_name, parse(manifest, catalog, **kwargs)) def parse(manifest: Manifest, catalog: Catalog, **kwargs) -> str: diff --git a/dbterd/adapters/targets/mermaid/mermaid_test_relationship.py b/dbterd/adapters/targets/mermaid/mermaid_test_relationship.py index ca2cf28..a38609d 100644 --- a/dbterd/adapters/targets/mermaid/mermaid_test_relationship.py +++ b/dbterd/adapters/targets/mermaid/mermaid_test_relationship.py @@ -15,7 +15,8 @@ def run(manifest: Manifest, catalog: Catalog, **kwargs) -> Tuple[str, str]: Returns: Tuple(str, str): File name and the Mermaid content """ - return ("output.md", parse(manifest, catalog, **kwargs)) + output_file_name = kwargs.get("output_file_name", "output.md") + return (output_file_name, parse(manifest, catalog, **kwargs)) def replace_column_name(column_name: str) -> str: diff --git a/dbterd/adapters/targets/plantuml/plantuml_test_relationship.py b/dbterd/adapters/targets/plantuml/plantuml_test_relationship.py index 60f582e..ccd6289 100644 --- a/dbterd/adapters/targets/plantuml/plantuml_test_relationship.py +++ b/dbterd/adapters/targets/plantuml/plantuml_test_relationship.py @@ -14,7 +14,8 @@ def run(manifest: Manifest, catalog: Catalog, **kwargs) -> Tuple[str, str]: Returns: Tuple(str, str): File name and the PlantUML content """ - return ("output.plantuml", parse(manifest, catalog, **kwargs)) + output_file_name = kwargs.get("output_file_name", "output.plantuml") + return (output_file_name, parse(manifest, catalog, **kwargs)) def parse(manifest: Manifest, catalog: Catalog, **kwargs) -> str: diff --git a/dbterd/cli/params.py b/dbterd/cli/params.py index 15f58c4..eb3828a 100644 --- a/dbterd/cli/params.py +++ b/dbterd/cli/params.py @@ -63,6 +63,14 @@ def common_params(func): show_default=True, type=click.STRING, ) + @click.option( + "--output-file-name", + "-ofn", + help="Output the result file name. Default is defined in the target module", + default=None, + show_default=True, + type=click.STRING, + ) @click.option( "--omit-columns", help="Flag to omit columns in diagram. Currently only mermaid is supported", diff --git a/docs/nav/guide/cli-references.md b/docs/nav/guide/cli-references.md index 212c277..e2dc64f 100644 --- a/docs/nav/guide/cli-references.md +++ b/docs/nav/guide/cli-references.md @@ -173,6 +173,23 @@ Configure the path to directory containing the output diagram file. dbterd run --output "./target" ``` +### dbterd run --output-file-name (-ofn) + +Configure the output file name +> Default to `output.{modulename}` which is defined in the target module + +**Examples:** +=== "CLI" + + ```bash + dbterd run -ofn "erd.dbml" + ``` +=== "CLI (long style)" + + ```bash + dbterd run --output-file-name "erd.dbml" + ``` + ### dbterd run --target (-t) > Default to `dbml` diff --git a/tests/unit/cli/test_runner.py b/tests/unit/cli/test_runner.py index a89e891..d80cc6c 100644 --- a/tests/unit/cli/test_runner.py +++ b/tests/unit/cli/test_runner.py @@ -72,11 +72,11 @@ def test_invoke_run_with_invalid_strategy(self, dbterd: dbterdRunner) -> None: @pytest.mark.parametrize( "target, output", [ - ("dbml", "output.dbml"), - ("mermaid", "output.md"), - ("plantuml", "output.plantuml"), - ("graphviz", "output.graphviz"), - ("d2", "output.d2"), + ("dbml", None), + ("mermaid", None), + ("plantuml", None), + ("graphviz", None), + ("d2", None), ], ) def test_invoke_run_ok(self, target, output, dbterd: dbterdRunner) -> None: