Skip to content
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

feat: add --output-file-name option #102

Merged
merged 1 commit into from
Mar 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading