mirror of
https://github.com/jrnl-org/jrnl.git
synced 2025-05-16 19:18:32 +02:00
Add machine readable --list output
This commit is contained in:
parent
72d1a044d9
commit
d706865850
3 changed files with 30 additions and 11 deletions
|
@ -85,7 +85,13 @@ def parse_args(args=[]):
|
|||
action="store_const",
|
||||
const=postconfig_list,
|
||||
dest="postconfig_cmd",
|
||||
help="List all configured journals",
|
||||
help="""
|
||||
List all configured journals.
|
||||
|
||||
Optional parameters:
|
||||
|
||||
--format [json or yaml]
|
||||
""",
|
||||
)
|
||||
standalone.add_argument(
|
||||
"--ls",
|
||||
|
|
|
@ -56,10 +56,10 @@ def preconfig_version(_):
|
|||
print(output)
|
||||
|
||||
|
||||
def postconfig_list(config, **kwargs):
|
||||
def postconfig_list(args, config, **kwargs):
|
||||
from jrnl.output import list_journals
|
||||
|
||||
print(list_journals(config))
|
||||
print(list_journals(config, args.export))
|
||||
|
||||
|
||||
def postconfig_import(args, config, **kwargs):
|
||||
|
|
|
@ -25,17 +25,30 @@ def deprecated_cmd(old_cmd, new_cmd, callback=None, **kwargs):
|
|||
callback(**kwargs)
|
||||
|
||||
|
||||
def list_journals(configuration):
|
||||
def list_journals(configuration, format=None):
|
||||
from jrnl import config
|
||||
|
||||
"""List the journals specified in the configuration file"""
|
||||
result = f"Journals defined in config ({config.get_config_path()})\n"
|
||||
ml = min(max(len(k) for k in configuration["journals"]), 20)
|
||||
for journal, cfg in configuration["journals"].items():
|
||||
result += " * {:{}} -> {}\n".format(
|
||||
journal, ml, cfg["journal"] if isinstance(cfg, dict) else cfg
|
||||
)
|
||||
return result
|
||||
if format == "json":
|
||||
import json
|
||||
|
||||
return json.dumps(configuration["journals"])
|
||||
elif format == "yaml":
|
||||
from io import StringIO
|
||||
|
||||
from ruamel.yaml import YAML
|
||||
|
||||
output = StringIO()
|
||||
YAML().dump(configuration["journals"], output)
|
||||
return output.getvalue()
|
||||
else:
|
||||
result = f"Journals defined in config ({config.get_config_path()})\n"
|
||||
ml = min(max(len(k) for k in configuration["journals"]), 20)
|
||||
for journal, cfg in configuration["journals"].items():
|
||||
result += " * {:{}} -> {}\n".format(
|
||||
journal, ml, cfg["journal"] if isinstance(cfg, dict) else cfg
|
||||
)
|
||||
return result
|
||||
|
||||
|
||||
def print_msg(msg: Message, **kwargs) -> Union[None, str]:
|
||||
|
|
Loading…
Add table
Reference in a new issue