From dd645e48bf951a5a944e102de451e469030023b3 Mon Sep 17 00:00:00 2001 From: Ian Henry Date: Sun, 17 Apr 2022 21:58:55 -0700 Subject: [PATCH] better error message when $SD_ROOT does not exist And also when it exists but is not a directory. --- README.md | 5 +++++ sd | 39 +++++++++++++++++++++++++++++++++++---- 2 files changed, 40 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 8419a61..a66e311 100644 --- a/README.md +++ b/README.md @@ -265,6 +265,11 @@ Bash doesn't support the fancy completion-with-description feature that is sort # Changelog +## v1.0.1 2022-04-17 + +- better error message if `~/sd` does not exist +- better error message if `~/sd` exists but is not a directory + ## v1.0.0 2022-02-27 `sd` is now released under the MIT license. There are no functional changes from the pre-1.0 releases. diff --git a/sd b/sd index f587be0..f9dd4be 100755 --- a/sd +++ b/sd @@ -174,10 +174,32 @@ __sd_new() { fi } +__sd_new_user_help() { + root=$1 + echo >&2 "error: $root not found" + echo >&2 + echo >&2 "It looks like you don't have a script directory yet!" + echo >&2 + echo >&2 "Get started by creating your first script:" + echo >&2 + echo >&2 " sd hello --new 'echo \"Hello, sd!\"'" + echo >&2 + echo >&2 "And then run it like this:" + echo >&2 + echo >&2 " sd hello" +} + __sd() { set -euo pipefail - local target=${SD_ROOT:-$HOME/sd} + local root=${SD_ROOT:-$HOME/sd} + + if [[ -e "$root" && ! -d "$root" ]]; then + echo "error: $root is not a directory" >&2 + exit 1 + fi + + local target=$root local arg while [[ $# -gt 0 ]]; do @@ -212,6 +234,15 @@ __sd() { esac done + # you're allowed to run --new even if there is no + # script directory root, in order to bootstrap it + if [[ "$found_really" = "true" || "$found_new" = "false" ]]; then + if [[ ! -d "$root" ]]; then + __sd_new_user_help "$root" + exit 1 + fi + fi + if [[ "$found_really" = "true" ]]; then local -a preserved=() for arg in "$@"; do @@ -223,12 +254,12 @@ __sd() { if [[ ${#preserved[@]} -gt 0 ]]; then set -- "${preserved[@]}" "$@" fi - elif [[ "$found_help" = "true" ]]; then - __sd_help "$target" - exit 0 elif [[ "$found_new" = "true" ]]; then __sd_new "$target" "$@" exit 0 + elif [[ "$found_help" = "true" ]]; then + __sd_help "$target" + exit 0 elif [[ "$found_edit" = "true" ]]; then __sd_edit "$target" exit 0