1
0
Fork 0
mirror of synced 2024-11-03 00:48:57 -04:00

Add mode option to create directive

See <https://github.com/anishathalye/dotbot/issues/260>. Thanks to
@eengstrom for the feature suggestion.
This commit is contained in:
Anish Athalye 2021-02-25 06:25:17 -05:00
parent 4ec846cdad
commit 945c1e5a4e
3 changed files with 55 additions and 8 deletions

View file

@ -247,15 +247,30 @@ apps, plugins, shell commands, etc.
#### Format #### Format
Create commands are specified as an array of directories to be created. Create commands are specified as an array of directories to be created. If you
want to use the optional extended configuration, create commands are specified
as dictionaries. For convenience, it's permissible to leave the options blank
(null) in the dictionary syntax.
| Parameter | Explanation |
| --- | --- |
| `mode` | The file mode to use for creating the leaf directory (default: 0777) |
The `mode` parameter is treated in the same way as in Python's
[os.mkdir](https://docs.python.org/3/library/os.html#mkdir-modebits). Its
behavior is platform-dependent. On Unix systems, the current umask value is
first masked out.
#### Example #### Example
```yaml ```yaml
- create: - create:
- ~/projects
- ~/downloads - ~/downloads
- ~/.vim/undo-history - ~/.vim/undo-history
- create:
~/.ssh:
mode: 0700
~/projects:
``` ```
### Shell ### Shell

View file

@ -19,9 +19,15 @@ class Create(dotbot.Plugin):
def _process_paths(self, paths): def _process_paths(self, paths):
success = True success = True
for path in paths: defaults = self._context.defaults().get('create', {})
path = os.path.expandvars(os.path.expanduser(path)) for key in paths:
success &= self._create(path) path = os.path.expandvars(os.path.expanduser(key))
mode = defaults.get('mode', 0o777) # same as the default for os.makedirs
if isinstance(paths, dict):
options = paths[key]
if options:
mode = options.get('mode', mode)
success &= self._create(path, mode)
if success: if success:
self._log.info('All paths have been set up') self._log.info('All paths have been set up')
else: else:
@ -35,13 +41,13 @@ class Create(dotbot.Plugin):
path = os.path.expanduser(path) path = os.path.expanduser(path)
return os.path.exists(path) return os.path.exists(path)
def _create(self, path): def _create(self, path, mode):
success = True success = True
if not self._exists(path): if not self._exists(path):
self._log.debug('Trying to create path %s' % path) self._log.debug('Trying to create path %s with mode %o' % (path, mode))
try: try:
self._log.lowinfo('Creating path %s' % path) self._log.lowinfo('Creating path %s' % path)
os.makedirs(path) os.makedirs(path, mode)
except OSError: except OSError:
self._log.warning('Failed to create path %s' % path) self._log.warning('Failed to create path %s' % path)
success = False success = False

View file

@ -0,0 +1,26 @@
test_description='create mode'
. '../test-lib.bash'
test_expect_success 'run' '
run_dotbot -v <<EOF
- defaults:
create:
mode: 0755
- create:
- ~/downloads
- ~/.vim/undo-history
- create:
~/.ssh:
mode: 0700
~/projects:
EOF
'
test_expect_success 'test' '
[ -d ~/downloads ] &&
[ -d ~/.vim/undo-history ] &&
[ -d ~/.ssh ] &&
[ -d ~/projects ] &&
[ "$(stat -c %a ~/.ssh)" = "700" ] &&
[ "$(stat -c %a ~/downloads)" = "755" ]
'