diff --git a/contrib/completion/.gitignore b/contrib/completion/.gitignore new file mode 100644 index 0000000000..b9d4dad074 --- /dev/null +++ b/contrib/completion/.gitignore @@ -0,0 +1 @@ +!docker-machine* diff --git a/contrib/completion/bash/docker-machine-prompt.bash b/contrib/completion/bash/docker-machine-prompt.bash new file mode 100644 index 0000000000..9b05e7cc48 --- /dev/null +++ b/contrib/completion/bash/docker-machine-prompt.bash @@ -0,0 +1,47 @@ +# +# bash prompt support for docker-machine +# +# This script allows you to see the active machine in your bash prompt. +# +# To enable: +# 1a. Copy this file somewhere and source it in your .bashrc +# source /some/where/docker-machine-prompt.bash +# 1b. Alternatively, just copy this file into into /etc/bash_completion.d +# 2. Change your PS1 to call __docker-machine-ps1 as command-substitution +# PS1='[\u@\h \W$(__docker-machine-ps1 " [%s]")]\$ ' +# +# Configuration: +# +# DOCKER_MACHINE_PS1_SHOWSTATUS +# When set, the machine status is indicated in the prompt. This can be slow, +# so use with care. +# + +__docker-machine-ps1 () { + local format=${1:- [%s]} + if test ${DOCKER_MACHINE_NAME}; then + local status + if test ${DOCKER_MACHINE_PS1_SHOWSTATUS:-false} = true; then + status=$(docker-machine status ${DOCKER_MACHINE_NAME}) + case ${status} in + Running) + status=' R' + ;; + Stopping) + status=' R->S' + ;; + Starting) + status=' S->R' + ;; + Error|Timeout) + status=' E' + ;; + *) + # Just consider everything elase as 'stopped' + status=' S' + ;; + esac + fi + printf -- "${format}" "${DOCKER_MACHINE_NAME}${status}" + fi +} diff --git a/contrib/completion/bash/docker-machine-wrapper.bash b/contrib/completion/bash/docker-machine-wrapper.bash new file mode 100644 index 0000000000..75d50c0b83 --- /dev/null +++ b/contrib/completion/bash/docker-machine-wrapper.bash @@ -0,0 +1,56 @@ +# +# Function wrapper to docker-machine that adds a use subcommand. +# +# The use subcommand runs `eval "$(docker-machine env [args])"`, which is a lot +# less typing. +# +# To enable: +# 1a. Copy this file somewhere and source it in your .bashrc +# source /some/where/docker-machine-wrapper.bash +# 1b. Alternatively, just copy this file into into /etc/bash_completion.d +# +# Configuration: +# +# DOCKER_MACHINE_WRAPPED +# When set to a value other than true, this will disable the alias wrapper +# alias for docker-machine. This is useful if you don't want the wrapper, +# but it is installed by default by your installation. +# + +: ${DOCKER_MACHINE_WRAPPED:=true} + +__docker-machine-wrapper () { + if [[ "$1" == use ]]; then + # Special use wrapper + shift 1 + case "$1" in + -h|--help|"") + cat < /dev/null; then + ${completion_func} + fi + + return 0 +} + +complete -F _docker-machine docker-machine diff --git a/docs/install-machine.md b/docs/install-machine.md index 6567eb318b..d430e8bfb8 100644 --- a/docs/install-machine.md +++ b/docs/install-machine.md @@ -57,6 +57,25 @@ to your PATH. $ docker-machine -v machine version 0.5.0 (3e06852) +## Installing bash completion scripts + +The Machine repository supplies several `bash` scripts that add features such +as: + +* command completion +* a function that displays the active machine in your shell prompt +* a function wrapper that adds a `docker-machine use` subcommand to switch the + active machine + +To install the scripts, copy or link them into your `/etc/bash_completion.d` or +`/usr/local/etc/bash_completion.d` file. To enable the `docker-machine` shell +prompt, add `$(__docker-machine-ps1)` to your `PS1` setting in `~/.bashrc`. + + PS1='[\u@\h \W$(__docker-machine-ps1)]\$ ' + +You can find additional documentation in the comments at the +[top of each script](https://github.com/docker/machine/tree/master/contrib/completion/bash). + ## Where to go next * [Docker Machine overview](/)