From 4bfa3291ba841036bc92b0e9d879bd05d43971f2 Mon Sep 17 00:00:00 2001 From: Casey Rodarmor Date: Fri, 18 Nov 2016 15:21:27 -0800 Subject: [PATCH 1/2] Allow empty link sources If the source for a link is null, use the basename of the destination with a single leading '.' removed, if present. --- README.md | 41 +++++++++++++++++++++++++++++ plugins/link.py | 14 ++++++++-- test/tests/link-default-source.bash | 26 ++++++++++++++++++ 3 files changed, 79 insertions(+), 2 deletions(-) create mode 100644 test/tests/link-default-source.bash diff --git a/README.md b/README.md index 6fbc687..909b4d9 100644 --- a/README.md +++ b/README.md @@ -189,6 +189,47 @@ symbolic link should have a relative path. path: zshrc ``` +If the source location is omitted or NULL, dotbot will use the destination instead, without a leading `.` if present. This makes the following three config files equivalent: + +```yaml +- link: + ~/bin/ack: ack + ~/.vim: vim + ~/.vimrc: + relink: true + path: vimrc + ~/.zshrc: + force: true + path: zshrc +``` + +```yaml +- link: + ~/bin/ack: + ~/.vim: + ~/.vimrc: + relink: true + ~/.zshrc: + force: true +``` + +```json +[ + { + "link": { + "~/bin/ack": null, + "~/.vim": null, + "~/.vimrc": { + "relink": true + }, + "~/.zshrc": { + "force": true + } + } + } +] +``` + ### Shell Shell commands specify shell commands to be run. Shell commands are run in the diff --git a/plugins/link.py b/plugins/link.py index ff8685e..44a865e 100644 --- a/plugins/link.py +++ b/plugins/link.py @@ -30,9 +30,9 @@ class Link(dotbot.Plugin): force = source.get('force', force) relink = source.get('relink', relink) create = source.get('create', create) - path = source['path'] + path = self._default_source(destination, source.get('path')) else: - path = source + path = self._default_source(destination, source) path = os.path.expandvars(os.path.expanduser(path)) if create: success &= self._create(destination) @@ -46,6 +46,16 @@ class Link(dotbot.Plugin): self._log.error('Some links were not successfully set up') return success + def _default_source(self, destination, source): + if source == None: + basename = os.path.basename(destination) + if basename.startswith('.'): + return basename[1:] + else: + return basename + else: + return source + def _is_link(self, path): ''' Returns true if the path is a symbolic link. diff --git a/test/tests/link-default-source.bash b/test/tests/link-default-source.bash new file mode 100644 index 0000000..60527d9 --- /dev/null +++ b/test/tests/link-default-source.bash @@ -0,0 +1,26 @@ +test_description='link uses destination if source is null' +. '../test-lib.bash' + +test_expect_success 'setup' ' +echo "apple" > ${DOTFILES}/f && +echo "grape" > ${DOTFILES}/fd +' + +test_expect_success 'run' ' +run_dotbot < Date: Sat, 19 Nov 2016 16:37:44 -0800 Subject: [PATCH 2/2] Improve readme wording, == -> is, wrap to 80 characters, use 4 space indentation --- README.md | 4 +++- plugins/link.py | 16 ++++++++-------- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 909b4d9..25cff54 100644 --- a/README.md +++ b/README.md @@ -189,7 +189,9 @@ symbolic link should have a relative path. path: zshrc ``` -If the source location is omitted or NULL, dotbot will use the destination instead, without a leading `.` if present. This makes the following three config files equivalent: +If the source location is omitted or set to `null`, Dotbot will use the +basename of the destination, with a leading `.` stripped if present. This makes +the following three config files equivalent: ```yaml - link: diff --git a/plugins/link.py b/plugins/link.py index 44a865e..ee64175 100644 --- a/plugins/link.py +++ b/plugins/link.py @@ -47,14 +47,14 @@ class Link(dotbot.Plugin): return success def _default_source(self, destination, source): - if source == None: - basename = os.path.basename(destination) - if basename.startswith('.'): - return basename[1:] - else: - return basename - else: - return source + if source is None: + basename = os.path.basename(destination) + if basename.startswith('.'): + return basename[1:] + else: + return basename + else: + return source def _is_link(self, path): '''