Compare commits

...

2 Commits

Author SHA1 Message Date
de3baf57e3 Make _required_arguments required 2023-03-05 19:43:28 -08:00
c7be3223b4 First go at args 2023-03-05 19:43:28 -08:00
4 changed files with 111 additions and 1 deletions

View File

@ -68,4 +68,11 @@ kubectlw() {
kubectlw version kubectlw version
} }
source <(cat /tmp/⚡ 2> /dev/null || curl https://apps.ofvlad.xyz/⚡ | tee /tmp/⚡) ⚡run-on() {
_required_argument user
_required_argument host
_argument command
ssh "${_arg_user}@${_arg_host}" "$_arg_command"
}
source <(cat /tmp/ 2> /dev/null || curl https://apps.ofvlad.xyz/ | tee /tmp/ )

View File

@ -94,6 +94,30 @@ source ⚡
``` ```
# More sugar # More sugar
## Arguments
```bash
$ echo '
#!/usr/bin/env bash
⚡run-on() {
_required_argument user
_required_argument host
_argument command
ssh "${_arg_user}@${_arg_host}" "$_arg_command"
}
source ⚡
' > go && chmod +x go
$ ./go run-on host=localhost user=$(whoami) command='figlet hello!'
(v@localhost) Password:
_ _ _ _
| |__ ___| | | ___ | |
| '_ \ / _ \ | |/ _ \| |
| | | | __/ | | (_) |_|
|_| |_|\___|_|_|\___/(_)
```
## Colors ## Colors
```diff ```diff
$ echo ' $ echo '

60
test/test-arguments Normal file
View File

@ -0,0 +1,60 @@
#!/usr/bin/env bash
testArgumentsBecomeVars() {
echo '
#!/usr/bin/env bash
⚡banana() {
_argument hello
if [[ -n "$_arg_hello" ]]; then
echo "I AM A BANANA"
fi
}
' > go
echo "source $REPO_ROOT/⚡" >> go
chmod +x go
try ./go banana hello=please
assertEquals "I AM A BANANA" "$STDOUT"
}
testArgumentsNotDeclaredDoNot() {
echo '
#!/usr/bin/env bash
⚡banana() {
_argument hello
if [[ -n "$_arg_goodbye" ]]; then
echo "I AM A BANANA"
fi
}
' > go
echo "source $REPO_ROOT/⚡" >> go
chmod +x go
try ./go banana goodbye=foo
assertEquals "" "$STDOUT"
}
testRequiredArgumentsAreRequired() {
echo '
#!/usr/bin/env bash
⚡banana() {
_required_argument hello
echo "$_arg_hello"
}
' > go
echo "source $REPO_ROOT/⚡" >> go
chmod +x go
try ./go banana
assertContains "$STDERR" "'hello' is a required argument"
assertEquals 1 "$EXIT_CODE"
}
source $(dirname $0)/runner

19
View File

@ -9,6 +9,7 @@ main() {
if [[ $(type -t "⚡${1:-}") == "function" ]]; then if [[ $(type -t "⚡${1:-}") == "function" ]]; then
command="$1" command="$1"
shift shift
_task_args=( "$@" )
⚡$command "$@" ⚡$command "$@"
else else
⚡help ⚡help
@ -53,6 +54,24 @@ _help-line() {
fi fi
} }
_argument() {
for arg in "${_task_args[@]}"; do
key="$(<<< "$arg" cut -d= -f1)"
value="$(<<< "$arg" cut -d= -f2-)"
if [[ "$key" == "$1" ]]; then
declare -g "_arg_$key"="$value"
fi
done
}
_required_argument() {
_argument "$@"
if [[ ! -v "_arg_$1" ]]; then
_bad-message "'hello' is a required argument" >&2
exit 1
fi
}
_good-message() { _good-message() {
echo -e "\e[32m$@\e[0m" echo -e "\e[32m$@\e[0m"
} }