Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
mdeboer committed Jun 6, 2023
2 parents 925835c + dd1fa36 commit 9ed5305
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 27 deletions.
8 changes: 8 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
root = true

[*]
end_of_line = lf
insert_final_newline = true
charset = utf-8
indent_size = 4
indent_style = space
39 changes: 25 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,33 @@
[![CircleCI](https://circleci.com/gh/Cloudstek/zsh-plugin-appup.svg?style=svg)](https://circleci.com/gh/Cloudstek/zsh-plugin-appup)

This plugins adds `start`, `restart`, `stop`, `up` and `down` commands when it detects a docker-compose or Vagrant file in the current directory (e.g. your application). Just run `up` and get coding! This saves you typing `docker-compose` or `vagrant` every time or aliasing them. Also gives you one set of commands that work for both environments.
This plugins adds `start`, `restart`, `stop`, `up` and `down` commands when it detects a docker-compose or Vagrant file
in the current directory (e.g. your application). Just run `up` and get coding! This saves you typing `docker-compose`
or `vagrant` every time or aliasing them. Also gives you one set of commands that work for both environments.

### Docker

Aside from simply running `up`, you can also extend your configuration by running `up <name>`, which will run `docker-compose` with both `docker-compose.yml` and extend it with `docker-compose.<name>.yml`. For more on extending please see the [official docker documentation](https://docs.docker.com/compose/extends). Additional arguments will be directly supplied to the docker-compose.
Aside from simply running `up`, you can also extend your configuration by running `up <name>`, which will
run `docker-compose` with both `docker-compose.yml` and extend it with `docker-compose.<name>.yml`. For more on
extending please see the [official docker documentation](https://docs.docker.com/compose/extends). Additional arguments
will be directly supplied to the docker-compose.

### Vagrant

Vagrant doesn't have a `down`, `restart`, `start` or `stop` commands natively but don't worry, that's been taken care of and running those commands will actually run vagrant's equivalent commands. Additional arguments will be directly supplied to vagrant.
Vagrant doesn't have a `down`, `restart`, `start` or `stop` commands natively but don't worry, that's been taken care of
and running those commands will actually run vagrant's equivalent commands. Additional arguments will be directly
supplied to vagrant.

### Command mapping

| Command | Vagrant command | Docker command |
| ------- | ---------------------------------------------------------- | ------------------------------------------------------------ |
| up | [up](https://www.vagrantup.com/docs/cli/up.html) | [up](https://docs.docker.com/compose/reference/up/) |
| down | [destroy](https://www.vagrantup.com/docs/cli/destroy.html) | [down](https://docs.docker.com/compose/reference/down/) |
| start | [up](https://www.vagrantup.com/docs/cli/up.html) | [start](https://docs.docker.com/compose/reference/start/) |
| Command | Vagrant command | Docker command |
|---------|------------------------------------------------------------|---------------------------------------------------------------|
| up | [up](https://www.vagrantup.com/docs/cli/up.html) | [up](https://docs.docker.com/compose/reference/up/) |
| down | [destroy](https://www.vagrantup.com/docs/cli/destroy.html) | [down](https://docs.docker.com/compose/reference/down/) |
| start | [up](https://www.vagrantup.com/docs/cli/up.html) | [start](https://docs.docker.com/compose/reference/start/) |
| restart | [reload](https://www.vagrantup.com/docs/cli/reload.html) | [restart](https://docs.docker.com/compose/reference/restart/) |
| stop | [halt](https://www.vagrantup.com/docs/cli/halt.html) | [stop](https://docs.docker.com/compose/reference/stop/) |
| stop | [halt](https://www.vagrantup.com/docs/cli/halt.html) | [stop](https://docs.docker.com/compose/reference/stop/) |
| enter | | [exec](https://docs.docker.com/compose/reference/exec/) /bin/bash -l (or custom command/shell, e.g. with `enter /bin/sh`) |

## Installation

Expand Down Expand Up @@ -52,11 +60,14 @@ Vagrant doesn't have a `down`, `restart`, `start` or `stop` commands natively bu

## Configuration options

AppUp has a few configuration options to customise its behaviour. Please make sure you define these in `~/.zshrc` *before* you load any plugins.
AppUp has a few configuration options to customise its behaviour. Please make sure you define these in `~/.zshrc`
*before* you load any plugins.

| Name | Values | Default | Description |
| -------------------- | ---------- | ------- | ------------------------------------------------------------ |
| APPUP_CHECK_STARTED | true/false | true | Enable/disable checking if docker is running completely. |
Currently these options only affect docker.

| Name | Values | Default | Description |
|----------------------|------------|---------|---------------------------------------------------------------------------------------------------------------------------------------------------|
| APPUP_CHECK_STARTED | true/false | true | Enable/disable checking if docker is running completely. |
| APPUP_DOCKER_MACHINE | true/false | true | If both docker (e.g. Docker Desktop) and docker-machine are installed, check if docker-machine (when `true`) or docker (when `false`) is running. |
| | | | |
| APPUP_LOAD_ENVS | true/false | true | When true, load .env, .env.local, .env.docker and .env.docker.local if they exist with `docker compose --env-file`. |

63 changes: 50 additions & 13 deletions appup.plugin.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -40,41 +40,64 @@ _appup_docker () {
fi
fi

# Check YAML extension
# Find docker-compose file
compose_file=''
compose_project_file=''

if [ -e "docker-compose.yml" ]; then
compose_file='docker-compose.yml'
elif [ -e "docker-compose.yaml" ]; then
compose_file='docker-compose.yaml'
fi

# Env files
env_files=()

if [ "${APPUP_LOAD_ENVS:-true}" = true ]; then
if [ -e ".env" ]; then
env_files+=( .env )
fi
if [ -e ".env.local" ]; then
env_files+=( .env.local )
fi
if [ -e ".env.docker" ]; then
env_files+=( .env.docker )
fi
if [ -e ".env.docker.local" ]; then
env_files+=( .env.docker.local )
fi
fi

# <cmd> <project name> will look for docker-compose.<project name>.yml
if [ -n "$2" ]; then
# Find project-specific docker-compose file
compose_project_file=''

if [ -e "docker-compose.$2.yml" ]; then
compose_project_file="docker-compose.$2.yml"
elif [ -e "docker-compose.$2.yaml" ]; then
compose_project_file="docker-compose.$2.yaml"
fi
if [ -n "$compose_project_file" ]; then
# Override project name from custom env
if [ -e ".env.$2" ]; then
project=$(source ".env.$2"; echo $COMPOSE_PROJECT_NAME)

if [ -n $project ]; then
docker-compose -p "${project}" -f "$compose_file" -f "$compose_project_file" $1 "${@:3}"
return

if [ -n "$compose_project_file" ]; then
# Project specific env file
if [ "${APPUP_LOAD_ENVS:-true}" = true ]; then
if [ -e ".env.$2" ]; then
env_files+=( ".env.$2" )
fi
if [ -e ".env.$2.local" ]; then
env_files+=( ".env.$2.local" )
fi
fi

docker-compose -f "$compose_file" -f "$compose_project_file" $1 "${@:3}"
# Run docker compose.
docker-compose -f "$compose_file" -f "$compose_project_file" --env-file=$^env_files $1 "${@:3}"

return
fi
fi

docker-compose $1 "${@:2}"
# Run docker compose.
docker-compose --env-file=$^env_files $1 "${@:2}"
else
echo >&2 "Docker compose file found but docker-compose is not installed."
fi
Expand Down Expand Up @@ -139,3 +162,17 @@ stop () {
env stop "$@"
fi
}

enter () {
if [ -e "docker-compose.yml" ] || [ -e "docker-compose.yaml" ]; then
CMD=( "${@:2}" )

if [ $# -eq 1 ]; then
CMD=( /bin/bash -l )
fi

_appup_docker exec "$1" $CMD
elif hash enter >/dev/null 2>&1; then
env enter "$@"
fi
}

0 comments on commit 9ed5305

Please sign in to comment.