From 87a20b90017c81e055fda6255cb92e56c52af590 Mon Sep 17 00:00:00 2001 From: Alexander Willner Date: Thu, 28 Dec 2017 20:07:41 +0100 Subject: [PATCH] added and executed shfmt --- Makefile | 5 ++ README.md | 3 +- test/thingsTest.sh | 62 +++++++++++------- things.sh | 159 ++++++++++++++++++++++++++------------------- 4 files changed, 140 insertions(+), 89 deletions(-) diff --git a/Makefile b/Makefile index 7e2d2d7..6840e60 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,7 @@ help: @echo "Nothing really to make, but there are some available commands:" @echo " * test : run some tests" + @echo " * style : style bash script" @echo " * feedback : create a GitHub issue" feedback: @@ -13,6 +14,10 @@ test: check @bashcov shunit2 test/thingsTest.sh @cat coverage/index.html||true +style: + @type shfmt >/dev/null 2>&1 || (echo "Run 'go get -u mvdan.cc/sh/cmd/shfmt' first." >&2 ; exit 1) + @shfmt -i 2 -w things.sh + check: @type shellcheck >/dev/null 2>&1 || (echo "Run 'brew install shellcheck' first." >&2 ; exit 1) @type shunit2 >/dev/null 2>&1 || (echo "Run 'brew install shunit2' first." >&2 ; exit 1) diff --git a/README.md b/README.md index 2aec319..2b712ba 100644 --- a/README.md +++ b/README.md @@ -95,4 +95,5 @@ Days/Task : 39.0 * https://dev.to/thiht/shell-scripts-matter * https://google.github.io/styleguide/shell.xml * https://kvz.io/blog/2013/11/21/bash-best-practices/ - * https://github.com/progrium/bashstyle \ No newline at end of file + * https://github.com/progrium/bashstyle + * https://github.com/mvdan/sh/ \ No newline at end of file diff --git a/test/thingsTest.sh b/test/thingsTest.sh index c371d7d..6971c81 100644 --- a/test/thingsTest.sh +++ b/test/thingsTest.sh @@ -22,58 +22,75 @@ testWaiting() { testStat() { command="stat" - output=$(eval "$CLI" "$command"|sed -e 's/[ ]//g') + output=$(eval "$CLI" "$command" | sed -e 's/[ ]//g') expected="Inbox:1" - (echo "$output"|grep "$expected" > /dev/null 2>&1); result=$? + (echo "$output" | grep "$expected" >/dev/null 2>&1) + result=$? assertTrue "Command '$command' should contain '$expected' in '\n$output'" $result expected="Today:1" - (echo "$output"|grep "$expected" > /dev/null 2>&1); result=$? + (echo "$output" | grep "$expected" >/dev/null 2>&1) + result=$? assertTrue "Command '$command' should contain '$expected' in '\n$output'" $result expected="Upcoming:3" - (echo "$output"|grep "$expected" > /dev/null 2>&1); result=$? + (echo "$output" | grep "$expected" >/dev/null 2>&1) + result=$? assertTrue "Command '$command' should contain '$expected' in '\n$output'" $result expected="Next:6" - (echo "$output"|grep "$expected" > /dev/null 2>&1); result=$? + (echo "$output" | grep "$expected" >/dev/null 2>&1) + result=$? assertTrue "Command '$command' should contain '$expected' in '\n$output'" $result expected="Someday:4" - (echo "$output"|grep "$expected" > /dev/null 2>&1); result=$? + (echo "$output" | grep "$expected" >/dev/null 2>&1) + result=$? assertTrue "Command '$command' should contain '$expected' in '\n$output'" $result expected="Completed:1" - (echo "$output"|grep "$expected" > /dev/null 2>&1); result=$? + (echo "$output" | grep "$expected" >/dev/null 2>&1) + result=$? assertTrue "Command '$command' should contain '$expected' in '\n$output'" $result expected="Cancelled:1" - (echo "$output"|grep "$expected" > /dev/null 2>&1); result=$? + (echo "$output" | grep "$expected" >/dev/null 2>&1) + result=$? assertTrue "Command '$command' should contain '$expected' in '\n$output'" $result expected="Trashed:3" - (echo "$output"|grep "$expected" > /dev/null 2>&1); result=$? + (echo "$output" | grep "$expected" >/dev/null 2>&1) + result=$? assertTrue "Command '$command' should contain '$expected' in '\n$output'" $result expected="Tasks:11" - (echo "$output"|grep "$expected" > /dev/null 2>&1); result=$? + (echo "$output" | grep "$expected" >/dev/null 2>&1) + result=$? assertTrue "Command '$command' should contain '$expected' in '\n$output'" $result expected="Subtasks:2" - (echo "$output"|grep "$expected" > /dev/null 2>&1); result=$? + (echo "$output" | grep "$expected" >/dev/null 2>&1) + result=$? assertTrue "Command '$command' should contain '$expected' in '\n$output'" $result expected="Waiting:1" - (echo "$output"|grep "$expected" > /dev/null 2>&1); result=$? + (echo "$output" | grep "$expected" >/dev/null 2>&1) + result=$? assertTrue "Command '$command' should contain '$expected' in '\n$output'" $result expected="Projects:4" - (echo "$output"|grep "$expected" > /dev/null 2>&1); result=$? + (echo "$output" | grep "$expected" >/dev/null 2>&1) + result=$? assertTrue "Command '$command' should contain '$expected' in '\n$output'" $result expected="Repeating:2" - (echo "$output"|grep "$expected" > /dev/null 2>&1); result=$? + (echo "$output" | grep "$expected" >/dev/null 2>&1) + result=$? assertTrue "Command '$command' should contain '$expected' in '\n$output'" $result expected="Nextish:6" - (echo "$output"|grep "$expected" > /dev/null 2>&1); result=$? + (echo "$output" | grep "$expected" >/dev/null 2>&1) + result=$? assertTrue "Command '$command' should contain '$expected' in '\n$output'" $result expected="Headings:2" - (echo "$output"|grep "$expected" > /dev/null 2>&1); result=$? + (echo "$output" | grep "$expected" >/dev/null 2>&1) + result=$? assertTrue "Command '$command' should contain '$expected' in '\n$output'" $result expected="Oldest:2017-12-27|TodayProject|TodayTodo" - (echo "$output"|grep "$expected" > /dev/null 2>&1); result=$? + (echo "$output" | grep "$expected" >/dev/null 2>&1) + result=$? assertTrue "Command '$command' should contain '$expected' in '\n$output'" $result expected="Farest:2045-05-13|(NoContext)|NextTodo" - (echo "$output"|grep "$expected" > /dev/null 2>&1); result=$? + (echo "$output" | grep "$expected" >/dev/null 2>&1) + result=$? assertTrue "Command '$command' should contain '$expected' in '\n$output'" $result } @@ -96,9 +113,10 @@ testDue() { } runSimpleTest() { - command="$1" - expected="$2" + command="$1" + expected="$2" output=$(eval "$CLI" "$command") - (echo "$output"|grep "$expected" > /dev/null 2>&1); result=$? + (echo "$output" | grep "$expected" >/dev/null 2>&1) + result=$? assertTrue "Command '$command' should contain '$expected' in '\n$output'" $result -} \ No newline at end of file +} diff --git a/things.sh b/things.sh index 60b0717..0d56176 100755 --- a/things.sh +++ b/things.sh @@ -1,5 +1,5 @@ #!/usr/bin/env bash -# +# # DESCRIPTION # # Simple read-only comand-line interface to your Things 3 database. Since @@ -292,7 +292,6 @@ LIMIT $limitBy; SQL } - old() { sqlite3 "$THINGSDB" <<-SQL SELECT @@ -314,7 +313,6 @@ LIMIT $limitBy; SQL } - due() { sqlite3 "$THINGSDB" <<-SQL SELECT @@ -439,9 +437,9 @@ SQL } csv() { -echo 'Title;"Creation Date";"Modification Date";"Due Date";"Start Date";"Completion Date";Project;Area;Subtask;Notes' + echo 'Title;"Creation Date";"Modification Date";"Due Date";"Start Date";"Completion Date";Project;Area;Subtask;Notes' -sqlite3 "$THINGSDB" <<-SQL + sqlite3 "$THINGSDB" <<-SQL .mode csv .separator ";" SELECT @@ -461,7 +459,7 @@ LEFT OUTER JOIN $AREATABLE T3 ON T1.area = T3.uuid WHERE T1.$ISNOTTRASHED AND (T1.$ISOPEN OR T1.status = 3) AND T1.$ISTASK; SQL -sqlite3 "$THINGSDB" <<-SQL + sqlite3 "$THINGSDB" <<-SQL .mode csv .separator ";" SELECT @@ -481,30 +479,47 @@ WHERE (T2.$ISOPEN OR T2.status = 3) AND T2.$ISNOTTRASHED; SQL } - stat() { - echo -n "Inbox :"; inbox|wc -l + echo -n "Inbox :" + inbox | wc -l echo "" - echo -n "Today :"; today|wc -l - echo -n "Upcoming :"; upcoming|wc -l - echo -n "Next :"; next|wc -l - echo -n "Someday :"; someday|wc -l - echo "" - echo -n "Completed :"; completed|wc -l - echo -n "Cancelled :"; cancelled|wc -l - echo -n "Trashed :"; trashed|wc -l - echo "" - echo -n "Tasks :"; all|wc -l - echo -n "Subtasks :"; subtasks|wc -l - echo -n "Waiting :"; waiting|wc -l - echo -n "Projects :"; projects|wc -l - echo -n "Repeating :"; repeating|wc -l - echo -n "Nextish :"; nextish|wc -l - echo -n "Headings :"; headings|wc -l - echo "" - echo -n "Oldest : "; limitBy="1" old - echo -n "Farest : "; orderBy="startDate DESC" upcoming|tail -n1 - echo -n "Days/Task : "; averageCompleteTime + echo -n "Today :" + today | wc -l + echo -n "Upcoming :" + upcoming | wc -l + echo -n "Next :" + next | wc -l + echo -n "Someday :" + someday | wc -l + echo "" + echo -n "Completed :" + completed | wc -l + echo -n "Cancelled :" + cancelled | wc -l + echo -n "Trashed :" + trashed | wc -l + echo "" + echo -n "Tasks :" + all | wc -l + echo -n "Subtasks :" + subtasks | wc -l + echo -n "Waiting :" + waiting | wc -l + echo -n "Projects :" + projects | wc -l + echo -n "Repeating :" + repeating | wc -l + echo -n "Nextish :" + nextish | wc -l + echo -n "Headings :" + headings | wc -l + echo "" + echo -n "Oldest : " + limitBy="1" old + echo -n "Farest : " + orderBy="startDate DESC" upcoming | tail -n1 + echo -n "Days/Task : " + averageCompleteTime } search() { @@ -527,7 +542,7 @@ WHERE T1.$ISNOTTRASHED AND T1.$ISTASK AND (T1.title LIKE "%$string%" OR T2.title LIKE "%$string%"); SQL -sqlite3 "$THINGSDB" <<-SQL + sqlite3 "$THINGSDB" <<-SQL .mode line SELECT T2.title as "Title", @@ -543,7 +558,7 @@ SQL } require_sqlite3() { - command -v sqlite3 > /dev/null 2>&1 || { + command -v sqlite3 >/dev/null 2>&1 || { echo >&2 "ERROR: SQLite3 is required but could not be found." exit 1 } @@ -551,9 +566,9 @@ require_sqlite3() { require_db() { test -r "$THINGSDB" -a -f "$THINGSDB" || { - echo >&2 "ERROR: Things database not found at '$THINGSDB'." - echo >&2 "HINT: You might need to install Things from https://culturedcode.com/things/" - exit 2 + echo >&2 "ERROR: Things database not found at '$THINGSDB'." + echo >&2 "HINT: You might need to install Things from https://culturedcode.com/things/" + exit 2 } } @@ -564,52 +579,64 @@ main() { while [[ $# -gt 1 ]]; do local key="$1" case $key in - -l|--limitBy) limitBy="$2";shift;; - -w|--waitingTag) waitingTag="$2";shift;; - -o|--orderBy) orderBy="$2";shift;; - -s|--string) string="$2";shift;; - *) ;; + -l | --limitBy) + limitBy="$2" + shift + ;; + -w | --waitingTag) + waitingTag="$2" + shift + ;; + -o | --orderBy) + orderBy="$2" + shift + ;; + -s | --string) + string="$2" + shift + ;; + *) ;; esac shift done - + local command=${1:-} if [[ -n $command ]]; then case $1 in - inbox) inbox;; - today) today;; - upcoming) upcoming;; - next) next;; - anytime) anytime;; - someday) someday;; - all) all;; - nextish) nextish;; - completed) completed;; - old) old;; - due) due;; - repeating) repeating;; - subtasks) subtasks;; - projects) projects;; - headings) headings;; - cancelled) cancelled;; - trashed) trashed;; - waiting) waiting;; - notes) notes;; - csv) csv|awk '{gsub("<[^>]*>", "")}1'|iconv -c -f UTF-8 -t WINDOWS-1252//TRANSLIT;; - stat) limitBy="999999" stat;; - search) search;; - feedback) open https://github.com/AlexanderWillner/things.sh/issues/;; - *) usage;; + inbox) inbox ;; + today) today ;; + upcoming) upcoming ;; + next) next ;; + anytime) anytime ;; + someday) someday ;; + all) all ;; + nextish) nextish ;; + completed) completed ;; + old) old ;; + due) due ;; + repeating) repeating ;; + subtasks) subtasks ;; + projects) projects ;; + headings) headings ;; + cancelled) cancelled ;; + trashed) trashed ;; + waiting) waiting ;; + notes) notes ;; + csv) csv | awk '{gsub("<[^>]*>", "")}1' | iconv -c -f UTF-8 -t WINDOWS-1252//TRANSLIT ;; + stat) limitBy="999999" stat ;; + search) search ;; + feedback) open https://github.com/AlexanderWillner/things.sh/issues/ ;; + *) usage ;; esac else - usage; + usage fi } cleanup() { - : # nothing to clean up - # echo "$(date) $(hostname) $0: EXIT on line $2 (exit status $1)" + : # nothing to clean up + # echo "$(date) $(hostname) $0: EXIT on line $2 (exit status $1)" } [[ "${BASH_SOURCE[0]}" == "${0}" ]] && trap 'cleanup $? $LINENO' EXIT && main "$@"