1
0
mirror of https://github.com/deadc0de6/dotdrop.git synced 2026-02-04 15:39:43 +00:00

Merge pull request #145 from ziirish/inherited-variables

adding ability to inherit variables from included profiles
This commit is contained in:
deadc0de
2019-05-23 19:39:12 +02:00
committed by GitHub
2 changed files with 134 additions and 14 deletions

View File

@@ -1118,31 +1118,39 @@ class Cfg:
t.update_variables(variables)
return variables
def _get_variables(self, profile=None):
def _get_variables(self, profile=None, sub=False):
"""return the un-interpreted variables"""
variables = {}
# profile variable
if profile:
variables['profile'] = profile
if not sub:
# profile variable
if profile:
variables['profile'] = profile
# add paths variables
variables['_dotdrop_dotpath'] = self.lnk_settings[self.key_dotpath]
variables['_dotdrop_cfgpath'] = self.cfgpath
variables['_dotdrop_workdir'] = self.lnk_settings[self.key_workdir]
# add paths variables
variables['_dotdrop_dotpath'] = self.lnk_settings[self.key_dotpath]
variables['_dotdrop_cfgpath'] = self.cfgpath
variables['_dotdrop_workdir'] = self.lnk_settings[self.key_workdir]
# global variables
if self.key_variables in self.content:
variables.update(self.content[self.key_variables])
# global variables
if self.key_variables in self.content:
variables.update(self.content[self.key_variables])
# external variables
variables.update(self.ext_variables)
# external variables
variables.update(self.ext_variables)
if not profile or profile not in self.lnk_profiles:
return variables
# profile variables
var = self.lnk_profiles[profile]
# inherited profile variables
if self.key_profiles_incl in var.keys():
for inherited_profile in var[self.key_profiles_incl]:
inherited_vars = self._get_variables(inherited_profile, True)
variables.update(inherited_vars)
# finally we override with profile variables
if self.key_variables in var.keys():
for k, v in var[self.key_variables].items():
variables[k] = v

112
tests-ng/variables-include.sh Executable file
View File

@@ -0,0 +1,112 @@
#!/usr/bin/env bash
# author: deadc0de6 (https://github.com/deadc0de6)
# Copyright (c) 2017, deadc0de6
#
# test variables from yaml file
# returns 1 in case of error
#
# exit on first error
set -e
# all this crap to get current path
rl="readlink -f"
if ! ${rl} "${0}" >/dev/null 2>&1; then
rl="realpath"
if ! hash ${rl}; then
echo "\"${rl}\" not found !" && exit 1
fi
fi
cur=$(dirname "$(${rl} "${0}")")
#hash dotdrop >/dev/null 2>&1
#[ "$?" != "0" ] && echo "install dotdrop to run tests" && exit 1
#echo "called with ${1}"
# dotdrop path can be pass as argument
ddpath="${cur}/../"
[ "${1}" != "" ] && ddpath="${1}"
[ ! -d ${ddpath} ] && echo "ddpath \"${ddpath}\" is not a directory" && exit 1
export PYTHONPATH="${ddpath}:${PYTHONPATH}"
bin="python3 -m dotdrop.dotdrop"
echo "dotdrop path: ${ddpath}"
echo "pythonpath: ${PYTHONPATH}"
# get the helpers
source ${cur}/helpers
echo -e "\e[96m\e[1m==> RUNNING $(basename $BASH_SOURCE) <==\e[0m"
################################################################
# this is the test
################################################################
# the dotfile source
tmps=`mktemp -d --suffix='-dotdrop-tests'`
mkdir -p ${tmps}/dotfiles
#echo "dotfile source: ${tmps}"
# the dotfile destination
tmpd=`mktemp -d --suffix='-dotdrop-tests'`
#echo "dotfile destination: ${tmpd}"
# create the config file
cfg="${tmps}/config.yaml"
cat > ${cfg} << _EOF
config:
backup: true
create: true
dotpath: dotfiles
variables:
var1: "this is some test"
var2: 12
var3: another test
dotfiles:
f_abc:
dst: ${tmpd}/abc
src: abc
profiles:
p1:
dotfiles:
- f_abc
variables:
var1: "this is some sub-test"
p2:
include:
- p1
variables:
var2: 42
_EOF
#cat ${cfg}
# create the dotfile
echo "{{@@ var1 @@}}" > ${tmps}/dotfiles/abc
echo "{{@@ var2 @@}}" >> ${tmps}/dotfiles/abc
echo "{{@@ var3 @@}}" >> ${tmps}/dotfiles/abc
echo "test" >> ${tmps}/dotfiles/abc
# install
cd ${ddpath} | ${bin} install -f -c ${cfg} -p p1
grep '^this is some sub-test' ${tmpd}/abc >/dev/null
grep '^12' ${tmpd}/abc >/dev/null
grep '^another test' ${tmpd}/abc >/dev/null
# install
cd ${ddpath} | ${bin} install -f -c ${cfg} -p p2
grep '^this is some sub-test' ${tmpd}/abc >/dev/null
grep '^42' ${tmpd}/abc >/dev/null
grep '^another test' ${tmpd}/abc >/dev/null
#cat ${tmpd}/abc
## CLEANING
rm -rf ${tmps} ${tmpd}
echo "OK"
exit 0