First go at args

This commit is contained in:
vlad 2023-03-05 19:08:55 -08:00
parent b62ef700f4
commit c7be3223b4
4 changed files with 89 additions and 1 deletions

View File

@ -68,4 +68,11 @@ kubectlw() {
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
## 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
```diff
$ echo '

41
test/test-arguments Normal file
View File

@ -0,0 +1,41 @@
#!/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"
}
source $(dirname $0)/runner

16
View File

@ -9,6 +9,7 @@ main() {
if [[ $(type -t "⚡${1:-}") == "function" ]]; then
command="$1"
shift
_task_args=( "$@" )
⚡$command "$@"
else
⚡help
@ -53,6 +54,21 @@ _help-line() {
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 "$@"
}
_good-message() {
echo -e "\e[32m$@\e[0m"
}