Skip to content

Commit

Permalink
better error message when $SD_ROOT does not exist
Browse files Browse the repository at this point in the history
And also when it exists but is not a directory.
  • Loading branch information
ianthehenry committed Apr 18, 2022
1 parent ea5cb98 commit dd645e4
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
39 changes: 35 additions & 4 deletions sd
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit dd645e4

Please sign in to comment.