Skip to content

Commit 2726b17

Browse files
committed
feat!: Use pretty output for exists if raw-option is not set
To archive the old beaviour add the --output-raw option to your cli call
1 parent ff0ebcb commit 2726b17

File tree

3 files changed

+32
-8
lines changed

3 files changed

+32
-8
lines changed

tests/test_cmd_dimension.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,16 @@ def test_dimension_list(mocker, command):
1616
assert result.stdout == "Dimension1\nDimension2\nDimension3\n"
1717

1818

19-
def test_dimension_exists(mocker):
19+
@pytest.mark.parametrize(
20+
"raw_option,expected_output",
21+
[(None, "✅ Dimension exists!\n"), ("--output-raw", "True\n")],
22+
)
23+
def test_dimension_exists(mocker, raw_option, expected_output):
2024
mocker.patch("tm1cli.utils.generic.TM1Service", MockedTM1Service)
21-
result = runner.invoke(app, ["dimension", "exists", "Dimension1"])
25+
if raw_option:
26+
result = runner.invoke(app, [raw_option, "dimension", "exists", "Dimension1"])
27+
else:
28+
result = runner.invoke(app, ["dimension", "exists", "Dimension1"])
2229
assert result.exit_code == 0
2330
assert isinstance(result.stdout, str)
24-
assert result.stdout == "True\n"
31+
assert result.stdout == expected_output

tm1cli/main.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import os
1+
import importlib.metadata
22
import json
3+
import os
34

4-
import importlib.metadata
55
import typer
66
import yaml
77
from rich import print
@@ -14,7 +14,6 @@
1414
from tm1cli.utils.cli_param import DATABASE_OPTION
1515
from tm1cli.utils.various import resolve_database
1616

17-
1817
console = Console()
1918
app = typer.Typer()
2019

@@ -30,7 +29,13 @@
3029

3130

3231
@app.callback()
33-
def main(ctx: typer.Context):
32+
def main(
33+
ctx: typer.Context,
34+
raw: Annotated[
35+
bool,
36+
typer.Option("--output-raw", help="Raw output without formatting"),
37+
] = False,
38+
):
3439
"""
3540
CLI tool to interact with TM1 using TM1py.
3641
"""
@@ -52,6 +57,8 @@ def main(ctx: typer.Context):
5257
}
5358
ctx.obj = {"configs": {}, "default_db_config": default_db_config}
5459

60+
ctx.obj["raw"] = raw
61+
5562

5663
@app.command()
5764
def version():

tm1cli/utils/generic.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,17 @@ def execute_exists(attribute_name, ctx, database, **args):
1515

1616
attribute = getattr(tm1, attribute_name)
1717
method = getattr(attribute, "exists")
18-
print(method(**args))
18+
output = method(**args)
19+
if ctx.obj["raw"]:
20+
print(output)
21+
return
22+
23+
output_name = f"{attribute_name[:-1].capitalize()}"
24+
if output:
25+
print(f":white_check_mark: {output_name} exists!")
26+
else:
27+
print(f":x: {output_name} does not exist!")
28+
1929
except Exception as e:
2030
print(f"[bold red]{type(e).__name__}:[/bold red] {e}")
2131
raise typer.Exit(code=1)

0 commit comments

Comments
 (0)