deploy: possibilité de filtrer les variables à afficher. en cas d'échec, afficher false pour faciliter l'utilisation avec eval()

This commit is contained in:
Jephté Clain 2017-03-09 06:39:33 +04:00
parent 8fa58e24e3
commit d5846bab1d
1 changed files with 98 additions and 31 deletions

View File

@ -7,32 +7,69 @@
function deploy_query() { function deploy_query() {
# Afficher le résultat d'une requête # Afficher le résultat d'une requête
local alv
eval "set -- $(getopt -ov: -lallow-vars: -- "$@")"
while [ -n "$1" ]; do
case "$1" in
-v|--allow-vars)
array_split alv "$2" ,
shift; shift
;;
--) shift; break;;
*) break;;
esac
done
local qtype="$1" otype="$2" ltype="$3" profile="$4" format="${5:-shell}" term="$6" local qtype="$1" otype="$2" ltype="$3" profile="$4" format="${5:-shell}" term="$6"
local -a vs; local object type link local -a vs; local object type link
case "$qtype" in case "$qtype" in
DEST|SOURCE) DEST|SOURCE)
if [ -n "$otype" ]; then deploy_is_type "$otype" || return 2; fi if [ -n "$otype" ]; then
deploy_is_type "$otype" || {
qvals false "$otype: invalid type"
return 2
}
fi
;; ;;
*) *)
if [ -n "$qtype" ]; then deploy_is_type "$qtype" || return 3; fi if [ -n "$qtype" ]; then
deploy_is_type "$qtype" || {
qvals false "$qtype: invalid type"
return 3
}
fi
;; ;;
esac esac
if [ -n "$ltype" ]; then deploy_is_link "$ltype" || return 4; fi if [ -n "$ltype" ]; then
[ -n "$term" ] || return 5 deploy_is_link "$ltype" || {
qvals false "$ltype: invalid link type"
return 4
}
fi
[ -n "$term" ] || {
qvals false "search term required"
return 5
}
case "$qtype" in case "$qtype" in
DEST) DEST)
edebug "=== search type DEST" edebug "=== search type DEST"
edebug "term=$term, otype=$otype" edebug "term=$term, otype=$otype"
deploy_search_artifact object type "$term" "$otype" host || return 1 deploy_search_artifact object type "$term" "$otype" host || {
qvals false "artifact not found"
return 1
}
edebug "object=$object, type=$type" edebug "object=$object, type=$type"
deploy_search_link link "$type" "$ltype" || return 1 deploy_search_link link "$type" "$ltype" || {
qvals false "link not found"
return 1
}
deploy_show_values object object "$format" shell deploy_show_values object object "$format" shell alv
deploy_show_values type otype "$format" shell deploy_show_values type otype "$format" shell alv
deploy_show_values link ltype "$format" shell deploy_show_values link ltype "$format" shell alv
deploy_show_links "$object" "$link" "$profile" "$format" deploy_show_links "$object" "$link" "$profile" "$format" "" alv
return 0 return 0
;; ;;
SOURCE) SOURCE)
@ -40,35 +77,42 @@ function deploy_query() {
return 0 return 0
;; ;;
*) *)
if deploy_search_artifact object type "$term" "$qtype"; then if deploy_search_artifact object type "$term" "$qtype"; then
deploy_copy_object_values vs "$object" "$type" deploy_copy_object_values vs "$object" "$type"
deploy_show_values object object "$format" shell deploy_show_values object object "$format" shell alv
deploy_show_values type otype "$format" shell deploy_show_values type otype "$format" shell alv
deploy_show_values vs "$type" "$format" deploy_show_values vs "$type" "$format" "" alv
deploy_show_attrs "$object" "$type" "$format" deploy_show_attrs "$object" "$type" "$format" alv
return 0 return 0
fi fi
qvals false "artifact not found"
return 1 return 1
;; ;;
esac esac
} }
function deploy_setconf() { function deploy_setconf() {
local confname="$1" confdir="$(abspath "${2:-$scriptdir}")" local confname="$1" confdir="$2"
IFILE="$confdir/init.conf" if [ -n "$confdir" ]; then
if [[ "$confname" == */* ]] || [[ "$confname" == *.* ]]; then confdir="$(abspath "$confdir")"
local d="$(abspath "$(dirname -- "$confname")")" if [[ "$confname" == */* ]] || [[ "$confname" == *.* ]]; then
if [ "$d" != "$confdir" ]; then local d="$(abspath "$(dirname -- "$confname")")"
CONFNAME="$confname" if [ "$d" != "$confdir" ]; then
SFILE="$CONFNAME" CONFNAME="$confname"
DFILE="${CONFNAME%.conf}.vars" SFILE="$CONFNAME"
return DFILE="${CONFNAME%.conf}.vars"
return
fi
confname="$(basename -- "$confname" .conf)"
fi fi
confname="$(basename -- "$confname" .conf)" CONFNAME="${confname:-deploy}"
IFILE="$confdir/init.conf"
SFILE="$confdir/$CONFNAME.conf"
else
CONFNAME="${confname:-deploy}"
IFILE=
SFILE=
fi fi
CONFNAME="${confname:-deploy}"
SFILE="$confdir/$CONFNAME.conf"
if is_root; then if is_root; then
DFILE="/var/local/deploy/$CONFNAME.vars" DFILE="/var/local/deploy/$CONFNAME.vars"
else else
@ -77,6 +121,7 @@ function deploy_setconf() {
} }
function deploy_loadconf() { function deploy_loadconf() {
# charger la configuration. deploy_setconf() DOIT être appelé avant
if [ "$1" != --no-auto-update ]; then ( if [ "$1" != --no-auto-update ]; then (
# mettre à jour le cas échéant le fichier des variables # mettre à jour le cas échéant le fichier des variables
DEPLOY_SRCDIR=; set_defaults deploy DEPLOY_SRCDIR=; set_defaults deploy
@ -168,19 +213,31 @@ function deploy_search_link() {
} }
function deploy_show_values() { function deploy_show_values() {
if [ -n "$5" ]; then
if ! array_isempty "$5"; then
array_contains "$5" "$2" || return
fi
fi
local -a __sv_vs; array_copy __sv_vs "$1" local -a __sv_vs; array_copy __sv_vs "$1"
local -a vs; array_copy vs __sv_vs local -a vs; array_copy vs __sv_vs
local t="$2" f="${3:-shell}" ff="$4" local t="$2" f="${3:-shell}" ff="$4"
[ -n "$ff" -a "$f" != "$ff" ] && return [ -n "$ff" -a "$f" != "$ff" ] && return
case "$f" in case "$f" in
shell) deploy_dump_values "$t" vs;; shell)
[ -n "$5" ] && echo "$t=()"
deploy_dump_values "$t" vs
;;
line) array_to_lines vs;; line) array_to_lines vs;;
*) array_join vs "$f";; *) array_join vs "$f";;
esac esac
} }
function deploy_show_attrs() { function deploy_show_attrs() {
if [ -n "$4" ]; then
local -a __dsa_alv; array_copy __dsa_alv "$4"
local -a alv; array_copy alv __dsa_alv
fi
local o="$1" t="$2" f="${3:-shell}" ff=shell local o="$1" t="$2" f="${3:-shell}" ff=shell
[ -n "$ff" -a "$f" != "$ff" ] && return [ -n "$ff" -a "$f" != "$ff" ] && return
@ -188,12 +245,22 @@ function deploy_show_attrs() {
deploy_prepare_copy_attrs ons gns "$o" "$t" deploy_prepare_copy_attrs ons gns "$o" "$t"
deploy_copy_attr_names ans "$o" "$t" ons gns deploy_copy_attr_names ans "$o" "$t" ons gns
for an in "${ans[@]}"; do for an in "${ans[@]}"; do
if [ -n "$4" ]; then
if ! array_isempty "$4"; then
array_contains alv "$an" || continue
fi
[ "$f" == shell ] && echo "$an=()"
fi
deploy_copy_attr_values vs "$an" "$o" "$t" ons deploy_copy_attr_values vs "$an" "$o" "$t" ons
deploy_dump_values "$an" vs deploy_dump_values "$an" vs
done done
} }
function deploy_show_links() { function deploy_show_links() {
if [ -n "$6" ]; then
local -a __dsl_alv; array_copy __dsl_alv "$6"
local -a alv; array_copy alv __dsl_alv
fi
local o="$1" l="$2" profile="$3" f="${4:-shell}" ff="$5" local o="$1" l="$2" profile="$3" f="${4:-shell}" ff="$5"
local -a ps pons pgns ons gns ans ovs os; local p an av o local -a ps pons pgns ons gns ans ovs os; local p an av o
[ -n "$ff" -a "$f" != "$ff" ] && return [ -n "$ff" -a "$f" != "$ff" ] && return
@ -201,7 +268,7 @@ function deploy_show_links() {
deploy_copy_profiles ps 1 deploy_copy_profiles ps 1
for p in "${ps[@]}"; do for p in "${ps[@]}"; do
[ -n "$profile" -a "$p" != "$profile" ] && continue [ -n "$profile" -a "$p" != "$profile" ] && continue
deploy_show_values p profile "$format" shell deploy_show_values p profile "$format" shell alv
deploy_prepare_copy_link_attrs pons pgns ons gns "$o" "$l" "$p" deploy_prepare_copy_link_attrs pons pgns ons gns "$o" "$l" "$p"
deploy_copy_link_attr_names ans "$o" "$l" "$p" pons pgns ons gns deploy_copy_link_attr_names ans "$o" "$l" "$p" pons pgns ons gns
@ -215,9 +282,9 @@ function deploy_show_links() {
deploy_copy_object_values os "$av" host deploy_copy_object_values os "$av" host
array_extendu ovs os array_extendu ovs os
done done
deploy_show_values ovs host "$f" "$ff" deploy_show_values ovs host "$f" "$ff" alv
else else
deploy_show_values avs "$an" "$f" shell deploy_show_values avs "$an" "$f" shell alv
fi fi
done done
done done