Merge pull request #953 from electrickite/shell-fetch

Add shell strategy to fetch params
This commit is contained in:
Hugo Osvaldo Barrera 2022-01-14 12:48:07 +01:00 committed by GitHub
commit 54a5bf4ad3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 19 additions and 3 deletions

View file

@ -6,6 +6,7 @@ In alphabetical order:
- Ben Boeckel
- Christian Geier
- Clément Mondon
- Corey Hinshaw
- Hugo Osvaldo Barrera
- Julian Mehne
- Malte Kiefer

View file

@ -12,6 +12,7 @@ may want to subscribe to `GitHub's tag feed
Version 0.19.0
==============
- Add "shell" password fetch strategy to pass command string to a shell.
- Add "description" and "order" as metadata. These fetch the CalDAV:
calendar-description, CardDAV:addressbook-description and apple-ns:calendar-order
properties.

View file

@ -38,6 +38,12 @@ You can fetch the username as well::
Or really any kind of parameter in a storage section.
You can also pass the command as a string to be executed in a shell::
[storage foo]
...
password.fetch = ["shell", "~/.local/bin/get-my-password | head -n1"]
With pass_ for example, you might find yourself writing something like this in
your configuration file::

View file

@ -11,7 +11,7 @@ def test_get_password_from_command(tmpdir, runner):
collections = ["a", "b", "c"]
[storage foo]
type = "filesystem"
type.fetch = ["shell", "echo filesystem"]
path = "{base}/foo/"
fileext.fetch = ["command", "echo", ".txt"]

View file

@ -72,7 +72,7 @@ def _fetch_value(opts, key):
return rv
def _strategy_command(*command: str):
def _strategy_command(*command: str, shell: bool = False):
"""Execute a user-specified command and return its output."""
import subprocess
@ -82,18 +82,26 @@ def _strategy_command(*command: str):
expanded_command = list(map(expand_path, command))
try:
stdout = subprocess.check_output(expanded_command, universal_newlines=True)
stdout = subprocess.check_output(
expanded_command, universal_newlines=True, shell=shell
)
return stdout.strip("\n")
except OSError as e:
cmd = " ".join(expanded_command)
raise exceptions.UserError(f"Failed to execute command: {cmd}\n{str(e)}")
def _strategy_shell(*command: str):
"""Execute a user-specified command string in a shell and return its output."""
return _strategy_command(*command, shell=True)
def _strategy_prompt(text):
return click.prompt(text, hide_input=True)
STRATEGIES = {
"command": _strategy_command,
"shell": _strategy_shell,
"prompt": _strategy_prompt,
}