summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnish Athalye <me@anishathalye.com>2018-10-16 20:50:59 -0400
committerAnish Athalye <me@anishathalye.com>2018-10-16 20:50:59 -0400
commit1b17e8e613c7b56b2c252b3a3e6e66592b798aec (patch)
tree7e961b90a7c017c4a698c4c7ba8f266e510370cd
parentb442b9bdced7d849ddc0ea4283de1f80d9bacd8c (diff)
parentb0ce63904c98a0f3f6547dc0d770aad22b08a8ef (diff)
Merge branch 'linkif'
-rw-r--r--README.md1
-rw-r--r--dotbot/plugins/link.py18
-rw-r--r--test/tests/link-if.bash32
3 files changed, 51 insertions, 0 deletions
diff --git a/README.md b/README.md
index 1e5a5fe..759647e 100644
--- a/README.md
+++ b/README.md
@@ -192,6 +192,7 @@ Available extended configuration parameters:
| `force` | Force removes the old target, file or folder, and forces a new link (default:false) |
| `relative` | Use a relative path when creating the symlink (default:false, absolute links) |
| `glob` | Treat a `*` character as a wildcard, and perform link operations on all of those matches (default:false) |
+| `if` | Execute this in your `$SHELL` and only link if it is successful. |
#### Example
diff --git a/dotbot/plugins/link.py b/dotbot/plugins/link.py
index 5274e3b..bf1c5a9 100644
--- a/dotbot/plugins/link.py
+++ b/dotbot/plugins/link.py
@@ -2,6 +2,7 @@ import os
import glob
import shutil
import dotbot
+import subprocess
class Link(dotbot.Plugin):
@@ -31,6 +32,10 @@ class Link(dotbot.Plugin):
use_glob = defaults.get('glob', False)
if isinstance(source, dict):
# extended config
+ test = source.get('if')
+ if test is not None and not self._test_success(test):
+ self._log.lowinfo('Skipping %s' % destination)
+ continue
relative = source.get('relative', relative)
force = source.get('force', force)
relink = source.get('relink', relink)
@@ -90,6 +95,19 @@ class Link(dotbot.Plugin):
self._log.error('Some links were not successfully set up')
return success
+ def _test_success(self, command):
+ with open(os.devnull, 'w') as devnull:
+ ret = subprocess.call(
+ command,
+ shell=True,
+ stdout=devnull,
+ stderr=devnull,
+ executable=os.environ.get('SHELL'),
+ )
+ if ret != 0:
+ self._log.debug('Test \'%s\' returned false' % command)
+ return ret == 0
+
def _default_source(self, destination, source):
if source is None:
basename = os.path.basename(destination)
diff --git a/test/tests/link-if.bash b/test/tests/link-if.bash
new file mode 100644
index 0000000..9fe6429
--- /dev/null
+++ b/test/tests/link-if.bash
@@ -0,0 +1,32 @@
+test_description='link if'
+. '../test-lib.bash'
+
+test_expect_success 'setup' '
+mkdir ~/d
+echo "apple" > ${DOTFILES}/f
+'
+
+test_expect_success 'run' '
+run_dotbot <<EOF
+- link:
+ ~/.f:
+ path: f
+ if: "true"
+ ~/.g:
+ path: f
+ if: "false"
+ ~/.h:
+ path: f
+ if: "[[ -d ~/d ]]"
+ ~/.i:
+ path: f
+ if: "badcommand"
+EOF
+'
+
+test_expect_success 'test' '
+grep "apple" ~/.f &&
+! test -f ~/.g &&
+grep "apple" ~/.h &&
+! test -f ~/.i
+'