Skip to content

Commit

Permalink
feat: add --output-file-name option (#102)
Browse files Browse the repository at this point in the history
  • Loading branch information
datnguye authored Mar 30, 2024
1 parent 19ecef7 commit ae36f30
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 12 deletions.
5 changes: 3 additions & 2 deletions dbterd/adapters/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
3 changes: 2 additions & 1 deletion dbterd/adapters/targets/d2/d2_test_relationship.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
3 changes: 2 additions & 1 deletion dbterd/adapters/targets/dbml/dbml_test_relationship.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
3 changes: 2 additions & 1 deletion dbterd/adapters/targets/mermaid/mermaid_test_relationship.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
8 changes: 8 additions & 0 deletions dbterd/cli/params.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
17 changes: 17 additions & 0 deletions docs/nav/guide/cli-references.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
10 changes: 5 additions & 5 deletions tests/unit/cli/test_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit ae36f30

Please sign in to comment.