Convert Cojure code from this article "Creating a TODO cli with Clojure" to use with Babashka: https://dev.to/prasannagnanaraj/i-just-created-a-todo-cli-with-clojure-1133
Last active
December 8, 2020 14:06
-
-
Save prestancedesign/d2f6ba223e418298618966937062dda0 to your computer and use it in GitHub Desktop.
A TODO cli with Clojure (Babashka)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bb | |
(require '[clojure.string :refer [split]]) | |
(require '[clojure.java.io :refer [writer reader]]) | |
(require '[clojure.pprint :refer [print-table]]) | |
(def file-location (System/getenv "TODO_LOCATION")) | |
(defn now [] (new java.util.Date)) | |
(defn add-content | |
"appends content to todo file" | |
[file-location text-content] | |
(with-open [file (writer file-location :append true)] | |
(.write file (str text-content "\t" (now) "\n")))) | |
(defn print-helper | |
"Converts line content to a row obj" | |
[line-content] | |
(let [[todo created_at] (split line-content #"\t")] | |
{:todo todo :created_at (or created_at "UNKNOWN")})) | |
(defn read-content | |
"reads content from todo file" | |
[file-location] | |
(with-open [file (reader file-location)] | |
(let [file-content (slurp file)] | |
(print-table | |
(map print-helper | |
(split file-content #"\n")))))) | |
(let [args *command-line-args*] | |
(when (nil? file-location) | |
(throw (AssertionError. "empty $TODO_LOCATION"))) | |
(case (first args) | |
"a" (do | |
(add-content file-location (second args)) | |
(read-content file-location)) | |
"ls" (read-content file-location) | |
(println "Choose either a or ls"))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment