aliases icon indicating copy to clipboard operation
aliases copied to clipboard

Nested aliases or reference to other alias.

Open wesselvdv opened this issue 7 years ago • 3 comments

Currently to achieve nested aliases we have to embed entire case switch statement inside the .aliases file that pick up the aliasing parts. Might be awesome to be able to create nested aliases? Would allow us to create very advanced aliases.

wesselvdv avatar Aug 16 '18 13:08 wesselvdv

I'm not sure I follow.

You can do nested aliases pretty easily. An example .aliases file would be:

dc:
  command: docker-compose
up:
  command: dc up

Does that cover what you mean?

sebglazebrook avatar Aug 17 '18 11:08 sebglazebrook

Well, not quite. I am currently using it as follows:

# This file is autogenerated by the aliases tool.
# For more info about aliases type `aliases --help`
# or visit https://github.com/sebglazebrook/aliases

---
cws:
  command: |
    command="$0"
    declare -gA COMMAND_CHECKS
    if [ -f /proc/sys/kernel/osrelease ] && cat /proc/sys/kernel/osrelease | grep "^[0-9.-]+Microsoft$"; then
      export POLLING=1;
    else
      export POLLING=0;
    fi;
    COMPOSE_HTTP_TIMEOUT=10000
    case "${command}" in
      start)
        if [ $(command -v docker-sync) ]; then
          docker-sync-stack start;
        else
          docker-compose up;
        fi;
      ;;
      stop)
        docker-compose stop
      ;;
      reset)
        if [ $(command -v docker-sync) ]; then
          docker-sync-stack clean;
        else
          docker-compose down;
        fi;
      ;;
      restart)
        cws stop && cws start
      ;;
      rebuild)
        read -e -cws "${service}" $'THIS WILL REBUILD ALL CONTAINERS AND CAN RESULT IN DATA LOSS!\nAre you sure? [y/N]: ' choice
        [[ "${choice}" == [Yy]* ]] && (cws "${service}" reset && docker-compose build --no-cache --force-rm) || :
      ;;
      rebuild:cache)
        read -e -cws "${service}" $'THIS WILL REBUILD ALL CONTAINERS AND CAN RESULT IN DATA LOSS!\nAre you sure? [y/N]: ' choice
        [[ "${choice}" == [Yy]* ]] && (cws "${service}" reset && docker-compose build --force-rm) || :
      ;;
      logs)
        docker-compose logs -t
      ;;
      *)
        if docker-compose config --services | grep --quiet "${command}"; then
          service="${command}"
          command="$1"
          container=$(docker-compose config --services | grep "${service}")
          shift 1
          case "${command}" in
            exec)
              docker-compose exec "${container}" bash -c "$* $@"
            ;;
            run)
              docker-compose run --no-deps --rm "${container}" bash -c "$* $@"
            ;;
            logs)
              docker-compose logs -f "${container}"
            ;;
            command)
              cmd=$(echo "$* $@" | xargs)
              echo >&2 ${COMMAND_CHECKS[$cmd]};
              if [ ! "${COMMAND_CHECKS[$cmd]+_}" ]; then
                if [[ ! $(cws "${service}" run command -v "${cmd}") ]]; then
                    eval COMMAND_CHECKS[$cmd]=false;
                else
                    eval COMMAND_CHECKS[$cmd]=true;
                fi;
              fi;
              echo >&2 ${COMMAND_CHECKS[$cmd]};
              if [ "${COMMAND_CHECKS[$cmd]}" = true ]; then
                  echo "OK!";
                  exit 0;
              elif [ "${COMMAND_CHECKS[$cmd]}" = false ]; then
                  echo >&2 "The service ${service} doesn't support ${cmd}. Aborting.";
                  exit 1;
              fi;
            ;;
            npm)
              cmd="npm"
              if cws "${service}" command "${cmd}" | grep --quiet "OK!"; then
                  cws "${service}" run "${cmd} $* $@";
              fi;
            ;;
            npm:update)
              cmd="npm-check"
              if cws "${service}" command "${cmd}" | grep --quiet "OK!"; then
                  cws "${service}" run "${cmd}" -uE;
              fi;
            ;;
            start)
              docker-compose up "${container}"
            ;;
            stop)
              docker-compose stop "${container}"
            ;;
            rm)
              docker-compose rm "${container}"
            ;;
            reset)
              cws "${service}" stop && cws "${service}" rm && cws "${service}" start
            ;;
            restart)
              cws "${service}" stop && cws "${service}" start
            ;;
            build)
              docker-compose build --force-rm --no-cache "${container}"
            ;;
            build:cache)
              docker-compose build --force-rm "${container}"
            ;;
            rebuild)
              read -e -p $'THIS WILL REBUILD YOUR CONTAINER WITHOUT CACHE AND CAN RESULT IN DATA LOSS!\nAre you sure? [y/N]: ' choice
              [[ "${choice}" == [Yy]* ]] && (cws "${service}" stop && cws "${service}" rm && cws "${service}" build) || :
            ;;
            rebuild:cache)
              read -e -p $'THIS WILL REBUILD YOUR CONTAINER WITH CACHE AND CAN RESULT IN DATA LOSS!\nAre you sure? [y/N]: ' choice
              [[ "${choice}" == [Yy]* ]] && (cws "${service}" stop && cws "${service}" rm && cws "${service}" build:cache) || :
            ;;
            types:generate)
              cmd="npm"
              if cws "${service}" command "${cmd}" | grep -v "${cmd}"; then
                  cws "${service}" "${cmd}" run apollo;
              fi;
            ;;
            sqitch:rework)
              change=$1
              notes=\'$2\'
              shift 2
              cws "${service}" sqitch "-f src/structure.plan rework -c $change -n $notes $* $@"
              ;;
            sqitch:tag)
              tag=\'$1\'
              notes=\'$2\'
              shift 2
              cws "${service}" sqitch "-f src/structure.plan tag $tag -n $notes $* $@"
              ;;
            sqitch:add)
              case "$1" in
                schema)
                    schema=$2
                    notes=\'$3\'
                    shift 3
                    cws "${service}" sqitch "-f src/structure.plan add -s schema=$schema -c $schema/schema -t schema -n $notes $* $@"
                    ;;
                table)
                    schema=$2
                    table=$3
                    notes=\'$4\'
                    shift 4
                    if [[ "$schema" == public ]]; then
                      cws "${service}" sqitch "-f src/structure.plan add -s schema=$schema -s table=$table -c $schema/tables/$table -t table -n $notes $* $@";
                    else
                      cws "${service}" sqitch "-f src/structure.plan add -s schema=$schema -s table=$table -c $schema/tables/$table -t table -n $notes --requires $schema/schema $* $@";
                    fi;
                    ;;
                view)
                    schema=$2
                    view=$3
                    notes=\'$4\'
                    shift 4
                    if [[ "$schema" == public ]]; then
                      cws "${service}" sqitch "-f src/structure.plan add -s schema=$schema -s view=$view -c $schema/views/$view -t view -n $notes $* $@";
                    else
                      cws "${service}" sqitch "-f src/structure.plan add -s schema=$schema -s view=$view -c $schema/views/$view -t view -n $notes --requires $schema/schema $* $@";
                    fi;
                    ;;
                function)
                    schema=$2
                    function=$3
                    notes=\'$4\'
                    shift 4
                    if [[ "$schema" == public ]]; then
                      cws "${service}" sqitch "-f src/structure.plan add -s schema=$schema -s function=$function -c $schema/functions/$function -t function -n $notes $* $@";
                    else
                      cws "${service}" sqitch "-f src/structure.plan add -s schema=$schema -s function=$function -c $schema/functions/$function -t function -n $notes --requires $schema/schema $* $@";
                    fi;
                    ;;
                column)
                    schema=$2
                    table=$3
                    column=$4
                    notes=\'$5\'
                    shift 5
                    read -e -cws "${service}" $'Is this a computed column? [y/N]: ' choice
                    if [[ "$choice" == y ]]; then
                      cws "${service}" sqitch "-f src/structure.plan add -s language=sql -s inputs=$schema.$table -s schema=public -s function=$column -c public/columns/$schema.$table.$column -t function -n $notes --requires $schema/tables/$table $* $@";
                    else
                      cws "${service}" sqitch "-f src/structure.plan add -s column=$column -s schema=$schema -s table=$table -c $schema/columns/$table.$column -t column -n $notes --requires $schema/tables/$table $* $@";
                    fi;
                    ;;
                data)
                    schema=$2
                    table=$3
                    notes=\'"Adds dummy data in ${schema}.${table}"\'
                    shift 3
                    cws "${service}" sqitch "-f src/data.plan add -s schema=$schema -s table=$table -c $schema/data/$table -t data -n $notes --requires structure:$schema/tables/$table $* $@"
                    ;;
                tap)
                    function=$2
                    notes=\'$3\'
                    shift 3
                    cws "${service}" sqitch "-f src/tap.plan add -s schema=tap -s function=$function -c tap/functions/$function -t function -n $notes --requires tap/schema $* $@"
                    ;;
                configuration)
                    schema=$2
                    table=$3
                    notes=\'"Adds configuration data in ${schema}.${table}"\'
                    shift 3
                    cws "${service}" sqitch "-f src/configuration.plan add -s schema=$schema -s table=$table -c $schema/configuration/$table -t data -n $notes --requires structure:$schema/tables/$table $* $@"
                    ;;
                extension)
                    extension=$2
                    notes=\'$3\'
                    shift 3
                    cws "${service}" sqitch "-f src/structure.plan add -s extension=$extension -c public/extensions/$extension -t extension -n $notes $* $@"
                    ;;
                *)
                    echo $"Usage: $1 {schema|table|function|column|extension|tap|data|view}"
                    exit 1
              esac
            ;;
            sqitch:test)
              match=$1
              if [[ "$match" != "" ]]; then
                  cws "${service}" exec pg_prove -r --shuffle --ext .sql --username postgres --dbname cws "$* $@";
              else
                  cws "${service}" exec pg_prove -rQf --shuffle --ext .sql --username postgres --dbname cws src/test;
              fi;
            ;;
            sqitch:deploy)
              if ! cws "${service}" sqitch -f src/structure.plan deploy | tee /dev/tty | grep -q 'Deploy failed'; then
                if ! cws "${service}" sqitch -f src/tap.plan deploy | tee /dev/tty | grep -q 'Deploy failed'; then
                  if ! cws "${service}" sqitch -f src/configuration.plan deploy | tee /dev/tty | grep -q 'Deploy failed'; then
                    if cws "${service}" sqitch:test | tee /dev/tty | grep -q 'Result: PASS'; then
                      cws "${service}" sqitch -f src/data.plan deploy;
                    fi;
                  fi;
                fi;
              fi
            ;;
            sqitch:redeploy)
              cws "${service}" sqitch:revert && cws "${service}" sqitch:deploy
            ;;
            sqitch:revert)
              cws "${service}" sqitch -f src/data.plan revert
              cws "${service}" sqitch -f src/structure.plan revert
              cws "${service}" sqitch -f src/tap.plan revert
            ;;
            sqitch)
              cmd="sqitch"
              if cws "${service}" command "${cmd}" | grep --quiet "OK!"; then
                  cws "${service}" exec "${cmd} $* $@"
              fi
            ;;
            psql)
              cmd="psql"
              if cws "${service}" command "${cmd}" | grep --quiet "OK!"; then
                  cws "${service}" exec "${cmd} $* $@"
              fi
            ;;
            *)
              echo $"Usage: $1 {sqitch|sqitch:revert|sqitch:deploy|sqitch:redeploy|sqitch:test|sqitch:add|sqitch:tag|sqitch:rework|exec|types:generate|npm|npm:update|start|stop|reset|restart|rm|build|build:cache|rebuild|rebuild:cache}"
              exit 1
            ;;
          esac
        else
          echo $"Usage: $0 {start|stop|restart|reset|rebuild|rebuild:cache}"
          exit 1
        fi
      ;;
    esac
  quiet: true
#  confirm: true

So, I have one root alias (cws) which usually corresponds to the project name, and subsequently nested aliases that all have to start with the root alias. The reason I am doing this is because I'd rather not pollute my path with common alias names. This file has to be used for multiple platforms.

Funny thing is I think I used aliases to built my own version of what they're trying to achieve here: https://github.com/creasty/rid

wesselvdv avatar Aug 17 '18 12:08 wesselvdv

wow that's one big alias!

So you have 1 root alias and its kinda like a namespace yeah?

I do similar things but I don't have as much magic going on.

I'll have a bunch of aliases named foo-deploy foo-publish foo-rollback etc. But you have to create an alias for each command you want. The benefit is if you type foo and then use tab completion you can see all aliases available under the namespace.

It probably doesn't look as pretty externally as what you're doing though.

You've got a lot going on in your example. If you wanted to provide a generic use case with example aliases/commands I would be glad to talk more about it

sebglazebrook avatar Sep 03 '18 11:09 sebglazebrook