docker-composeのコマンドが毎回めんどくさいのでbashテンプレ化

# まえがき

[dockerのコマンドが毎回めんどくさいのでbashテンプレ化](https://qiita.com/Azumahata/items/37ad788d82f12e61620d)に続きdocker-composeもテンプレ化


# 注意点
- 前提として私用開発用(雑)、自己責任で煮るなり焼くなりどうぞ。
- 一階層掘った場所を想定(ex. `$ ./scripts/ctl`)
- `# execute`のところで雑にコールしてるので脆弱性に繋がる可能性あり

# script
```bash:scripts/ctl
#!/bin/bash
SCRIPT_DIR=`dirname "${0}"`
SCRIPT_NAME=`basename "${0}"`

# cd to exec dir
cd "${SCRIPT_DIR}/../"

# --------------------------------------------------
# set docker-compose command
CMD_DOCKER_COMPOSE='docker compose'
${CMD_DOCKER_COMPOSE} > /dev/null 2>&1
if [ "${?}" -ne "0" ]; then
  CMD_DOCKER_COMPOSE='docker-compose'
fi


# --------------------------------------------------
# define aliases
SERVICE_ALIASES=$(cat << EOS
b backend
f frontend
EOS
)

_alias() {
  echo "${SERVICE_ALIASES}" | grep -m1 -e "^${1} " | cut -d' ' -f2
}


# --------------------------------------------------
# args
CTL_CMD="${1}"
shift

ALIASED=$(_alias "${1}")
MAYBE_SERVICE="${ALIASED:-${1}}"
shift

REST_ARGS="${@}"


# --------------------------------------------------
# functions
usage() {
  cat << EOS

Usage: ${SCRIPT_DIR}/${SCRIPT_NAME} COMMAND [MAYBE_SERVICE] [REST_ARGS]

Commands:
  build   `type _build | grep '^ '`
  up      `type _up    | grep '^ '`
  run     `type _run   | grep '^ '`
  exec    `type _exec  | grep '^ '`
  stop    `type _stop  | grep '^ '`
  down    `type _down  | grep '^ '`
ServiceAliases:
`echo "${SERVICE_ALIASES}" | sed -e 's/^/  /'`

EOS
  exit 1
}

_build() {
  ${CMD_DOCKER_COMPOSE} build ${MAYBE_SERVICE} ${REST_ARGS}
}

_up() {
  ${CMD_DOCKER_COMPOSE} up ${MAYBE_SERVICE} ${REST_ARGS}
}

_run() {
  ${CMD_DOCKER_COMPOSE} run --rm ${MAYBE_SERVICE} ${REST_ARGS}
}

_exec() {
  ${CMD_DOCKER_COMPOSE} exec -it ${MAYBE_SERVICE} ${REST_ARGS}
}

_stop() {
  ${CMD_DOCKER_COMPOSE} stop ${MAYBE_SERVICE} ${REST_ARGS}
}

_down() {
  ${CMD_DOCKER_COMPOSE} down ${MAYBE_SERVICE} ${REST_ARGS}
}


__main() {
  if [ -z "${CTL_CMD}" ]; then
    usage
  fi

  type "_${CTL_CMD}" > /dev/null 2>&1

  if [ "${?}" -eq "0" ]; then
    # execute
    _${CTL_CMD}
  else
    echo "-${0}: ${SCRIPT_NAME}: ${CTL_CMD} command not found" 1>&2
    usage
  fi
}

__main

```

# ex.usage
```

$ ./scripts/ctl

Usage: ./scripts/ctl COMMAND [MAYBE_SERVICE] [REST_ARGS]

Commands:
  build       ${CMD_DOCKER_COMPOSE} build ${MAYBE_SERVICE} ${REST_ARGS}
  up          ${CMD_DOCKER_COMPOSE} up ${MAYBE_SERVICE} ${REST_ARGS}
  run         ${CMD_DOCKER_COMPOSE} run --rm ${MAYBE_SERVICE} ${REST_ARGS}
  exec        ${CMD_DOCKER_COMPOSE} exec -it ${MAYBE_SERVICE} ${REST_ARGS}
  stop        ${CMD_DOCKER_COMPOSE} stop ${MAYBE_SERVICE} ${REST_ARGS}
  down        ${CMD_DOCKER_COMPOSE} down ${MAYBE_SERVICE} ${REST_ARGS}
ServiceAliases:
  b backend
  f frontend

$ ./scripts/ctl run frontend echo 'Lorem ipsum'
Lorem ipsum
```

# できること
rspecを例に
```bash:example
$ docker-compose run --rm backend_api_rails bundle exec rspec
↓
$ ./scripts/ctl rspec # ex.　コマンド追加
or
$ ./scripts/ctl run rails bundle exec rspec # ex. エイリアス利用
```

## ex. コマンド追加
```bash
_rspec() {
  ${CMD_DOCKER_COMPOSE} run --rm backend_api_rails bundle exec rspec ${MAYBE_SERVICE} ${REST_ARGS}
}
```
を定義しコマンド追加

## ex. エイリアス利用
`SERVICE_ALIASES`にスペース区切りで`[alias] [service_name]`
```bash
SERVICE_ALIASES=$(cat << EOS
rails backend_api_rails
EOS
)
```

# あとがき
`declare -A`使えない子がいて遠回りした・・
これで長いサービス名やコマンドとお別れできる。

突貫なので不都合出てきたら修正するかも。ある程度枯れたらgithubあげるかな。
もっと良い感じにできそうだったら教えて頂けますと幸いです。