Skip to content

Commit 8159e46

Browse files
committed
History: Unlock YAML export format
1 parent ba267bb commit 8159e46

File tree

4 files changed

+37
-24
lines changed

4 files changed

+37
-24
lines changed

CHANGES.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ in progress
1111
- History: Stop ``grafana-wtf log <UID>`` acquiring *all* dashboards
1212
- Refactoring: Move all report renderers to ``grafana_wtf.report``
1313
- History: Add ``id`` and ``uid`` dashboard attributes to report
14+
- History: Unlock YAML export format
1415

1516
2023-03-05 0.14.1
1617
=================

grafana_wtf/commands.py

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,23 @@
11
# -*- coding: utf-8 -*-
22
# (c) 2019-2021 Andreas Motl <[email protected]>
33
# License: GNU Affero General Public License, Version 3
4-
import json
54
import logging
65
import os
76
from functools import partial
87
from operator import itemgetter
9-
from typing import List
108

119
from docopt import DocoptExit, docopt
1210

1311
from grafana_wtf import __appname__, __version__
1412
from grafana_wtf.core import GrafanaWtf
13+
from grafana_wtf.report.data import output_results
1514
from grafana_wtf.report.textual import TextualSearchReport
1615
from grafana_wtf.report.tabular import TabularSearchReport, get_table_format, TabularEditHistoryReport
1716
from grafana_wtf.util import (
1817
configure_http_logging,
1918
normalize_options,
2019
read_list,
2120
setup_logging,
22-
yaml_dump,
2321
)
2422

2523
log = logging.getLogger(__name__)
@@ -229,17 +227,12 @@ def run():
229227
count = int(options.number)
230228
entries = entries[:count]
231229

232-
if output_format == "json":
233-
output = json.dumps(entries, indent=4)
234-
235-
elif output_format.startswith("tabular"):
230+
if output_format.startswith("tabular"):
236231
report = TabularEditHistoryReport(data=entries)
237232
output = report.render(output_format)
238-
233+
print(output)
239234
else:
240-
raise ValueError(f'Unknown output format "{output_format}"')
241-
242-
print(output)
235+
output_results(output_format, entries)
243236

244237
if options.explore and options.datasources:
245238
results = engine.explore_datasources()
@@ -257,16 +250,3 @@ def run():
257250
if options.info:
258251
response = engine.info()
259252
output_results(output_format, response)
260-
261-
262-
def output_results(output_format: str, results: List):
263-
if output_format == "json":
264-
output = json.dumps(results, indent=4)
265-
266-
elif output_format == "yaml":
267-
output = yaml_dump(results)
268-
269-
else:
270-
raise ValueError(f'Unknown output format "{output_format}"')
271-
272-
print(output)

grafana_wtf/report/data.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import json
2+
from typing import List
3+
4+
from grafana_wtf.util import yaml_dump
5+
6+
7+
def output_results(output_format: str, results: List):
8+
if output_format == "json":
9+
output = json.dumps(results, indent=4)
10+
11+
elif output_format == "yaml":
12+
output = yaml_dump(results)
13+
14+
else:
15+
raise ValueError(f'Unknown output format "{output_format}"')
16+
17+
print(output)

tests/test_commands.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,21 @@ def test_log_tabular_success(ldi_resources, capsys, caplog):
274274
assert first_item_normalized == reference
275275

276276

277+
def test_log_yaml_success(ldi_resources, capsys, caplog):
278+
# Only provision specific dashboard(s).
279+
ldi_resources(dashboards=["tests/grafana/dashboards/ldi-v27.json", "tests/grafana/dashboards/ldi-v33.json"])
280+
281+
# Run command and capture output.
282+
set_command("log")
283+
with caplog.at_level(logging.DEBUG):
284+
grafana_wtf.commands.run()
285+
captured = capsys.readouterr()
286+
287+
data = yaml.safe_load(captured.out)
288+
289+
assert len(data) == 3
290+
291+
277292
def test_explore_datasources_used(create_datasource, create_dashboard, capsys, caplog):
278293
# Create two data sources and a dashboard which uses them.
279294
ds_foo = create_datasource(name="foo")

0 commit comments

Comments
 (0)