Skip to content

Commit

Permalink
Fix json output (#221)
Browse files Browse the repository at this point in the history
* Fix json output

* Update devbox lock

* Bump version

* Fixing json output
  • Loading branch information
mbovo authored Nov 29, 2024
1 parent 255594c commit a240aa6
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 11 deletions.
9 changes: 8 additions & 1 deletion devbox.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,12 @@
"[email protected]",
"[email protected]",
"statix@latest"
]
],
"shell":{
"init_hook": [
"pre-commit install",
"source $VENV_DIR/bin/activate && poetry install",
"echo 'source $VENV_DIR/bin/activate' to activate the virtual environment"
]
}
}
2 changes: 1 addition & 1 deletion devbox.lock
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@
},
"[email protected]": {
"last_modified": "2024-06-12T20:55:33Z",
"plugin_version": "0.0.3",
"plugin_version": "0.0.4",
"resolved": "github:NixOS/nixpkgs/a9858885e197f984d92d7fe64e9fff6b2e488d40#python3",
"source": "devbox-search",
"version": "3.11.9",
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ ignore = [

[tool.poetry]
name = "pdh"
version = "0.6.0"
version = "0.6.1"
description = "Pagerduty CLI for Humans"
authors = ["Manuel Bovo <[email protected]>"]
license = "GPL-3.0-or-later"
Expand Down
24 changes: 17 additions & 7 deletions src/pdh/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ class Output(object):
def plain(self, **kwargs) -> None:
items = kwargs["items"] if "items" in kwargs else []
plain_print_f = kwargs["print_f"] if "print_f" in kwargs else None
console = kwargs["console"] if "console" in kwargs else Console()
console = kwargs["console"] if "console" in kwargs else None
if console is None:
console = Console()
for i in items:
if plain_print_f:
plain_print_f(i)
Expand All @@ -36,22 +38,30 @@ def plain(self, **kwargs) -> None:

def raw(self, **kwargs) -> None:
items = kwargs["items"] if "items" in kwargs else []
console = kwargs["console"] if "console" in kwargs else Console()
console = kwargs["console"] if "console" in kwargs else None
if console is None:
console = Console()
console.print(items)

def yaml(self, **kwargs) -> None:
items = kwargs["items"] if "items" in kwargs else []
console = kwargs["console"] if "console" in kwargs else Console()
console = kwargs["console"] if "console" in kwargs else None
if console is None:
console = Console()
console.print(yaml.safe_dump(items))

def json(self, **kwargs) -> None:
items = kwargs["items"] if "items" in kwargs else []
console = kwargs["console"] if "console" in kwargs else Console()
console.print(json.dumps(items))
console = kwargs["console"] if "console" in kwargs else None
if console is None:
console = Console()
console.print_json(json.dumps(items))

def table(self, **kwargs) -> None:
items = kwargs["items"] if "items" in kwargs else []
console = kwargs["console"] if "console" in kwargs else Console()
console = kwargs["console"] if "console" in kwargs else None
if console is None:
console = Console()
skip_columns = kwargs["skip_columns"] if "skip_columns" in kwargs else []
odd_color = kwargs["odd_color"] if "odd_color" in kwargs else "grey93 on black"
even_color = kwargs["even_color"] if "even_color" in kwargs else "grey50 on black"
Expand All @@ -78,7 +88,7 @@ def print_items(
output,
skip_columns: list = [],
plain_print_f = None,
console: Console = Console(),
console: Console|None = None,
odd_color: str = "grey93 on black",
even_color: str = "grey50 on black"
) -> None:
Expand Down
8 changes: 7 additions & 1 deletion tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,13 @@ def test_print_items_yaml():

def test_print_items_json():
items = [{"Title": "text", "Url": "https://localhost"}]
expected = '[{"Title": "text", "Url": "https://localhost"}]\n'
expected = """[
{
"Title": "text",
"Url": "https://localhost"
}
]
"""
console = Console(force_terminal=True, file=io.StringIO(), _environ={}, color_system=None)
main.print_items(items, output="json", console=console)
assert console.file.getvalue() == expected
Expand Down

0 comments on commit a240aa6

Please sign in to comment.