From 945c1e5a4ebf1c5e24280ced0752f2e26d71a922 Mon Sep 17 00:00:00 2001 From: Anish Athalye Date: Thu, 25 Feb 2021 06:25:17 -0500 Subject: [PATCH] Add mode option to create directive See . Thanks to @eengstrom for the feature suggestion. --- README.md | 19 +++++++++++++++++-- dotbot/plugins/create.py | 18 ++++++++++++------ test/tests/create-mode.bash | 26 ++++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 8 deletions(-) create mode 100644 test/tests/create-mode.bash diff --git a/README.md b/README.md index e2e27b1..f7fccdf 100644 --- a/README.md +++ b/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 diff --git a/dotbot/plugins/create.py b/dotbot/plugins/create.py index 015645a..d7754fb 100644 --- a/dotbot/plugins/create.py +++ b/dotbot/plugins/create.py @@ -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 diff --git a/test/tests/create-mode.bash b/test/tests/create-mode.bash new file mode 100644 index 0000000..e2dd649 --- /dev/null +++ b/test/tests/create-mode.bash @@ -0,0 +1,26 @@ +test_description='create mode' +. '../test-lib.bash' + +test_expect_success 'run' ' +run_dotbot -v <