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:
parent
4ec846cdad
commit
945c1e5a4e
3 changed files with 55 additions and 8 deletions
19
README.md
19
README.md
|
@ -247,15 +247,30 @@ apps, plugins, shell commands, etc.
|
|||
|
||||
#### 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
|
||||
|
||||
```yaml
|
||||
- create:
|
||||
- ~/projects
|
||||
- ~/downloads
|
||||
- ~/.vim/undo-history
|
||||
- create:
|
||||
~/.ssh:
|
||||
mode: 0700
|
||||
~/projects:
|
||||
```
|
||||
|
||||
### Shell
|
||||
|
|
|
@ -19,9 +19,15 @@ class Create(dotbot.Plugin):
|
|||
|
||||
def _process_paths(self, paths):
|
||||
success = True
|
||||
for path in paths:
|
||||
path = os.path.expandvars(os.path.expanduser(path))
|
||||
success &= self._create(path)
|
||||
defaults = self._context.defaults().get('create', {})
|
||||
for key in paths:
|
||||
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:
|
||||
self._log.info('All paths have been set up')
|
||||
else:
|
||||
|
@ -35,13 +41,13 @@ class Create(dotbot.Plugin):
|
|||
path = os.path.expanduser(path)
|
||||
return os.path.exists(path)
|
||||
|
||||
def _create(self, path):
|
||||
def _create(self, path, mode):
|
||||
success = True
|
||||
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:
|
||||
self._log.lowinfo('Creating path %s' % path)
|
||||
os.makedirs(path)
|
||||
os.makedirs(path, mode)
|
||||
except OSError:
|
||||
self._log.warning('Failed to create path %s' % path)
|
||||
success = False
|
||||
|
|
26
test/tests/create-mode.bash
Normal file
26
test/tests/create-mode.bash
Normal 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" ]
|
||||
'
|
Loading…
Reference in a new issue