Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
These should be shell commands, but I only have helpers for non-shell.
The error handling has to be beefed up, and is it safe to run shell
commands?
  • Loading branch information
Ned Batchelder committed Oct 9, 2023
1 parent 8cc5926 commit 1dbcea4
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/scriv/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from .exceptions import ScrivException
from .literals import find_literal
from .optional import tomllib
from .shell import run_command

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -363,6 +364,7 @@ def resolve_value(self, value: str) -> str:
Prefixes:
"file:" read the content from a file.
"literal:" read a literal string from a file.
"command:" read the output of a shell command.
"""
value = value.replace("${config:format}", self._options.format)
Expand Down Expand Up @@ -392,6 +394,14 @@ def resolve_value(self, value: str) -> str:
+ f"{value!r}"
)
value = found
elif value.startswith("command:"):
cmd = value.partition(":")[2].strip()
ok, out = run_command(cmd)
if not ok:
raise ScrivException(f"Command {cmd!r} failed:\n{out}")
if out.count("\n") == 1:
out = out.rstrip("\n")
value = out
return value

def read_file_value(self, file_name: str) -> str:
Expand Down
16 changes: 16 additions & 0 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,3 +344,19 @@ def test_no_toml_installed_no_settings(self, temp_dir):
with without_module(scriv.config, "tomllib"):
config = Config.read()
assert config.categories[0] == "Removed"


def test_command_running(fake_run_command):
# Any setting can be the output of a command.
fake_run_command.patch_module("scriv.config")
fake_run_command.add_handler(
"showtext", lambda argv: (True, " ".join(argv[1:]))
)
text = Config(output_file="command: showtext Xyzzy 2 3").output_file
assert text == "Xyzzy 2 3"


def test_nosuch_command(fake_run_command):
# Any setting can be the output of a command.
text = Config(output_file="command: xyzzyplugh").output_file
assert text == "Xyzzy 2 3"

0 comments on commit 1dbcea4

Please sign in to comment.