-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add command to list commands with prefix
- Loading branch information
1 parent
84a8051
commit b615f68
Showing
2 changed files
with
54 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#!/usr/bin/env bash | ||
# | ||
# Summary: List all available commands with a prefix | ||
# Usage: lasher _commands <prefix> | ||
# | ||
# Looks for executable files prefixed with prefix- and | ||
# displays each suffix as an available command. | ||
# | ||
# This is supposed to be used from other scripts. | ||
|
||
set -e | ||
|
||
if [ "$#" -ne 1 ]; then | ||
lasher-help _commands | ||
exit 1 | ||
fi | ||
|
||
prefix="$1" | ||
|
||
IFS=: paths=($PATH) | ||
|
||
shopt -s nullglob | ||
|
||
{ for path in "${paths[@]}"; do | ||
for command in "${path}/$prefix-"*; do | ||
command="${command##*$prefix-}" | ||
if [[ ! "$command" == _* ]]; then | ||
echo ${command} | ||
fi | ||
done | ||
done | ||
} | sort | uniq |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#!/usr/bin/env bats | ||
|
||
load test_helper | ||
|
||
@test "without arguments prints usage" { | ||
run lasher-_commands | ||
assert_failure | ||
assert_line "Usage: lasher _commands <prefix>" | ||
} | ||
|
||
@test "lists commands" { | ||
run lasher-_commands lasher | ||
assert_success | ||
assert_line help | ||
assert_line new-command | ||
} | ||
|
||
@test "does not list hidden commands" { | ||
run lasher-_commands lasher | ||
assert_success | ||
refute_line _commands | ||
} |