Skip to content

Commit 4ca4d1f

Browse files
authored
Merge pull request #125 from hackberrydev/exit-code
Print exit status code when there is an error
2 parents 3f77208 + 90cb97a commit 4ca4d1f

13 files changed

Lines changed: 57 additions & 55 deletions

src/alas.janet

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,11 @@
55

66
(import ./commands :prefix "")
77

8+
(import ./errors)
89
(import ./file_repository)
910
(import ./plan/parser :as plan_parser)
1011
(import ./plan/serializer :as plan_serializer)
1112

12-
(defn- print-errors [errors]
13-
(each error errors (print (string error "."))))
14-
1513
# Keep commands sorted alphabetically.
1614
(def argparse-params
1715
["A command line utility for planning your days"
@@ -42,27 +40,30 @@
4240
(def load-file-result (file_repository/load file-path))
4341
(def errors (load-file-result :errors))
4442
(if errors
45-
(print-errors errors)
43+
(errors/print-errors errors (errors/exit-status-codes :file-error))
4644
(let [plan-string (load-file-result :text)
4745
parse-result (plan_parser/parse plan-string)
4846
parse-errors (parse-result :errors)
4947
plan (parse-result :plan)]
5048
(if parse-errors
51-
(print-errors parse-errors)
52-
(let [serialize-empty-inbox (plan_parser/serialize-empty-inbox? plan-string)
53-
new-plan (run-commands plan file-path arguments)
54-
new-plan-string (plan_serializer/serialize
49+
(errors/print-errors parse-errors (errors/exit-status-codes :parse-error))
50+
(let [{:plan new-plan :errors run-errors} (run-commands plan file-path arguments)]
51+
(if (empty? run-errors)
52+
(let [serialize-empty-inbox (plan_parser/serialize-empty-inbox? plan-string)
53+
new-plan-string (plan_serializer/serialize
5554
new-plan
5655
{:serialize-empty-inbox serialize-empty-inbox})]
57-
(file_repository/save new-plan-string file-path))))))
56+
(file_repository/save new-plan-string file-path))
57+
(errors/print-errors run-errors (errors/exit-status-codes :command-error))))))))
5858

5959
(defn- run-with-arguments [arguments]
6060
(def file-path (arguments :default))
6161
(if file-path
6262
(run-with-file-path arguments file-path)
6363
(if (arguments "version")
6464
(print-version)
65-
(print "Plan file path missing."))))
65+
(errors/print-errors ["Plan file path is missing"]
66+
(errors/exit-status-codes :plan-path-missing)))))
6667

6768
## —————————————————————————————————————————————————————————————————————————————————————————————————
6869
## Public Interface

src/commands.janet

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
(import ./commands/stats)
1313

1414
(import ./date :as d)
15+
(import ./errors)
1516
(import ./schedule_parser)
1617

1718
# backup command needs to be first
@@ -27,10 +28,6 @@
2728
schedule_tasks/build-command
2829
stats/build-command])
2930

30-
(defn- print-errors [errors]
31-
(loop [error :in errors]
32-
(print error)))
33-
3431
## —————————————————————————————————————————————————————————————————————————————————————————————————
3532
## Public Interface
3633

@@ -49,13 +46,12 @@
4946
(defn run-commands [plan file-path arguments]
5047
(def commands (build-commands arguments file-path))
5148
(def errors (filter identity (flatten (map (fn [c] (c :errors)) commands))))
52-
(if (any? errors)
53-
(do
54-
(print-errors errors)
55-
plan)
56-
(reduce (fn [new-plan command-and-arguments]
57-
(def command (first (command-and-arguments :command)))
58-
(def arguments (drop 1 (command-and-arguments :command)))
59-
(apply command new-plan arguments))
60-
plan
61-
commands)))
49+
(var new-plan plan)
50+
(if (empty? errors)
51+
(set new-plan (reduce (fn [new-plan command-and-arguments]
52+
(def command (first (command-and-arguments :command)))
53+
(def arguments (drop 1 (command-and-arguments :command)))
54+
(apply command new-plan arguments))
55+
plan
56+
commands)))
57+
{:plan new-plan :errors errors})

src/commands/list_contacts.janet

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
(import ../utils :prefix "")
55
(import ../date :as d)
6+
(import ../errors)
67
(import ../contact/repository :as contacts_repository)
78

89
(defn- to-csv-line [contact]
@@ -35,6 +36,6 @@
3536
(let [load-result (contacts_repository/load-contacts argument)
3637
errors (load-result :errors)]
3738
(if errors
38-
{:errors (format-command-errors "--list-contacts" errors)}
39+
{:errors (errors/format-command-errors "--list-contacts" errors)}
3940
{:command [print-contacts (load-result :contacts)]}))
4041
{}))

src/commands/schedule_contacts.janet

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
### ————————————————————————————————————————————————————————————————————————————————————————————————
22
### This module implements a command for scheduling contacts for today in a plan.
33

4+
(import ../errors)
45
(import ../utils :prefix "")
56

67
(import ../contact)
@@ -69,6 +70,6 @@
6970
errors (load-result :errors)
7071
contacts (load-result :contacts)]
7172
(if errors
72-
{:errors (format-command-errors "--schedule-contacts" errors)}
73+
{:errors (errors/format-command-errors "--schedule-contacts" errors)}
7374
{:command [schedule-contacts contacts (date/today)]}))
7475
{}))

src/commands/schedule_tasks.janet

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
(import ../plan)
99
(import ../task)
1010

11+
(import ../errors)
1112
(import ../file_repository)
1213
(import ../schedule_parser)
1314

@@ -84,10 +85,10 @@
8485
(let [load-file-result (file_repository/load argument)
8586
errors (load-file-result :errors)]
8687
(if errors
87-
{:errors (format-command-errors command errors)}
88+
{:errors (errors/format-command-errors command errors)}
8889
(let [parse-result (schedule_parser/parse (load-file-result :text))
8990
errors (parse-result :errors)]
9091
(if errors
91-
{:errors (format-command-errors command errors)}
92+
{:errors (errors/format-command-errors command errors)}
9293
{:command [schedule-tasks (parse-result :tasks) (date/today)]}))))
9394
{}))

src/errors.janet

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
### ————————————————————————————————————————————————————————————————————————————————————————————————
2+
### Errors.
3+
4+
## —————————————————————————————————————————————————————————————————————————————————————————————————
5+
## Public Interface
6+
7+
(def exit-status-codes
8+
{:error 1
9+
:plan-path-missing 2
10+
:file-error 3
11+
:parse-error 4
12+
:command-error 5})
13+
14+
(defn format-command-errors [command errors]
15+
(map (fn [error] (string command " " (string/ascii-lower error))) errors))
16+
17+
(defn print-errors [errors exit-status-code]
18+
(each error errors (print (string error ".")))
19+
(os/exit exit-status-code))

src/utils.janet

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,3 @@
1313
(if (string/has-suffix? "/" path)
1414
(string/trimr path "/")
1515
path))
16-
17-
(defn format-command-errors [command errors]
18-
(map (fn [error] (string command " " (string/ascii-lower error) ".")) errors))

test/commands/list_contacts_test.janet

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,4 @@
3232
(deftest "when the directory doesn't exist"
3333
(def arguments {"list-contacts" "test/missing-directory"})
3434
(def result (build-command arguments))
35-
(test (first (result :errors)) "--list-contacts directory does not exist."))
35+
(test (first (result :errors)) "--list-contacts directory does not exist"))

test/commands/schedule_contacts_test.janet

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,4 +127,4 @@
127127
(def arguments {"schedule-contacts" "test/examples/people"})
128128
(def result (build-command arguments))
129129
(test (nil? (result :command)) true)
130-
(test (first (result :errors)) "--schedule-contacts directory does not exist."))
130+
(test (first (result :errors)) "--schedule-contacts directory does not exist"))

test/commands/schedule_tasks_test.janet

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -225,16 +225,16 @@
225225
(def arguments {"schedule-tasks" "test/examples/missing-schedule.md"})
226226
(def result (build-command arguments))
227227
(test (nil? (result :command)) true)
228-
(test (first (result :errors)) "--schedule-tasks file does not exist."))
228+
(test (first (result :errors)) "--schedule-tasks file does not exist"))
229229

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

236236
(deftest "returns an error when the schedule is empty"
237237
(def arguments {"schedule-tasks" "test/examples/empty-schedule.md"})
238238
(def result (build-command arguments))
239239
(test (nil? (result :command)) true)
240-
(test (first (result :errors)) "--schedule-tasks schedule is empty."))
240+
(test (first (result :errors)) "--schedule-tasks schedule is empty"))

0 commit comments

Comments
 (0)