add csv export - dossier - console collection manager DIR Log DIR Files DIR Refs DIR Tags DIR README DIR LICENSE --- DIR commit 93f007a2d82cf9b5dd1068a99f74f290385b7132 DIR parent bce0d7478586d1b7c226b321a9d8be6cc56d9034 HTML Author: Solene Rapenne <solene@perso.pw> Date: Sat, 14 Jul 2018 22:37:20 +0200 add csv export Diffstat: M cdb | 60 +++++++++++++++++++++++++++---- 1 file changed, 53 insertions(+), 7 deletions(-) --- DIR diff --git a/cdb b/cdb @@ -28,6 +28,43 @@ show() { fi } +# export the data in csv format "data","data","data" +# we assume it'll works with the dataset +export_csv() { + cd "${REPO}" + IDS=$(find . -type f | cut -d '/' -f 3 | sort | uniq | paste -s -d '\x0' -) + ATTRS=$(find . -type f | cut -d '/' -f 2 | sort | uniq | paste -s -d '\x0' -) + + OLDIFS=$IFS + IFS=''\x0 + + # display header + printf '"identifier",' + for attr in $ATTRS + do + printf '"%s",' $attr + done + printf '\n' + + # print database + for id in $IDS + do + printf '"%s",' "$id" + for attr in $ATTRS + do + if [ -f "${attr}/${id}" ] + then + printf '"%s",' "$(cat "${attr}/${id}")" + else + printf '"",' + fi + done + printf '\n' + done + IFS=$OLDIFS + exit 0 +} + # delete identifier from attributes # $1 identifier delete() { @@ -37,6 +74,7 @@ delete() { if [ -f "${attribute}/${1}" ] then rm "${attribute}/${1}" + rmdir "${attribute}" 2> /dev/null else printf "%s is not in the library!\n" "$1" exit 1 @@ -106,23 +144,31 @@ list() { usage() { printf '%s\n' \ "cdb help" \ + "cdb export" \ "cdb show [identifier]" \ "cdb search [attribute [value]]" \ - "cdb identifier attribute value" + "cdb identifier attribute value ... attribute value ..." exit 0 } +if [ "$1" = "export" ] ; then export_csv ; fi if [ "$1" = "rm" ] && [ "$#" -eq 2 ] ; then delete "$2" ; fi -if [ "$1" = "help" ] ; then usage ; fi +if [ "$1" = "help" ] ; then usage ; fi # dealing with identifiers -if [ "$1" = "show" ] && [ "$#" -eq 1 ]; then show_list ; fi -if [ "$1" = "show" ] && [ "$#" -eq 2 ]; then show "$2" ; fi +if [ "$1" = "show" ] +then + if [ "$#" -eq 1 ]; then show_list ; fi + if [ "$#" -eq 2 ]; then show "$2" ; fi +fi # dealing with attributes -if [ "$1" = "search" ] && [ "$#" -eq 1 ]; then show_attributes ; fi -if [ "$1" = "search" ] && [ "$#" -eq 2 ]; then list "$2" ; fi -if [ "$1" = "search" ] && [ "$#" -eq 3 ]; then search_value "$2" "$3" ; fi +if [ "$1" = "search" ]; +then + if [ "$#" -eq 1 ]; then show_attributes ; fi + if [ "$#" -eq 2 ]; then list "$2" ; fi + if [ "$#" -eq 3 ]; then search_value "$2" "$3" ; fi +fi if [ "$#" -ge 3 ]; then add_value "$@" ; fi