Skip to content

Commit e288e42

Browse files
committed
Ensure dynaconf get returns a valid json string.
1 parent 6395e97 commit e288e42

3 files changed

Lines changed: 25 additions & 2 deletions

File tree

dynaconf/cli.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22

33
import importlib
4+
import json
45
import os
56
import pprint
67
import sys
@@ -439,7 +440,10 @@ def init(ctx, fileformat, path, env, _vars, _secrets, wg, y, django):
439440
is_flag=True,
440441
)
441442
def get(key, default, env, unparse):
442-
"""Returns the raw value for a settings key"""
443+
"""Returns the raw value for a settings key.
444+
445+
If result is a dict, list or tuple it is printes as a valid json string.
446+
"""
443447
if env:
444448
env = env.strip()
445449
if key:
@@ -456,6 +460,9 @@ def get(key, default, env, unparse):
456460
if unparse:
457461
result = unparse_conf_data(result)
458462

463+
if isinstance(result, (dict, list, tuple)):
464+
result = json.dumps(result, sort_keys=True)
465+
459466
click.echo(result, nl=False)
460467

461468

tests/config.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,8 @@
33
from dynaconf import Dynaconf
44

55
settingsenv = Dynaconf(environments=True)
6-
settings = Dynaconf(TEST_KEY="test_value", ANOTHER_KEY="another_value")
6+
settings = Dynaconf(
7+
TEST_KEY="test_value",
8+
ANOTHER_KEY="another_value",
9+
DATA={"KEY": "value", "OTHERKEY": "other value"},
10+
)

tests/test_cli.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,18 @@ def test_get(testdir):
125125
assert result == "test_value"
126126

127127

128+
def test_get_json_dict(testdir):
129+
"""Tests get command printing json"""
130+
env = env = {
131+
"ROOT_PATH_FOR_DYNACONF": testdir,
132+
"DYNACONF_DATA__KEY": "value",
133+
"DYNACONF_DATA__OTHERKEY": "other value",
134+
"INSTANCE_FOR_DYNACONF": "tests.config.settings",
135+
}
136+
result = run(["get", "data"], env=env)
137+
assert result == '{"KEY": "value", "OTHERKEY": "other value"}'
138+
139+
128140
def test_get_lower(testdir):
129141
"""Tests get command"""
130142
result = run(

0 commit comments

Comments
 (0)