summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlbert Puig <albert.puig@cern.ch>2018-05-25 22:04:04 +0200
committerAnish Athalye <me@anishathalye.com>2019-12-31 14:25:53 -0500
commiteabd84bce1139a5c48089c97cd74bb919a8df331 (patch)
tree29378daa81cf6511089b5342b637a1cc842f0257
parenta8380f6496d091a13769bd12bb6ca7a89d3618eb (diff)
Add ignore-missing option to link
-rw-r--r--README.md1
-rw-r--r--dotbot/plugins/link.py18
-rw-r--r--test/tests/link-ignore-missing.bash23
3 files changed, 36 insertions, 6 deletions
diff --git a/README.md b/README.md
index cff7a3b..00094aa 100644
--- a/README.md
+++ b/README.md
@@ -178,6 +178,7 @@ Available extended configuration parameters:
| `relative` | Use a relative path to the source 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. |
+| `ignore-missing` | Do not fail if the source is missing and create the link anyway (default:false) |
#### Example
diff --git a/dotbot/plugins/link.py b/dotbot/plugins/link.py
index 0b47ac6..bf3db3e 100644
--- a/dotbot/plugins/link.py
+++ b/dotbot/plugins/link.py
@@ -31,6 +31,7 @@ class Link(dotbot.Plugin):
create = defaults.get('create', False)
use_glob = defaults.get('glob', False)
test = defaults.get('if', None)
+ ignore_missing = defaults.get('ignore-missing', False)
if isinstance(source, dict):
# extended config
test = source.get('if', test)
@@ -39,6 +40,7 @@ class Link(dotbot.Plugin):
relink = source.get('relink', relink)
create = source.get('create', create)
use_glob = source.get('glob', use_glob)
+ ignore_missing = source.get('ignore-missing', ignore_missing)
path = self._default_source(destination, source.get('path'))
else:
path = self._default_source(destination, source)
@@ -67,7 +69,7 @@ class Link(dotbot.Plugin):
success &= self._create(destination)
if force or relink:
success &= self._delete(path, destination, relative, force)
- success &= self._link(path, destination, relative)
+ success &= self._link(path, destination, relative, ignore_missing)
else:
self._log.lowinfo("Globs from '" + path + "': " + str(glob_results))
glob_base = path[:glob_star_loc]
@@ -78,18 +80,22 @@ class Link(dotbot.Plugin):
success &= self._create(glob_link_destination)
if force or relink:
success &= self._delete(glob_full_item, glob_link_destination, relative, force)
- success &= self._link(glob_full_item, glob_link_destination, relative)
+ success &= self._link(glob_full_item, glob_link_destination, relative, ignore_missing)
else:
if create:
success &= self._create(destination)
- if not self._exists(os.path.join(self._context.base_directory(), path)):
+ if not ignore_missing and not self._exists(os.path.join(self._context.base_directory(), path)):
+ # we seemingly check this twice (here and in _link) because
+ # if the file doesn't exist and force is True, we don't
+ # want to remove the original (this is tested by
+ # link-force-leaves-when-nonexistent.bash)
success = False
self._log.warning('Nonexistent source %s -> %s' %
(destination, path))
continue
if force or relink:
success &= self._delete(path, destination, relative, force)
- success &= self._link(path, destination, relative)
+ success &= self._link(path, destination, relative, ignore_missing)
if success:
self._log.info('All links have been set up')
else:
@@ -189,7 +195,7 @@ class Link(dotbot.Plugin):
destination_dir = os.path.dirname(destination)
return os.path.relpath(source, destination_dir)
- def _link(self, source, link_name, relative):
+ def _link(self, source, link_name, relative, ignore_missing):
'''
Links link_name to source.
@@ -209,7 +215,7 @@ class Link(dotbot.Plugin):
# we need to use absolute_source below because our cwd is the dotfiles
# directory, and if source is relative, it will be relative to the
# destination directory
- elif not self._exists(link_name) and self._exists(absolute_source):
+ elif not self._exists(link_name) and (ignore_missing or self._exists(absolute_source)):
try:
os.symlink(source, destination)
except OSError:
diff --git a/test/tests/link-ignore-missing.bash b/test/tests/link-ignore-missing.bash
new file mode 100644
index 0000000..4978279
--- /dev/null
+++ b/test/tests/link-ignore-missing.bash
@@ -0,0 +1,23 @@
+test_description='link is created even if source is missing'
+. '../test-lib.bash'
+
+test_expect_failure 'run' '
+run_dotbot <<EOF
+- link:
+ ~/missing_link:
+ path: missing
+EOF
+'
+
+test_expect_success 'run 2' '
+run_dotbot <<EOF
+- link:
+ ~/missing_link:
+ path: missing
+ ignore-missing: true
+EOF
+'
+
+test_expect_success 'test' '
+test -L ~/missing_link
+'