Skip to content

Commit 3dc2e4a

Browse files
committed
Options: Do not assume defaults for ACTION and --grafana_label args
1 parent ae50df2 commit 3dc2e4a

File tree

4 files changed

+25
-3
lines changed

4 files changed

+25
-3
lines changed

.github/workflows/tests.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,14 @@ jobs:
2424
python-version: ["3.6", "3.13"]
2525
fail-fast: false
2626

27+
services:
28+
grafana:
29+
image: grafana/grafana:latest
30+
ports:
31+
- 3000:3000
32+
env:
33+
GF_SECURITY_ADMIN_PASSWORD: admin
34+
2735
env:
2836
OS: ${{ matrix.os }}
2937
PYTHON: ${{ matrix.python-version }}

HISTORY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
by respecting the `credential` keyword argument
88
- Added basic example program about how to import a dashboard
99
- Options: Permitted usage without authentication credentials or token
10+
- Options: Do not assume defaults for `ACTION` and `--grafana_label` args
1011

1112
## 0.4.0 (2024-10-16)
1213
- Fixed folder argument issue

grafana_import/cli.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,6 @@ def main():
117117
"-g",
118118
"--grafana_label",
119119
help="label in the config file that represents the grafana to connect to.",
120-
default="default",
121120
)
122121

123122
parser.add_argument("-f", "--grafana_folder", help="the folder name where to import into Grafana.")
@@ -159,7 +158,6 @@ def main():
159158
metavar="ACTION",
160159
nargs="?",
161160
choices=["import", "export", "remove"],
162-
default="import",
163161
help="action to perform. Is one of 'export', 'import' (default), or 'remove'.\n"
164162
"export: lookup for dashboard name in Grafana and dump it to local file.\n"
165163
"import: import a local dashboard file (previously exported) to Grafana.\n"
@@ -332,7 +330,7 @@ def process_dashboard(file_path):
332330
sys.exit(1)
333331

334332
# Export
335-
else:
333+
elif args.action == "export":
336334
dashboard_name = config["general"]["dashboard_name"]
337335
try:
338336
dash = grafana_api.export_dashboard(dashboard_name)
@@ -347,6 +345,10 @@ def process_dashboard(file_path):
347345
save_dashboard(config, args, base_path, dashboard_name, dash, "exported")
348346
sys.exit(0)
349347

348+
else:
349+
logger.error(f"Unknown action: {args.action}. Use one of: {parser._actions[-2].choices}")
350+
sys.exit(1)
351+
350352

351353
if __name__ == "__main__":
352354
main()

tests/test_cli.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,17 @@ def get_settings_arg(use_settings: bool = True):
1919
return "--grafana_url http://localhost:3000"
2020

2121

22+
def test_no_action_failure(caplog):
23+
"""
24+
Verify the program fails when invoked without positional `action` argument.
25+
"""
26+
sys.argv = ["grafana-import"]
27+
with pytest.raises(SystemExit) as ex:
28+
main()
29+
assert ex.match("1")
30+
assert "Unknown action: None. Use one of: ['import', 'export', 'remove']" in caplog.messages
31+
32+
2233
@pytest.mark.parametrize("use_settings", [True, False], ids=["config-yes", "config-no"])
2334
def test_import_dashboard_success(mocked_grafana, mocked_responses, tmp_path, caplog, use_settings):
2435
"""

0 commit comments

Comments
 (0)