Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions src/alas.janet
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,11 @@

(import ./commands :prefix "")

(import ./errors)
(import ./file_repository)
(import ./plan/parser :as plan_parser)
(import ./plan/serializer :as plan_serializer)

(defn- print-errors [errors]
(each error errors (print (string error "."))))

# Keep commands sorted alphabetically.
(def argparse-params
["A command line utility for planning your days"
Expand Down Expand Up @@ -42,27 +40,30 @@
(def load-file-result (file_repository/load file-path))
(def errors (load-file-result :errors))
(if errors
(print-errors errors)
(errors/print-errors errors (errors/exit-status-codes :file-error))
(let [plan-string (load-file-result :text)
parse-result (plan_parser/parse plan-string)
parse-errors (parse-result :errors)
plan (parse-result :plan)]
(if parse-errors
(print-errors parse-errors)
(let [serialize-empty-inbox (plan_parser/serialize-empty-inbox? plan-string)
new-plan (run-commands plan file-path arguments)
new-plan-string (plan_serializer/serialize
(errors/print-errors parse-errors (errors/exit-status-codes :parse-error))
(let [{:plan new-plan :errors run-errors} (run-commands plan file-path arguments)]
(if (empty? run-errors)
(let [serialize-empty-inbox (plan_parser/serialize-empty-inbox? plan-string)
new-plan-string (plan_serializer/serialize
new-plan
{:serialize-empty-inbox serialize-empty-inbox})]
(file_repository/save new-plan-string file-path))))))
(file_repository/save new-plan-string file-path))
(errors/print-errors run-errors (errors/exit-status-codes :command-error))))))))

(defn- run-with-arguments [arguments]
(def file-path (arguments :default))
(if file-path
(run-with-file-path arguments file-path)
(if (arguments "version")
(print-version)
(print "Plan file path missing."))))
(errors/print-errors ["Plan file path is missing"]
(errors/exit-status-codes :plan-path-missing)))))

## —————————————————————————————————————————————————————————————————————————————————————————————————
## Public Interface
Expand Down
24 changes: 10 additions & 14 deletions src/commands.janet
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
(import ./commands/stats)

(import ./date :as d)
(import ./errors)
(import ./schedule_parser)

# backup command needs to be first
Expand All @@ -27,10 +28,6 @@
schedule_tasks/build-command
stats/build-command])

(defn- print-errors [errors]
(loop [error :in errors]
(print error)))

## —————————————————————————————————————————————————————————————————————————————————————————————————
## Public Interface

Expand All @@ -49,13 +46,12 @@
(defn run-commands [plan file-path arguments]
(def commands (build-commands arguments file-path))
(def errors (filter identity (flatten (map (fn [c] (c :errors)) commands))))
(if (any? errors)
(do
(print-errors errors)
plan)
(reduce (fn [new-plan command-and-arguments]
(def command (first (command-and-arguments :command)))
(def arguments (drop 1 (command-and-arguments :command)))
(apply command new-plan arguments))
plan
commands)))
(var new-plan plan)
(if (empty? errors)
(set new-plan (reduce (fn [new-plan command-and-arguments]
(def command (first (command-and-arguments :command)))
(def arguments (drop 1 (command-and-arguments :command)))
(apply command new-plan arguments))
plan
commands)))
{:plan new-plan :errors errors})
3 changes: 2 additions & 1 deletion src/commands/list_contacts.janet
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

(import ../utils :prefix "")
(import ../date :as d)
(import ../errors)
(import ../contact/repository :as contacts_repository)

(defn- to-csv-line [contact]
Expand Down Expand Up @@ -35,6 +36,6 @@
(let [load-result (contacts_repository/load-contacts argument)
errors (load-result :errors)]
(if errors
{:errors (format-command-errors "--list-contacts" errors)}
{:errors (errors/format-command-errors "--list-contacts" errors)}
{:command [print-contacts (load-result :contacts)]}))
{}))
3 changes: 2 additions & 1 deletion src/commands/schedule_contacts.janet
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
### ————————————————————————————————————————————————————————————————————————————————————————————————
### This module implements a command for scheduling contacts for today in a plan.

(import ../errors)
(import ../utils :prefix "")

(import ../contact)
Expand Down Expand Up @@ -69,6 +70,6 @@
errors (load-result :errors)
contacts (load-result :contacts)]
(if errors
{:errors (format-command-errors "--schedule-contacts" errors)}
{:errors (errors/format-command-errors "--schedule-contacts" errors)}
{:command [schedule-contacts contacts (date/today)]}))
{}))
5 changes: 3 additions & 2 deletions src/commands/schedule_tasks.janet
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
(import ../plan)
(import ../task)

(import ../errors)
(import ../file_repository)
(import ../schedule_parser)

Expand Down Expand Up @@ -84,10 +85,10 @@
(let [load-file-result (file_repository/load argument)
errors (load-file-result :errors)]
(if errors
{:errors (format-command-errors command errors)}
{:errors (errors/format-command-errors command errors)}
(let [parse-result (schedule_parser/parse (load-file-result :text))
errors (parse-result :errors)]
(if errors
{:errors (format-command-errors command errors)}
{:errors (errors/format-command-errors command errors)}
{:command [schedule-tasks (parse-result :tasks) (date/today)]}))))
{}))
19 changes: 19 additions & 0 deletions src/errors.janet
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
### ————————————————————————————————————————————————————————————————————————————————————————————————
### Errors.

## —————————————————————————————————————————————————————————————————————————————————————————————————
## Public Interface

(def exit-status-codes
{:error 1
:plan-path-missing 2
:file-error 3
:parse-error 4
:command-error 5})

(defn format-command-errors [command errors]
(map (fn [error] (string command " " (string/ascii-lower error))) errors))

(defn print-errors [errors exit-status-code]
(each error errors (print (string error ".")))
(os/exit exit-status-code))
3 changes: 0 additions & 3 deletions src/utils.janet
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,3 @@
(if (string/has-suffix? "/" path)
(string/trimr path "/")
path))

(defn format-command-errors [command errors]
(map (fn [error] (string command " " (string/ascii-lower error) ".")) errors))
2 changes: 1 addition & 1 deletion test/commands/list_contacts_test.janet
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@
(deftest "when the directory doesn't exist"
(def arguments {"list-contacts" "test/missing-directory"})
(def result (build-command arguments))
(test (first (result :errors)) "--list-contacts directory does not exist."))
(test (first (result :errors)) "--list-contacts directory does not exist"))
2 changes: 1 addition & 1 deletion test/commands/schedule_contacts_test.janet
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,4 @@
(def arguments {"schedule-contacts" "test/examples/people"})
(def result (build-command arguments))
(test (nil? (result :command)) true)
(test (first (result :errors)) "--schedule-contacts directory does not exist."))
(test (first (result :errors)) "--schedule-contacts directory does not exist"))
6 changes: 3 additions & 3 deletions test/commands/schedule_tasks_test.janet
Original file line number Diff line number Diff line change
Expand Up @@ -225,16 +225,16 @@
(def arguments {"schedule-tasks" "test/examples/missing-schedule.md"})
(def result (build-command arguments))
(test (nil? (result :command)) true)
(test (first (result :errors)) "--schedule-tasks file does not exist."))
(test (first (result :errors)) "--schedule-tasks file does not exist"))

(deftest "returns an error when the schedule cannot be parsed"
(def arguments {"schedule-tasks" "test/examples/unparsable-schedule.md"})
(def result (build-command arguments))
(test (nil? (result :command)) true)
(test (first (result :errors)) "--schedule-tasks schedule can not be parsed."))
(test (first (result :errors)) "--schedule-tasks schedule can not be parsed"))

(deftest "returns an error when the schedule is empty"
(def arguments {"schedule-tasks" "test/examples/empty-schedule.md"})
(def result (build-command arguments))
(test (nil? (result :command)) true)
(test (first (result :errors)) "--schedule-tasks schedule is empty."))
(test (first (result :errors)) "--schedule-tasks schedule is empty"))
8 changes: 5 additions & 3 deletions test/commands_test.janet
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,9 @@
(day/build-day (d/date 2020 8 2) @[]
@[(task/build-task "Buy milk" true)])]))
(def arguments {"skip-backup" true "remove-empty-days" true "insert-days" "3"})
(def new-plan (run-commands plan file-path arguments))
(def {:plan new-plan :errors errors} (run-commands plan file-path arguments))
(def days (new-plan :days))
(test (empty? errors) true)
(test (length days) 4)
(test (= (d/+days today 2) ((days 0) :date)) true)
(test (= (d/+days today 1) ((days 1) :date)) true)
Expand All @@ -70,9 +71,10 @@
(day/build-day (d/date 2020 8 2) @[]
@[(task/build-task "Buy milk" true)])]))
(def arguments {"skip-backup" true "remove-empty-days" true "insert-days" "three"})
(def new-plan (run-commands plan file-path arguments))
(def {:plan new-plan :errors errors} (run-commands plan file-path arguments))
(def days (new-plan :days))
(test (length days) 3)
(test (= today ((days 0) :date)) true)
(test (= (d/date 2020 8 4) ((days 1) :date)) true)
(test (= (d/date 2020 8 2) ((days 2) :date)) true))
(test (= (d/date 2020 8 2) ((days 2) :date)) true)
(test (= (first errors) "--insert-days argument is not a number.") true))
3 changes: 0 additions & 3 deletions test/examples/my-plan.md

This file was deleted.

13 changes: 0 additions & 13 deletions test/examples/todo-2022-02-10.md

This file was deleted.