diff --git a/guides/docker/linux/machine/README.md b/guides/docker/linux/machine/README.md index 06b041c..c935c21 100644 --- a/guides/docker/linux/machine/README.md +++ b/guides/docker/linux/machine/README.md @@ -76,5 +76,5 @@ exit ## Step 5. Easier loading of the dev machine -To prevent having to type 'bash' and 'eval' as above, run [this bash script](./load-dev-machine.sh) instead. It will drop you into a new shell with the 'dev' machine loaded, and modify your prompt to signify you are using this new machine. Just run `exit` to drop back into your previous shell. +To prevent having to type 'bash' and 'eval' as above, run [this bash script](./load-machine.sh) instead. It will drop you into a new shell with the 'dev' machine loaded, and modify your prompt to signify you are using this new machine. Just run `exit` to drop back into your previous shell. diff --git a/guides/docker/linux/machine/docker-machine-prompt.rc b/guides/docker/linux/machine/docker-machine-prompt.rc deleted file mode 100644 index a0eb7fc..0000000 --- a/guides/docker/linux/machine/docker-machine-prompt.rc +++ /dev/null @@ -1,19 +0,0 @@ -# docker-machine-prompt.sh - change PS1 prompt to include docker machine name -# Copyright (C) 2018 Peter Willis - -# Load user's bashrc file -. ~/.bashrc - -# The following will evaluate a PROMPT_COMMAND (if it was set), -# then it will replace any '\h' entry in the PS1 variable with -# the DOCKER_MACHINE_NAME environment variable. -function __dockermachine_name () { - [ -n "$1" ] && eval "$1" - PS1=${PS1/\\h/(docker-$DOCKER_MACHINE_NAME)} -} -if [ -n "$PROMPT_COMMAND" ] ; then - OLD_PROMPT_COMMAND=$PROMPT_COMMAND - PROMPT_COMMAND="__dockermachine_name '$OLD_PROMPT_COMMAND'" -else - __dockermachine_name -fi diff --git a/guides/docker/linux/machine/load-dev-machine.sh b/guides/docker/linux/machine/load-dev-machine.sh deleted file mode 100755 index 371fff1..0000000 --- a/guides/docker/linux/machine/load-dev-machine.sh +++ /dev/null @@ -1,3 +0,0 @@ -#!/bin/bash -eval $(docker-machine env dev) -exec bash --rcfile docker-machine-prompt.rc diff --git a/guides/docker/linux/machine/load-machine.sh b/guides/docker/linux/machine/load-machine.sh new file mode 100755 index 0000000..d23dab9 --- /dev/null +++ b/guides/docker/linux/machine/load-machine.sh @@ -0,0 +1,46 @@ +#!/usr/bin/env bash +# load-machine.sh - load a docker machine and change PS1 prompt to include machine name +# Copyright (C) 2018 Peter Willis +# +# Usage: +# $ ./load-machine.sh +# OR +# $ ./load-machine.sh machine-name +# + +[ $# -gt 0 ] && DM_NAME="$1" + +# The following will evaluate a PROMPT_COMMAND (if it was set), +# then it will replace any '\h' entry in the PS1 variable with +# the DOCKER_MACHINE_NAME environment variable. +function __dockermachine_name () { + [ -n "$1" ] && eval "$1" + PS1=${PS1/\\h/(docker-$DOCKER_MACHINE_NAME)} +} + +function __load_prompt() { + # Load the user's bashrc file + [ -r "$HOME/.bashrc" ] && . $HOME/.bashrc + if [ -n "$PROMPT_COMMAND" ] ; then + OLD_PROMPT_COMMAND=$PROMPT_COMMAND + PROMPT_COMMAND="__dockermachine_name '$OLD_PROMPT_COMMAND'" + else + __dockermachine_name + fi +} + +if [ -n ${#BASH_LINENO[0]} -a ${BASH_LINENO[0]} -le 0 -a -n "$HISTFILE" ] ; then + __load_prompt +else + + [ -n "$DM_NAME" ] || DM_NAME="dev" + if ! docker-machine ls | tail -n +2 | grep -q "^$DM_NAME" ; then + echo "Error: $0: Docker-machine named '$DM_NAME' does not exist." + else + STATUS=$(docker-machine status $DM_NAME) + [ "$STATUS" = "Stopped" ] && docker-machine start $DM_NAME + eval $(docker-machine env $DM_NAME) + _INCLUDING_AS_RC=1 + exec bash --rcfile ${BASH_SOURCE[0]} + fi +fi