Skip to content

Commit

Permalink
Merge pull request #53 from DannyBen/change/if-to-case
Browse files Browse the repository at this point in the history
Change generated script to use `case..in` instead of `if..else`
  • Loading branch information
DannyBen committed Jul 27, 2023
2 parents b91946d + 33a1c08 commit a0b8007
Show file tree
Hide file tree
Showing 6 changed files with 172 additions and 114 deletions.
4 changes: 3 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# This dockerfile is designed for testing alf in isolation
FROM alpine

RUN apk --no-cache add bash git diffutils grep curl zsh
Expand All @@ -11,6 +12,7 @@ COPY alf /usr/local/bin/alf

RUN git config --global pull.rebase false && \
git config --global user.email "tester@testland.com" && \
git config --global user.name "Approval Tester"
git config --global user.name "Approval Tester" && \
git config --global --add safe.directory '*'

RUN chmod +x /usr/local/bin/alf
32 changes: 20 additions & 12 deletions alf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env bash
# This script was generated by bashly 1.0.3 (https://bashly.dannyb.co)
# This script was generated by bashly 1.0.7 (https://bashly.dannyb.co)
# Modifying it manually is not recommended

# :wrapper.bash3_bouncer
Expand Down Expand Up @@ -540,8 +540,9 @@ generate_completions() {
# src/lib/generate_config.sh
generate_config() {
regex="^( *)([a-zA-Z0-9\-]+): *(.+)$"
cond="if"
state="simple"
lastcmd=""
case_open="false"
find_config

echo "# This file was automatically generated by alf"
Expand All @@ -567,8 +568,13 @@ generate_config() {
fi

if [[ -n $ali2 ]]; then
echo " $cond [[ \$1 = \"$ali2\" ]]; then"
echo " shift"
state="nested"
if [[ "$case_open" = "false" ]]; then
echo " case \"\$1\" in"
case_open="true"
fi
echo " $ali2)"
echo " shift"

if [[ $cmd2 =~ ^! ]]; then
cmd=${cmd2:1}
Expand All @@ -579,12 +585,13 @@ generate_config() {
fi

if [[ $cmd2 =~ \$ ]]; then
echo " $cmd"
echo " $cmd"
else
echo " $cmd \"\$@\""
echo " $cmd \"\$@\""
fi
cond="elif"
echo " ;;"
else
case_open="false"
echo ""
echo "unalias $ali1 1>/dev/null 2>&1"
echo "$ali1() {"
Expand All @@ -610,15 +617,16 @@ generate_last_cmd() {
fullcmd="$lastcmd \"\$@\""
fi

if [[ $cond = "if" ]]; then
if [[ $state = "simple" ]]; then
echo " $fullcmd"
echo "}"
else
echo " else"
echo " $fullcmd"
echo " fi"
echo " *)"
echo " $fullcmd"
echo " ;;"
echo " esac"
echo "}"
cond="if"
state="simple"
fi
fi
}
Expand Down
18 changes: 12 additions & 6 deletions src/lib/generate_config.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
generate_config() {
regex="^( *)([a-zA-Z0-9\-]+): *(.+)$"
cond="if"
state="simple"
lastcmd=""
case_open="false"
find_config

echo "# This file was automatically generated by alf"
Expand All @@ -27,8 +28,13 @@ generate_config() {
fi

if [[ -n $ali2 ]]; then
echo " $cond [[ \$1 = \"$ali2\" ]]; then"
echo " shift"
state="nested"
if [[ "$case_open" = "false" ]]; then
echo " case \"\$1\" in"
case_open="true"
fi
echo " $ali2)"
echo " shift"

if [[ $cmd2 =~ ^! ]]; then
cmd=${cmd2:1}
Expand All @@ -39,11 +45,11 @@ generate_config() {
fi

if [[ $cmd2 =~ \$ ]]; then
echo " $cmd"
echo " $cmd"
else
echo " $cmd \"\$@\""
echo " $cmd \"\$@\""
fi
cond="elif"
echo " ;;"
else
echo ""
echo "unalias $ali1 1>/dev/null 2>&1"
Expand Down
12 changes: 7 additions & 5 deletions src/lib/generate_last_cmd.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,17 @@ generate_last_cmd() {
fullcmd="$lastcmd \"\$@\""
fi

if [[ $cond = "if" ]]; then
if [[ $state = "simple" ]]; then
echo " $fullcmd"
echo "}"
else
echo " else"
echo " $fullcmd"
echo " fi"
echo " *)"
echo " $fullcmd"
echo " ;;"
echo " esac"
echo "}"
cond="if"
state="simple"
case_open="false"
fi
fi
}
110 changes: 65 additions & 45 deletions test/fixtures/generate/aliases.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,23 @@ dir() {

unalias g 1>/dev/null 2>&1
g() {
if [[ $1 = "s" ]]; then
shift
git status "$@"
elif [[ $1 = "l" ]]; then
shift
git log --all --graph --date=relative "$@"
elif [[ $1 = "p" ]]; then
shift
git push "$@"
else
git "$@"
fi
case "$1" in
s)
shift
git status "$@"
;;
l)
shift
git log --all --graph --date=relative "$@"
;;
p)
shift
git push "$@"
;;
*)
git "$@"
;;
esac
}

unalias gg 1>/dev/null 2>&1
Expand All @@ -34,12 +39,15 @@ ag() {

unalias abc 1>/dev/null 2>&1
abc() {
if [[ $1 = "help" ]]; then
shift
command abc --help "$@"
else
command abc "$@"
fi
case "$1" in
help)
shift
command abc --help "$@"
;;
*)
command abc "$@"
;;
esac
}

unalias reverse 1>/dev/null 2>&1
Expand All @@ -49,41 +57,53 @@ reverse() {

unalias say 1>/dev/null 2>&1
say() {
if [[ $1 = "again" ]]; then
shift
echo $1 $1
else
echo "$@"
fi
case "$1" in
again)
shift
echo $1 $1
;;
*)
echo "$@"
;;
esac
}

unalias dc 1>/dev/null 2>&1
dc() {
if [[ $1 = "ls" ]]; then
shift
docker-compose config --services "$@"
elif [[ $1 = "deploy" ]]; then
shift
docker stack deploy -c docker-compose.yml "$@"
elif [[ $1 = "upd" ]]; then
shift
docker-compose up -d "$@"
else
docker-compose "$@"
fi
case "$1" in
ls)
shift
docker-compose config --services "$@"
;;
deploy)
shift
docker stack deploy -c docker-compose.yml "$@"
;;
upd)
shift
docker-compose up -d "$@"
;;
*)
docker-compose "$@"
;;
esac
}

unalias dns 1>/dev/null 2>&1
dns() {
if [[ $1 = "check" ]]; then
shift
host -t ns "$@"
elif [[ $1 = "flush" ]]; then
shift
sudo systemd-resolve --flush-caches "$@"
else
echo this alias requires a subcommand
fi
case "$1" in
check)
shift
host -t ns "$@"
;;
flush)
shift
sudo systemd-resolve --flush-caches "$@"
;;
*)
echo this alias requires a subcommand
;;
esac
}

# Completions
Expand Down
Loading

0 comments on commit a0b8007

Please sign in to comment.