From c79f5c03a01594e87252da53390b48eb3554d56d Mon Sep 17 00:00:00 2001 From: deadc0de6 Date: Fri, 1 Mar 2019 19:51:07 +0100 Subject: [PATCH] add variables from file doc and refactor to import_variables --- README.md | 38 +++++++++++++++++++++++++++++++++++--- dotdrop/config.py | 2 +- tests-ng/extvariables.sh | 4 ++-- 3 files changed, 38 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 5aefb3d..76a5e63 100644 --- a/README.md +++ b/README.md @@ -84,6 +84,7 @@ why [dotdrop](https://github.com/deadc0de6/dotdrop) rocks. * [Include dotfiles from another profile](#include-dotfiles-from-another-profile) * [Templating](#templating) * [Available variables](#available-variables) + * [Variables from file](#variables-from-file) * [Available methods](#available-methods) * [Dynamic dotfile paths](#dynamic-dotfile-paths) * [Dynamic actions](#dynamic-actions) @@ -655,6 +656,8 @@ the following entries: (absolute path or relative to the config file location, defaults to *~/.config/dotdrop*) * `showdiff`: on install show a diff before asking to overwrite (see `--showdiff`) (default *false*) * `ignoreempty`: do not deploy template if empty (default *false*) + * `import_variables`: list of paths to load variables from + (absolute path or relative to the config file location). * **dotfiles** entry: a list of dotfiles * `dst`: where this dotfile needs to be deployed (can use `variables` and `dynvariables`, make sure to quote). @@ -834,7 +837,7 @@ will result in the following available variables * var4: `echo var1 var2 var3` * dvar4: `var1 var2 var3` -## Variables +### Variables Variables can be added in the config file under the `variables` entry. The variables added there are directly reachable in any templates. @@ -871,7 +874,7 @@ profiles: - f_gitconfig ``` -## Interpreted variables +### Interpreted variables It is also possible to have *dynamic* variables in the sense that their content will be interpreted by the shell before being replaced in the templates. @@ -892,7 +895,7 @@ These can be used as any variables in the templates As for variables (see [Variables](#variables)) profile dynvariables will take precedence over globally defined dynvariables. -## Environment variables +### Environment variables It's possible to access environment variables inside the templates. ``` @@ -924,6 +927,35 @@ alias dotdrop='eval $(grep -v "^#" ~/dotfiles/.env) /usr/bin/dotdrop --cfg=~/dot The above aliases load all the variables from `~/dotfiles/.env` (while omitting lines starting with `#`) before calling dotdrop. +## Variables from file + +Variables can be loaded from external files by specifying their +paths in the config entry `import_variables`. + +`config.yaml` +```yaml +config: + import_variables: + - variables.yaml +variables: + v1: var2 +``` + +`variables.yaml` +```yaml +variables: + v1: var1 + v2: var2 +dynvariables: + dv1: "echo test" +``` + +External variables will take precedence over variables defined within +the source config file. + +This can be useful for example if you have sensitive information stored in variables +and want those to be encrypted when versioned. + ## Available methods Beside jinja2 global functions diff --git a/dotdrop/config.py b/dotdrop/config.py index a5ab494..2a885ed 100644 --- a/dotdrop/config.py +++ b/dotdrop/config.py @@ -33,7 +33,7 @@ class Cfg: key_showdiff = 'showdiff' key_deflink = 'link_by_default' key_workdir = 'workdir' - key_include_vars = 'include_variables' + key_include_vars = 'import_variables' # actions keys key_actions = 'actions' diff --git a/tests-ng/extvariables.sh b/tests-ng/extvariables.sh index e448a18..db97e88 100755 --- a/tests-ng/extvariables.sh +++ b/tests-ng/extvariables.sh @@ -60,7 +60,7 @@ config: backup: true create: true dotpath: dotfiles - include_variables: + import_variables: - $(basename ${extvars}) variables: var1: "var1" @@ -123,7 +123,7 @@ config: backup: true create: true dotpath: dotfiles - include_variables: + import_variables: - $(basename ${extvars}) dotfiles: f_abc: