mirror of
https://github.com/deadc0de6/dotdrop.git
synced 2026-02-16 13:26:11 +00:00
action with arguments for #59
This commit is contained in:
@@ -32,12 +32,23 @@ class Cmd:
|
|||||||
|
|
||||||
class Action(Cmd):
|
class Action(Cmd):
|
||||||
|
|
||||||
|
def __init__(self, key, action, *args):
|
||||||
|
super(Action, self).__init__(key, action)
|
||||||
|
self.args = args
|
||||||
|
|
||||||
def execute(self):
|
def execute(self):
|
||||||
"""execute the action in the shell"""
|
"""execute the action in the shell"""
|
||||||
ret = 1
|
ret = 1
|
||||||
self.log.sub('executing \"{}\"'.format(self.action))
|
|
||||||
try:
|
try:
|
||||||
ret = subprocess.call(self.action, shell=True)
|
cmd = self.action.format(*self.args)
|
||||||
|
except IndexError:
|
||||||
|
err = 'bad action: \"{}\"'.format(self.action)
|
||||||
|
err += ' with \"{}\"'.format(self.args)
|
||||||
|
self.log.warn(err)
|
||||||
|
return False
|
||||||
|
self.log.sub('executing \"{}\"'.format(cmd))
|
||||||
|
try:
|
||||||
|
ret = subprocess.call(cmd, shell=True)
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
self.log.warn('action interrupted')
|
self.log.warn('action interrupted')
|
||||||
return ret == 0
|
return ret == 0
|
||||||
|
|||||||
@@ -258,16 +258,29 @@ class Cfg:
|
|||||||
self.key_actions_pre: [],
|
self.key_actions_pre: [],
|
||||||
self.key_actions_post: [],
|
self.key_actions_post: [],
|
||||||
}
|
}
|
||||||
for entry in entries:
|
for line in entries:
|
||||||
|
fields = line.split(' ')
|
||||||
|
entry = fields[0]
|
||||||
|
args = []
|
||||||
|
if len(fields) > 1:
|
||||||
|
args = fields[1:]
|
||||||
action = None
|
action = None
|
||||||
if self.key_actions_pre in self.actions and \
|
if self.key_actions_pre in self.actions and \
|
||||||
entry in self.actions[self.key_actions_pre]:
|
entry in self.actions[self.key_actions_pre]:
|
||||||
key = self.key_actions_pre
|
key = self.key_actions_pre
|
||||||
|
if not args:
|
||||||
action = self.actions[self.key_actions_pre][entry]
|
action = self.actions[self.key_actions_pre][entry]
|
||||||
|
else:
|
||||||
|
a = self.actions[self.key_actions_pre][entry].action
|
||||||
|
action = Action(key, a, *args)
|
||||||
elif self.key_actions_post in self.actions and \
|
elif self.key_actions_post in self.actions and \
|
||||||
entry in self.actions[self.key_actions_post]:
|
entry in self.actions[self.key_actions_post]:
|
||||||
key = self.key_actions_post
|
key = self.key_actions_post
|
||||||
|
if not args:
|
||||||
action = self.actions[self.key_actions_post][entry]
|
action = self.actions[self.key_actions_post][entry]
|
||||||
|
else:
|
||||||
|
a = self.actions[self.key_actions_post][entry].action
|
||||||
|
action = Action(key, a, *args)
|
||||||
else:
|
else:
|
||||||
self.log.warn('unknown action \"{}\"'.format(entry))
|
self.log.warn('unknown action \"{}\"'.format(entry))
|
||||||
continue
|
continue
|
||||||
|
|||||||
113
tests-ng/actions-args.sh
Executable file
113
tests-ng/actions-args.sh
Executable file
@@ -0,0 +1,113 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
# author: deadc0de6 (https://github.com/deadc0de6)
|
||||||
|
# Copyright (c) 2017, deadc0de6
|
||||||
|
#
|
||||||
|
# test pre/post/naked actions with arguments
|
||||||
|
# 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 "RUNNING $(basename $BASH_SOURCE)"
|
||||||
|
|
||||||
|
################################################################
|
||||||
|
# this is the test
|
||||||
|
################################################################
|
||||||
|
|
||||||
|
# the action temp
|
||||||
|
tmpa=`mktemp -d`
|
||||||
|
# the dotfile source
|
||||||
|
tmps=`mktemp -d`
|
||||||
|
mkdir -p ${tmps}/dotfiles
|
||||||
|
# the dotfile destination
|
||||||
|
tmpd=`mktemp -d`
|
||||||
|
|
||||||
|
# create the config file
|
||||||
|
cfg="${tmps}/config.yaml"
|
||||||
|
|
||||||
|
cat > ${cfg} << _EOF
|
||||||
|
actions:
|
||||||
|
pre:
|
||||||
|
preaction: echo '{0} {1}' > ${tmpa}/pre
|
||||||
|
post:
|
||||||
|
postaction: echo '{0} {1} {2}' > ${tmpa}/post
|
||||||
|
nakedaction: echo '{0}' > ${tmpa}/naked
|
||||||
|
emptyaction: echo 'empty' > ${tmpa}/empty
|
||||||
|
config:
|
||||||
|
backup: true
|
||||||
|
create: true
|
||||||
|
dotpath: dotfiles
|
||||||
|
dotfiles:
|
||||||
|
f_abc:
|
||||||
|
dst: ${tmpd}/abc
|
||||||
|
src: abc
|
||||||
|
actions:
|
||||||
|
- preaction test1 test2
|
||||||
|
- postaction test3 test4 test5
|
||||||
|
- nakedaction test6
|
||||||
|
- emptyaction
|
||||||
|
profiles:
|
||||||
|
p1:
|
||||||
|
dotfiles:
|
||||||
|
- f_abc
|
||||||
|
_EOF
|
||||||
|
cat ${cfg}
|
||||||
|
|
||||||
|
# create the dotfile
|
||||||
|
echo "test" > ${tmps}/dotfiles/abc
|
||||||
|
|
||||||
|
# install
|
||||||
|
cd ${ddpath} | ${bin} install -f -c ${cfg} -p p1
|
||||||
|
|
||||||
|
# checks
|
||||||
|
[ ! -e ${tmpa}/pre ] && echo "pre arg action not found" && exit 1
|
||||||
|
grep test1 ${tmpa}/pre >/dev/null
|
||||||
|
grep test2 ${tmpa}/pre >/dev/null
|
||||||
|
|
||||||
|
[ ! -e ${tmpa}/post ] && echo "post arg action not found" && exit 1
|
||||||
|
grep test3 ${tmpa}/post >/dev/null
|
||||||
|
grep test4 ${tmpa}/post >/dev/null
|
||||||
|
grep test5 ${tmpa}/post >/dev/null
|
||||||
|
|
||||||
|
[ ! -e ${tmpa}/naked ] && echo "naked arg action not found" && exit 1
|
||||||
|
grep test6 ${tmpa}/naked >/dev/null
|
||||||
|
|
||||||
|
[ ! -e ${tmpa}/empty ] && echo "empty arg action not found" && exit 1
|
||||||
|
grep empty ${tmpa}/empty >/dev/null
|
||||||
|
|
||||||
|
## CLEANING
|
||||||
|
rm -rf ${tmps} ${tmpd} ${tmpa}
|
||||||
|
|
||||||
|
echo "OK"
|
||||||
|
exit 0
|
||||||
Reference in New Issue
Block a user