123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185 |
- #!/bin/bash
- BACKEND="sqlite3"
- DB="$HOME/.ipfslinks.db"
- MYSQL_USER=""
- MYSQL_PASSWORD=""
- MYSQL_SCHEMA=""
- if [[ "$BACKEND" == "mysql" ]]
- then
- if [[ -z "$MYSQL_PASSWORD" ]]; then
- DB_COMMAND="mysql -u $MYSQL_USER -D $MYSQL_SCHEMA -e"
- else
- DB_COMMAND="mysql -u $MYSQL_USER -p $MYSQL_PASSWORD -D $MYSQL_SCHEMA -e"
- fi
- else
- DB_COMMAND="sqlite3 $DB"
- fi
- create_table() {
- if [[ "$BACKEND" == "mysql" ]]
- then
- $DB_COMMAND "
- CREATE TABLE IF NOT EXISTS ipfs_tracker (
- id INTEGER PRIMARY KEY AUTO_INCREMENT,
- description VARCHAR(512) NOT NULL,
- hash VARCHAR(46) NOT NULL,
- filetype VARCHAR(12) NOT NULL,
- tags MEDIUMTEXT
- );
- "
- else
- $DB_COMMAND "
- CREATE TABLE IF NOT EXISTS ipfs_tracker (
- id INTEGER PRIMARY KEY AUTOINCREMENT,
- description TEXT NOT NULL,
- hash TEXT NOT NULL,
- filetype TEXT NOT NULL,
- tags TEXT
- );
- "
- fi
- }
- insert() {
-
- insert_query="INSERT INTO ipfs_tracker (description, hash, filetype, tags)
- VALUES ('$1', '$2', '$3', '$4');"
- $DB_COMMAND "$insert_query"
- }
- search() {
-
- results="$($DB_COMMAND "SELECT description, hash, filetype FROM ipfs_tracker
- WHERE description LIKE '%$1%'
- OR tags LIKE '%$1%'")"
- if [[ -n "$results" ]]
- then
- echo "$results"
- else
- echo "Token '$1' not found."
- fi
- }
- delete() {
-
- $DB_COMMAND "DELETE FROM ipfs_tracker WHERE id=$1" ||
- echo "Error: entry not found."
- }
- show_help() {
- echo "$(basename $0) - an IPFS address bookmarker"
- echo "USAGE: $(basename $0) COMMAND"
- echo "Commands are: 'init', 'register', 'search', 'delete'"
- echo "Written by Klaus Zimmermann - https://quitter.se/kzimmermann"
- }
- case "$1" in
- "--help" | "-h" )
- show_help
- exit 0
- ;;
- "register" )
- title=""
- dag_hash=""
- filetype=""
- tags=""
- while [[ -z "$title" ]]
- do
- printf "Type a short description of the entry: "
- read title
- done
- while [[ -z "$dag_hash" ]]
- do
- printf "Enter the hash of the entry: "
- read dag_hash
- done
- while [[ -z "$filetype" ]]
- do
- printf "Enter the file type of the entry (jpg, gif, pdf, zip...): "
- read filetype
- done
- while [[ -z "$tags" ]]
- do
- printf "Type a few tags to aid the searching of this entry later: "
- read tags
- done
- insert "$title" "$dag_hash" "$filetype" "$tags" ||
- echo "Error registering '$title'"
- exit 0
- ;;
- "search" )
- printf "Enter a string to search for (empty for ALL): "
- read token
- search "$token"
- exit 0
- ;;
- "delete" )
- $DB_COMMAND "SELECT id, description FROM ipfs_tracker"
- printf "Enter the id you would like to delete: "
- read del_id
- delete "$del_id"
- exit 0
- ;;
- "init" )
- create_table && echo "All necessary tables initialized."
- exit 0
- ;;
- * )
- echo "Unknown option '$1'"
- show_help
- exit 1
- ;;
- esac
|