diff --git a/Dockerfile b/Dockerfile index 16aea43..ad0e18d 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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 @@ -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 diff --git a/alf b/alf index ed5fc65..17ef044 100755 --- a/alf +++ b/alf @@ -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 @@ -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" @@ -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} @@ -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() {" @@ -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 } diff --git a/src/lib/generate_config.sh b/src/lib/generate_config.sh index 033da2e..3756588 100644 --- a/src/lib/generate_config.sh +++ b/src/lib/generate_config.sh @@ -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" @@ -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} @@ -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" diff --git a/src/lib/generate_last_cmd.sh b/src/lib/generate_last_cmd.sh index 8d6caf1..2fde8f7 100644 --- a/src/lib/generate_last_cmd.sh +++ b/src/lib/generate_last_cmd.sh @@ -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 } diff --git a/test/fixtures/generate/aliases.txt b/test/fixtures/generate/aliases.txt index ef4230d..536fec5 100644 --- a/test/fixtures/generate/aliases.txt +++ b/test/fixtures/generate/aliases.txt @@ -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 @@ -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 @@ -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 diff --git a/test/fixtures/generate/approvals/alf_generate b/test/fixtures/generate/approvals/alf_generate index ef4230d..536fec5 100644 --- a/test/fixtures/generate/approvals/alf_generate +++ b/test/fixtures/generate/approvals/alf_generate @@ -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 @@ -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 @@ -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