Skip to content

Commit

Permalink
Add plain format output to cli tables (apache#14546)
Browse files Browse the repository at this point in the history
Add plain format output to cli tables so users can use standard 
linux utilities like awk, xargs etc.

closes: apache#14517
  • Loading branch information
turbaszek authored Mar 2, 2021
1 parent 8801a0c commit 0ef084c
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 3 deletions.
6 changes: 3 additions & 3 deletions airflow/cli/cli_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,9 +184,9 @@ def _check(value):
"-o",
"--output",
),
help="Output format. Allowed values: json, yaml, table (default: table)",
metavar="(table, json, yaml)",
choices=("table", "json", "yaml"),
help="Output format. Allowed values: json, yaml, plain, table (default: table)",
metavar="(table, json, yaml, plain)",
choices=("table", "json", "yaml", "plain"),
default="table",
)
ARG_COLOR = Arg(
Expand Down
11 changes: 11 additions & 0 deletions airflow/cli/simple_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from rich.console import Console
from rich.syntax import Syntax
from rich.table import Table
from tabulate import tabulate

from airflow.plugins_manager import PluginsDirectorySource
from airflow.utils.platform import is_tty
Expand Down Expand Up @@ -62,6 +63,15 @@ def print_as_table(self, data: List[Dict]):
table.add_row(*[str(d) for d in row.values()])
self.print(table)

def print_as_plain_table(self, data: List[Dict]):
"""Renders list of dictionaries as a simple table than can be easily piped"""
if not data:
self.print("No data found")
return
rows = [d.values() for d in data]
output = tabulate(rows, tablefmt="plain", headers=data[0].keys())
print(output)

# pylint: disable=too-many-return-statements
def _normalize_data(self, value: Any, output: str) -> Optional[Union[list, str, dict]]:
if isinstance(value, (tuple, list)):
Expand All @@ -82,6 +92,7 @@ def print_as(self, data: List[Union[Dict, Any]], output: str, mapper: Optional[C
"json": self.print_as_json,
"yaml": self.print_as_yaml,
"table": self.print_as_table,
"plain": self.print_as_plain_table,
}
renderer = output_to_renderer.get(output)
if not renderer:
Expand Down
1 change: 1 addition & 0 deletions docs/apache-airflow/usage-cli.rst
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ Some Airflow commands like ``airflow dags list`` or ``airflow tasks states-for-d
which allow users to change the formatting of command's output. Possible options:

- ``table`` - renders the information as a plain text table
- ``simple`` - renders the information as simple table which can be parsed by standard linux utilities
- ``json`` - renders the information in form of json string
- ``yaml`` - render the information in form of valid yaml

Expand Down
1 change: 1 addition & 0 deletions docs/spelling_wordlist.txt
Original file line number Diff line number Diff line change
Expand Up @@ -940,6 +940,7 @@ licence
licences
lifecycle
lineterminator
linux
livy
localExecutor
localexecutor
Expand Down

0 comments on commit 0ef084c

Please sign in to comment.