Make it do something
- Add constant `*version*' which contains the version number of the project. - Change `db-connect' to `check-db'. Update to using sqlite functions instead of cl-sql ones. - Move the bookmark collection loop to `get-bookmarks'. - When starting up, if the length of args is greater than 1 check if the given option is a known "command" and if so, call it with the command line arguments (minus the first). Otherwise show all the bookmarks.
This commit is contained in:
parent
940d990418
commit
6d1a3b02e0
2 changed files with 45 additions and 19 deletions
62
clark.lisp
62
clark.lisp
|
@ -5,14 +5,29 @@
|
||||||
(defvar *db* nil
|
(defvar *db* nil
|
||||||
"The database connection.")
|
"The database connection.")
|
||||||
|
|
||||||
(defun db-connect (name)
|
(defconstant *version* "0.1.0"
|
||||||
|
"Clark's version.")
|
||||||
|
|
||||||
|
(defun check-db (name)
|
||||||
"Connect to the database, possibly creating it."
|
"Connect to the database, possibly creating it."
|
||||||
(let ((db-exists (probe-file name)))
|
(let ((db-exists (probe-file name)))
|
||||||
(connect (list name) :database-type :sqlite3)
|
(setf *db* (connect name))
|
||||||
(unless db-exists
|
(unless db-exists
|
||||||
(create-view-from-class 'bookmark)
|
(execute-non-query *db* "CREATE TABLE bookmark (url VARCHAR(255) UNIQUE, date INTEGER, name VARCHAR(255), description TEXT)")
|
||||||
(create-view-from-class 'tag)
|
(execute-non-query *db* "CREATE TABLE tag (name VARCHAR(255) UNIQUE);")
|
||||||
(create-view-from-class 'bookmark-tag))))
|
(execute-non-query *db* "CREATE TABLE bookmark_tag (bookmark_id INTEGER REFERENCES bookmark(rowid), tag_id INTEGER REFERENCES tag(rowid), PRIMARY KEY (bookmark_id, tag_id))"))))
|
||||||
|
|
||||||
|
(defun get-bookmarks ()
|
||||||
|
"Get a list of all bookmarks.
|
||||||
|
|
||||||
|
The result contains the url and the name of the bookmark."
|
||||||
|
(let ((statement
|
||||||
|
(prepare-statement *db* "select url, name from bookmark")))
|
||||||
|
(loop
|
||||||
|
while (step-statement statement)
|
||||||
|
collect (list (statement-column-value statement 0)
|
||||||
|
(statement-column-value statement 1))
|
||||||
|
finally (finalize-statement statement))))
|
||||||
|
|
||||||
(defun print-bookmark (bm)
|
(defun print-bookmark (bm)
|
||||||
"Print information about bookmark BM.
|
"Print information about bookmark BM.
|
||||||
|
@ -21,19 +36,30 @@ BM should be a list containing the url and name of the bookmark."
|
||||||
(destructuring-bind (url name) bm
|
(destructuring-bind (url name) bm
|
||||||
(format t "~A~%~A~%~%" url name)))
|
(format t "~A~%~A~%~%" url name)))
|
||||||
|
|
||||||
(defun get-bookmarks ()
|
(defun make-command-name (base)
|
||||||
"Get a list of all bookmarks.
|
"Turn BASE into the name of a possible command."
|
||||||
|
(concatenate 'string (string-upcase base) "-COMMAND"))
|
||||||
|
|
||||||
The result contains the url and the name of the bookmark."
|
(defun help-command (args)
|
||||||
(loop
|
"Show a help message."
|
||||||
with statement = (prepare-statement
|
(format t (concatenate
|
||||||
*db* "select url, name from bookmark")
|
'string
|
||||||
while (step-statement statement)
|
"Usage: clark [command [options]...]~%"
|
||||||
collect (list (statement-column-value statement 0)
|
"~%"
|
||||||
(statement-column-value statement 1))
|
"Possible commands:~%"
|
||||||
finally (finalize-statement statement)))
|
"~%"
|
||||||
|
"help Display this help and exit~%"
|
||||||
|
"version Output version information and exit~%")))
|
||||||
|
|
||||||
(defun clark ()
|
(defun version-command (args)
|
||||||
(setf *db* (connect "test.db"))
|
"Display clark's version number."
|
||||||
(map nil #'print-bookmark (get-bookmarks))
|
(format t "clark version ~A~%" *version*))
|
||||||
|
|
||||||
|
(defun clark (args)
|
||||||
|
(check-db "test2.db")
|
||||||
|
(if (> (length args) 1)
|
||||||
|
(let* ((cmd-name (make-command-name (cadr args)))
|
||||||
|
(sym (intern cmd-name :org.ryuslash.clark)))
|
||||||
|
(when (fboundp sym) (funcall sym (cdr args))))
|
||||||
|
(map nil #'print-bookmark (get-bookmarks)))
|
||||||
(disconnect *db*))
|
(disconnect *db*))
|
||||||
|
|
|
@ -11,6 +11,6 @@
|
||||||
"clark" :toplevel
|
"clark" :toplevel
|
||||||
(lambda ()
|
(lambda ()
|
||||||
(sb-posix:putenv (format nil "SBCL_HOME=~A" #.(sb-ext:posix-getenv "SBCL_HOME")))
|
(sb-posix:putenv (format nil "SBCL_HOME=~A" #.(sb-ext:posix-getenv "SBCL_HOME")))
|
||||||
(org.ryuslash.clark:clark)
|
(org.ryuslash.clark:clark sb-ext:*posix-argv*)
|
||||||
0)
|
0)
|
||||||
:executable t)
|
:executable t)
|
||||||
|
|
Loading…
Add table
Reference in a new issue