1
0
Fork 0
mirror of synced 2024-11-26 10:05:34 -05:00

Merge branch 'linkif'

This commit is contained in:
Anish Athalye 2018-10-16 20:50:59 -04:00
commit 1b17e8e613
3 changed files with 51 additions and 0 deletions

View file

@ -192,6 +192,7 @@ Available extended configuration parameters:
| `force` | Force removes the old target, file or folder, and forces a new link (default:false) | | `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) | | `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) | | `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 #### Example

View file

@ -2,6 +2,7 @@ import os
import glob import glob
import shutil import shutil
import dotbot import dotbot
import subprocess
class Link(dotbot.Plugin): class Link(dotbot.Plugin):
@ -31,6 +32,10 @@ class Link(dotbot.Plugin):
use_glob = defaults.get('glob', False) use_glob = defaults.get('glob', False)
if isinstance(source, dict): if isinstance(source, dict):
# extended config # 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) relative = source.get('relative', relative)
force = source.get('force', force) force = source.get('force', force)
relink = source.get('relink', relink) relink = source.get('relink', relink)
@ -90,6 +95,19 @@ class Link(dotbot.Plugin):
self._log.error('Some links were not successfully set up') self._log.error('Some links were not successfully set up')
return success 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): def _default_source(self, destination, source):
if source is None: if source is None:
basename = os.path.basename(destination) basename = os.path.basename(destination)

32
test/tests/link-if.bash Normal file
View file

@ -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
'