Skip to content

Commit 54a5bf4

Browse files
author
Hugo Osvaldo Barrera
authored
Merge pull request #953 from electrickite/shell-fetch
Add shell strategy to fetch params
2 parents 10659b8 + 59b95d9 commit 54a5bf4

File tree

5 files changed

+19
-3
lines changed

5 files changed

+19
-3
lines changed

AUTHORS.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ In alphabetical order:
66
- Ben Boeckel
77
- Christian Geier
88
- Clément Mondon
9+
- Corey Hinshaw
910
- Hugo Osvaldo Barrera
1011
- Julian Mehne
1112
- Malte Kiefer

CHANGELOG.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ may want to subscribe to `GitHub's tag feed
1212
Version 0.19.0
1313
==============
1414

15+
- Add "shell" password fetch strategy to pass command string to a shell.
1516
- Add "description" and "order" as metadata. These fetch the CalDAV:
1617
calendar-description, CardDAV:addressbook-description and apple-ns:calendar-order
1718
properties.

docs/keyring.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ You can fetch the username as well::
3838

3939
Or really any kind of parameter in a storage section.
4040

41+
You can also pass the command as a string to be executed in a shell::
42+
43+
[storage foo]
44+
...
45+
password.fetch = ["shell", "~/.local/bin/get-my-password | head -n1"]
46+
4147
With pass_ for example, you might find yourself writing something like this in
4248
your configuration file::
4349

tests/system/cli/test_fetchparams.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def test_get_password_from_command(tmpdir, runner):
1111
collections = ["a", "b", "c"]
1212
1313
[storage foo]
14-
type = "filesystem"
14+
type.fetch = ["shell", "echo filesystem"]
1515
path = "{base}/foo/"
1616
fileext.fetch = ["command", "echo", ".txt"]
1717

vdirsyncer/cli/fetchparams.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ def _fetch_value(opts, key):
7272
return rv
7373

7474

75-
def _strategy_command(*command: str):
75+
def _strategy_command(*command: str, shell: bool = False):
7676
"""Execute a user-specified command and return its output."""
7777
import subprocess
7878

@@ -82,18 +82,26 @@ def _strategy_command(*command: str):
8282
expanded_command = list(map(expand_path, command))
8383

8484
try:
85-
stdout = subprocess.check_output(expanded_command, universal_newlines=True)
85+
stdout = subprocess.check_output(
86+
expanded_command, universal_newlines=True, shell=shell
87+
)
8688
return stdout.strip("\n")
8789
except OSError as e:
8890
cmd = " ".join(expanded_command)
8991
raise exceptions.UserError(f"Failed to execute command: {cmd}\n{str(e)}")
9092

9193

94+
def _strategy_shell(*command: str):
95+
"""Execute a user-specified command string in a shell and return its output."""
96+
return _strategy_command(*command, shell=True)
97+
98+
9299
def _strategy_prompt(text):
93100
return click.prompt(text, hide_input=True)
94101

95102

96103
STRATEGIES = {
97104
"command": _strategy_command,
105+
"shell": _strategy_shell,
98106
"prompt": _strategy_prompt,
99107
}

0 commit comments

Comments
 (0)