From 22acc1b990ac7d97c16344fbcbc2621e24cdf915 Mon Sep 17 00:00:00 2001 From: tv Date: Sun, 20 Dec 2015 20:59:16 +0100 Subject: refactor --- cac | 76 ++++++++++++++++++++++++++++++++++----------------------------------- 1 file changed, 38 insertions(+), 38 deletions(-) diff --git a/cac b/cac index b9982b2..68513ff 100755 --- a/cac +++ b/cac @@ -13,8 +13,9 @@ cac_resources_cache=${cac_resources_cache-$HOME/tmp/cac_resources_cache.json} cac_servers_cache=${cac_servers_cache-$HOME/tmp/cac_servers_cache.json} cac_tasks_cache=${cac_tasks_cache-$HOME/tmp/cac_tasks_cache.json} cac_templates_cache=${cac_templates_cache-$HOME/tmp/cac_templates_cache.json} -update_interval=${update_interval-10} cac_secrets=${cac_secrets-$HOME/.secrets/cac} +cac_always_update=${cac_always_update-false} +cac_update_interval=${cac_update_interval-10} _load_secrets() { if test ! -r "$cac_secrets" ;then @@ -26,11 +27,13 @@ _load_secrets() { fi } - -cac() { - __cac_cli__command=${1-help} - shift || : - __cac_cli__"$__cac_cli__command" "$@" +_cac_main() { + _load_secrets + case ${run-true} in true) + __cac_cli__command=${1-help} + shift || : + __cac_cli__"$__cac_cli__command" "$@" + esac } #? cac help [REGEX] @@ -77,18 +80,11 @@ __cac_cli__console() {( _cac_post_api_v1 console sid="$sid" | jq -r .console )} -_maybe_update(){ - - if test ! -r "$1" -o -n "${always_update:-}" ;then - echo "$(date -Is) updating cache file" >&2 - __cac_cli__update - fi -} #? cac servers #? Print cached servers JSON. #? __cac_cli__servers() { - _maybe_update $cac_servers_cache + _cac_maybe_update jq -r . $cac_servers_cache } @@ -96,7 +92,7 @@ __cac_cli__servers() { #? Print cached tasks JSON. #? __cac_cli__tasks() { - _maybe_update $cac_tasks_cache + _cac_maybe_update jq -r . $cac_tasks_cache } @@ -104,7 +100,7 @@ __cac_cli__tasks() { #? Print cached templates JSON. #? __cac_cli__templates() { - _maybe_update $cac_templates_cache + _cac_maybe_update jq -r . $cac_templates_cache } @@ -112,7 +108,7 @@ __cac_cli__templates() { #? Print CloudPRO resources JSON. #? __cac_cli__resources() { - _maybe_update $cac_resources_cache + _cac_maybe_update jq -r . $cac_resources_cache } @@ -144,7 +140,7 @@ __cac_cli__update() {( #? Print cached server JSON. #? __cac_cli__getserver() {( - + _cac_maybe_update case $1 in *:*) k=${1%%:*} @@ -155,7 +151,6 @@ __cac_cli__getserver() {( v=${1#*:} ;; esac - _maybe_update $cac_servers_cache if result=$(jq \ -e \ --arg k "$k" \ @@ -277,15 +272,26 @@ __cac_cli__waitstatus() { ;; esac - # always_update is not compatible with inotifywait - if test -z "${always_update:-}";then - echo "$(date -Is) Waiting for status: $2; current status: $status ..." >&2 - __cac_cli__waitforcacheupdate __cac_cli__waitstatus "$@" - else - echo "$(date -Is) Waiting ${update_interval}secs for status: $2; current status: $status ..." - sleep ${update_interval} - __cac_cli__waitstatus "$@" - fi + # cac_always_update is not compatible with inotifywait + case $cac_always_update in + true) + echo "$(date -Is) Waiting ${cac_update_interval}s for status: $2; current status: $status ..." + sleep ${cac_update_interval} + __cac_cli__waitstatus "$@" + ;; + false) + echo "$(date -Is) Waiting for status: $2; current status: $status ..." >&2 + __cac_cli__waitforcacheupdate __cac_cli__waitstatus "$@" + ;; + esac +} + +# helper to implement cac_always_update +_cac_maybe_update() { + case $cac_always_update in true) + echo "$(date -Is) updating cache file" >&2 + __cac_cli__update + esac } @@ -349,8 +355,9 @@ __cac_cli__delete() {( #? cac_tasks_cache=$HOME/tmp/cac_tasks_cache.json #? cac_templates_cache=$HOME/tmp/cac_templates_cache.json #? cac_secrets=$HOME/.secrets/cac -#? run=true < set to false to be able to source this file -#? always_update= < set to true to always update when using cache files +#? cac_always_update=false < set to true to always update when using cache files +#? cac_update_interval=10 < interval (in s) used by `cac waitstatus` +#? run=true < set to false to be able to source this file #? #? You can override these by setting them beforehand. #? @@ -461,13 +468,6 @@ _cac_exec() { fi } -_main() { - _load_secrets || exit 1 - case ${run-true} in - true) cac "$@";; - esac -} - # # imported: urlencode() { @@ -533,4 +533,4 @@ rsyncfiles() {( # -_main "$@" +_cac_main "$@" -- cgit v1.2.3