lot of work - dossier - console collection manager DIR Log DIR Files DIR Refs DIR Tags DIR README DIR LICENSE --- DIR commit 8c7a95569085ede05744318afe3212f4900aca2c DIR parent 3bf0b90aa2f8f10172f97fcc092d990407e7bf22 HTML Author: Solene Rapenne <solene@perso.pw> Date: Sat, 14 Jul 2018 21:48:15 +0200 lot of work Diffstat: M cdb | 106 ++++++++++++++++++++++++++----- 1 file changed, 90 insertions(+), 16 deletions(-) --- DIR diff --git a/cdb b/cdb @@ -2,48 +2,122 @@ REPO=/home/solene/dev/cbd/games/ - +# displays the values of an identifier +# $1 identifier show() { cd "${REPO}" + SEEN=0 + for attribute in * + do + if [ -f "${attribute}/${1}" ] + then + if [ "$SEEN" -eq 0 ] + then + printf "%s:\n" "$1" + SEEN=1 + fi + printf "%15s: %s\n" ${attribute} "$(cat "${attribute}/${1}")" + fi + done + if [ "$SEEN" -eq 1 ] + then + exit 0 + else + printf '%s is not in the library.\n' "$1" + exit 1 + fi +} + +# delete identifier from attributes +# $1 identifier +delete() { + cd "${REPO}" for attribute in * do if [ -f "${attribute}/${1}" ] then - printf "%15s: %s\n" ${attribute} "$(cat ${attribute}/${1})" + rm "${attribute}/${1}" fi done + exit 0 +} + +# displays list of identifiers +show_list() { + cd "${REPO}" + find . -type f | cut -d '/' -f 3 | sort | uniq -c | \ + awk '{ for(i=2;i<=NF;i=i+1) { printf "%s ", $i } + printf "(%i)\n", $1 + }' + exit 0 } +# displays attributes used show_attributes() { cd "${REPO}" - ( - for attribute in $(find . -type d -maxdepth 1 -mindepth 1) - do - printf "${attribute}" | cut -d '/' -f 2 - done - ) | sort | tr '\n' ' ' + find . -type f | cut -d '/' -f 2 | sort | uniq -c | \ + awk '{ for(i=2;i<=NF;i=i+1) { printf "%s ", $i } + printf "(%i)\n", $1 + }' exit 0 } -# $1 identifier -# $2 attribute -# $3 value +# add/modify a value +# $@ identifier / attr / value / attr / value / .... +# shift to have attr / value again and again add_value() { cd "$REPO" - mkdir -p "$2" - printf '%s' "$3" > "$2"/"$1" + ID="$1" + shift + + while [ "$#" -gt 1 ] + do + ATTRIBUTE="$1" + VALUE="$2" + shift 2 + + mkdir -p "$ATTRIBUTE" + printf '%s' "$VALUE" > "$ATTRIBUTE"/"${ID}" + done exit 0 } +# returns identifiers having attribute=value # $1 attribute # $2 value search_value() { cd "$REPO" - grep -r "$2" "$1" + grep -rl "$2" "$1" | cut -d '/' -f 2 exit 0 } +# returns list of identifiers in a attribute +# $1 attribute +list() { + cd "$REPO" + grep -r . "$1" | cut -d '/' -f 2- | sort + exit 0 +} + +# displays usage +usage() { + printf '%s\n' \ + "cdb help" \ + "cdb ls" \ + "cdb ls attribute" \ + "cdb identifier" \ + "cdb attributes" \ + "cdb search attribute value" \ + "cdb identifier attribute value" + exit 0 +} + +if [ "$1" = "rm" ] && [ "$#" -eq 2 ] ; then delete "$2" ; fi +if [ "$1" = "ls" ] && [ "$#" -eq 2 ] ; then list "$2" ; fi +if [ "$1" = "help" ] ; then usage ; fi +if [ "$1" = "ls" ] ; then show_list ; fi if [ "$1" = "search" ] && [ "$#" -eq 3 ]; then search_value "$2" "$3" ; fi if [ "$1" = "attrs" ]; then show_attributes ; fi -if [ "$#" -eq 3 ]; then add_value "$1" "$2" "$3" ; fi -show "$1" +if [ "$#" -ge 3 ]; then add_value $@ ; fi +if [ "$#" -eq 1 ]; then show "$1" ; fi +usage