allow adding/switching collections - dossier - console collection manager
DIR Log
DIR Files
DIR Refs
DIR Tags
DIR README
DIR LICENSE
---
DIR commit 3e78a7de88f0fe245aeb084b52a8325832b1968e
DIR parent 9da2c6c29fe7d7b2c1b86bdccd5544e04b54913d
HTML Author: Solene Rapenne <solene@perso.pw>
Date: Wed, 18 Jul 2018 12:02:23 +0200
allow adding/switching collections
Diffstat:
M cdb | 64 +++++++++++++++++++++++++++++--
1 file changed, 60 insertions(+), 4 deletions(-)
---
DIR diff --git a/cdb b/cdb
@@ -1,13 +1,18 @@
#!/bin/sh
-: ${REPO:=collection}
-mkdir -p "${REPO}" || exit 1
-if [ ! -d "${REPO}/.git" ]
+mkdir -p ~/.collections
+test -f ~/.collections/context && . ~/.collections/context
+
+if [ -n "$CONTEXT" ]
then
+ REPO="${HOME}/.collections/${CONTEXT}/"
cd "$REPO"
- git init
+ test ! -d ".git" && git init
+else
+ printf 'No current collection in use\n'
fi
+
# displays the values of an identifier
# $1 identifier
show() {
@@ -183,12 +188,57 @@ usage() {
printf '%s\n' \
"cdb help" \
"cdb export" \
+ "cdb collections [register path name] [name]" \
"cdb show [identifier]" \
"cdb search [attribute [value]] ... [attribute [value]] ..." \
"cdb identifier attribute value ... attribute value ..."
exit 0
}
+switch() {
+ if [ ! -L "${HOME}/.collections/${1}" ]
+ then
+ printf 'Collection %s is not registered\n' "${1}"
+ exit 9
+ else
+ printf 'Switching to collection %s\n' "${1}"
+ printf 'CONTEXT=%s\n' $1 > ~/.collections/context
+ exit $?
+ fi
+}
+
+collections() {
+ if [ -n "$CONTEXT" ]
+ then
+ printf 'Currently using collection: %s\n' "${CONTEXT}"
+ else
+ printf 'No collection currently in use\n'
+ fi
+ printf 'Collections registerd in ~/.collections/:\n'
+ ls ~/.collections/ | grep -v 'context'
+ exit 0
+}
+
+# create symlink to register a collection
+# $1 absolute path to collection
+# $2 name of collection
+register() {
+ set -x
+ if [ -d "${1}" ]
+ then
+ if ! expr "${1}" : '^/'
+ then
+ printf 'Aborting, the path of the collection must be an absolute path. %s is not valid\n' "${1}"
+ fi
+ test -L "${HOME}/.collections/${2}" && rm "${HOME}/.collections/${2}"
+ ln -s "${1}" "${HOME}/.collections/${2}"
+ exit 0
+ else
+ printf 'Aborting, %s is not a directory\n' "${2}"
+ exit 8
+ fi
+}
+
if [ "$1" = "export" ] ; then export_csv ; fi
if [ "$1" = "rm" ] && [ "$#" -eq 2 ] ; then delete "$2" ; fi
if [ "$1" = "help" ] ; then usage ; fi
@@ -208,6 +258,12 @@ then
if [ "$#" -ge 3 ]; then search_value "$@" ; fi
fi
+if [ "$1" = "collections" ]; then
+ if [ "$#" -eq 1 ]; then collections ; fi
+ if [ "$#" -eq 2 ]; then switch "$2" ; fi
+ if [ "$2" = "register" ] && [ "$#" -eq 4 ]; then register "$3" "$4" ; fi
+fi
+
if [ "$#" -ge 3 ]; then add_value "$@" ; fi
usage