;; This Source Code Form is subject to the terms of the Mozilla Public ;; License, v. 2.0. If a copy of the MPL was not distributed with this ;; file, You can obtain one at http://mozilla.org/MPL/2.0/. ;; bjomarks/examples/marks.bjo — bookmarks from the command line. ;; ;; marks SHARED LOCAL create ;; marks SHARED LOCAL join ;; marks SHARED LOCAL add URL [TAG ...] ;; marks SHARED LOCAL list ;; marks SHARED LOCAL tag ID [TAG ...] ;; marks SHARED LOCAL archive ID URL ;; marks SHARED LOCAL delete ID ;; marks SHARED LOCAL sync ;; ;; SHARED is the folder that the devices share, LOCAL this device's own ;; folder. The password is read from BJOMARKS_PASSWORD, or from the first ;; line of the input. An ID can be its first characters, if only one ;; bookmark starts with them. Each command syncs before and after it. (import (std prelude)) (import (std uuid)) (import (bjomarks bookmark)) (import (bjomarks store)) (: (-> string)) (defun (password) (match (get-environment-variable "BJOMARKS_PASSWORD") ((Some p) p) (None (print "Password: ") (read-line (parameter-ref current-input-port))))) (: (-> StoreError string)) (defun (describe e) (match e (WrongPassword "The password is wrong.") ((NoVault why) why) ((VaultExists why) why) ((OtherVault why) why) ((BadFile why) why))) (: (-> SyncReport void)) (defun (report r) (loop (:for p (.problems r)) (:do (println (str "warning: " p))))) (: (-> Bookmark void)) (defun (show b) (def tags (if (vec-empty? (.tags b)) "" (str " [" (string-join (.tags b) " ") "]"))) (def archive (match (.archive b) ((Some a) (str "\n archived: " a)) (None ""))) (println (str (string-take (uuid->string (.id b)) 8) " " (.url b) tags archive))) ;; The one bookmark whose id starts with `prefix`. (: (-> Vault string (Option Bookmark))) (defun (find-bookmark v prefix) (def found (vec-filter (fun (b) (string-starts-with? (uuid->string (.id b)) prefix)) (vault-bookmarks v))) (if (= (vec-length found) 1) (Some (vec-ref found 0)) None)) ;; Runs a change on the open, synced vault, and syncs again. (: (-> Storage string (-> Vault (Result string Vault)) int)) (defun (with-vault storage local change) (match (open-vault storage local (password)) ((Err e) (println (describe e)) 1) ((Ok opened) (def (Tuple v r) (vault-sync opened)) (report r) (match (change v) ((Err why) (println why) (vault-close v) 1) ((Ok changed) (def (Tuple done r2) (vault-sync changed)) (report r2) (vault-close done) 0))))) (: (-> (Vec string) int)) (defun (main args) (if (< (vec-length args) 3) (begin (println "usage: marks SHARED LOCAL create|join|add|list|tag|archive|delete|sync ...") 2) (let ((storage (folder-storage (vec-ref args 0))) (local (vec-ref args 1)) (command (vec-ref args 2)) (rest (vec-drop args 3))) (cond ((= command "create") (match (create-vault storage local (password)) ((Ok v) (println (str "A new vault, " (uuid->string (vault-id v)))) (vault-close v) 0) ((Err e) (println (describe e)) 1))) ((= command "join") (match (join-vault storage local (password)) ((Ok (Tuple v r)) (report r) (println (str "Joined: " (int->string (vec-length (vault-bookmarks v))) " bookmarks")) (vault-close v) 0) ((Err e) (println (describe e)) 1))) ((= command "list") (with-vault storage local (fun (v) (loop (:for b (vault-bookmarks v)) (:do (show b))) (Ok v)))) ((= command "sync") (with-vault storage local (fun (v) (Ok v)))) ((and (= command "add") (>= (vec-length rest) 1)) (with-vault storage local (fun (v) (match (vault-add v (vec-ref rest 0) #:tags (vec-drop rest 1)) ((Ok (Tuple v2 b)) (show b) (Ok v2)) ((Err why) (Err why)))))) ((and (= command "tag") (>= (vec-length rest) 1)) (with-vault storage local (fun (v) (match (find-bookmark v (vec-ref rest 0)) ((Some b) (Ok (vault-put v (bookmark-with-tags b (vec-drop rest 1))))) (None (Err (str "No one bookmark has the id " (vec-ref rest 0)))))))) ((and (= command "archive") (= (vec-length rest) 2)) (with-vault storage local (fun (v) (match (find-bookmark v (vec-ref rest 0)) ((Some b) (match (bookmark-with-archive b (Some (vec-ref rest 1))) ((Ok b2) (Ok (vault-put v b2))) ((Err why) (Err why)))) (None (Err (str "No one bookmark has the id " (vec-ref rest 0)))))))) ((and (= command "delete") (= (vec-length rest) 1)) (with-vault storage local (fun (v) (match (find-bookmark v (vec-ref rest 0)) ((Some b) (Ok (vault-delete v (.id b)))) (None (Err (str "No one bookmark has the id " (vec-ref rest 0)))))))) (else (println (str "Unknown command, or the wrong arguments: " command)) 2)))))