diff options
-rwxr-xr-x | repoman/bin/checkrepo | 49 | ||||
-rwxr-xr-x | server/lib/ogfunctions.sh | 65 |
2 files changed, 72 insertions, 42 deletions
diff --git a/repoman/bin/checkrepo b/repoman/bin/checkrepo index b38a689b..3a7448a2 100755 --- a/repoman/bin/checkrepo +++ b/repoman/bin/checkrepo @@ -1,9 +1,9 @@ #!/bin/bash #/** -# checkrepo #@file checkrepo -#@brief Generate repository information in a JSON file. +#@brief Maintain repository information in a JSON file. +#@usage checkrepo #@warning This script uses "jq" command. #@version 1.1.0 - Initial version. #@author Ramón M. Gómez - ETSII Univ. Sevilla @@ -18,15 +18,8 @@ IMAGESDIR=$OPENGNSYS/images INFOFILE=$OPENGNSYS/etc/repoinfo.json -# Auxiliar functions. - -# Metafunction to check if JSON result exists. -function jq() { - local OUTPUT - OUTPUT=$($JQ "$@") || return $? - [[ "$OUTPUT" = "null" ]] && return 1 - echo "$OUTPUT" -} +# Functions. +source $OPENGNSYS/lib/ogfunctions.sh # Create/edit JSON file about installed ogLive clients. function addToJson() { @@ -105,7 +98,7 @@ EOT # Create new JSON file. if [ -z "$OUNAME" ]; then cat << EOT | jq . > $INFOFILE -{"directory":"$IMAGESDIR","images":[$JSON],"ous":[]} +{"directory":"$IMAGESDIR","images":[${IMAGENAME+$JSON}],"ous":[]} EOT else cat << EOT | jq . > $INFOFILE @@ -115,34 +108,6 @@ EOT fi } -# Show an error message. -function raiseError() { - case "$1" in - usage) - echo "$PROG: Usage error: Type \"$PROG help\"" >&2 - exit 1 ;; - notfound) - echo "$PROG: Resource not found: $2" >&2 - exit 2 ;; - access) - echo "$PROG: Access error: $2" >&2 - exit 3 ;; - *) - echo "$PROG: Unknown error" >&2 - exit 1 ;; - esac -} - -# Command functions. - -# Show help message. -function help() { - cat << EOT -$PROG: maintain the repository information. -Usage: $PROG -EOT -} - # Check for file-based images to update the repository configuration file. function checkfiles() { local IMAGES IMG INFO DATA @@ -220,8 +185,8 @@ function checkremoved() { # Main progrram. # Check dependencies. -[ ! -w "$(dirname "$INFOFILE")" ] && raiseError access "$INFOFILE" -JQ=$(which jq 2>/dev/null) || raiseError notfound "Need to install \"jq\"." +[ -w "$(dirname "$INFOFILE")" ] || raiseError access "$INFOFILE" +[ -e "$INFOFILE" ] || addToJson which sponge &>/dev/null || raiseError notfound "Need to install \"moreutils\"." checkfiles diff --git a/server/lib/ogfunctions.sh b/server/lib/ogfunctions.sh new file mode 100755 index 00000000..51de6b77 --- /dev/null +++ b/server/lib/ogfunctions.sh @@ -0,0 +1,65 @@ +#!/bin/bash +#/** +#@file ogfunctions.sh +#@brief Generic functions for OpenGnsys Server and OpenGnsys Repository. +#@version 1.1.1 - Initial version +#@author Ramón M. Gómez, ETSII Universidad de Sevilla +#@date 2017-10-08 +#*/ + + +# Showing an error message. +function raiseError() { + case "$1" in + usage) + echo "$PROG: Usage error: Type \"$PROG help\"" >&2 + exit 1 ;; + notfound) + echo "$PROG: Resource not found: $2" >&2 + exit 2 ;; + access) + echo "$PROG: Access error: $2" >&2 + exit 3 ;; + download) + echo "$PROG: Download error: $2" >&2 + exit 4 ;; + *) + echo "$PROG: Unknown error" >&2 + exit 1 ;; + esac +} + +# Showing help message. +function help() { + [ -n "$1" ] && DESCRIPTION="$1" || DESCRIPTION=$(grep "^#@brief" "$0" | cut -f2- -d" ") + shift + if [ -n "$1" ]; then + USAGE="$1" + shift + else + USAGE=$(grep "^#@usage" "$0" | cut -f2- -d" ") + [ -n "$USAGE" ] && PARAMS=$(awk '$1=="#@param" {sub($1,""); print "\t",$0}' "$0") + fi + # Showing help. + echo "$PROG: ${DESCRIPTION:-"no description"}" + echo "Usage: ${USAGE:-"no usage info"}" + [ -n "$PARAMS" ] && echo -e "$PARAMS" + if [ -n "$*" ]; then + echo "Examples:" + while (( "$#" )); do + echo -e "\t$1" + shift + done + fi + exit 0 +} + +# Metafunction to check if JSON result exists. +JQ=$(which jq 2>/dev/null) || raiseError notfound "Need to install \"jq\"." +function jq() { + local OUTPUT + OUTPUT=$($JQ "$@") || return $? + [[ "$OUTPUT" = "null" ]] && return 1 + echo "$OUTPUT" +} + |