Skip to content

Commit

Permalink
Refactor and rename
Browse files Browse the repository at this point in the history
 - Changes name of script
 - Makes sure the machine exists, and starts it if necessary
 - Loads itself as the bash rc script
  • Loading branch information
peterwwillis committed Oct 11, 2018
1 parent b4f0413 commit c5f226c
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 23 deletions.
2 changes: 1 addition & 1 deletion guides/docker/linux/machine/README.md
Expand Up @@ -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.
19 changes: 0 additions & 19 deletions guides/docker/linux/machine/docker-machine-prompt.rc

This file was deleted.

3 changes: 0 additions & 3 deletions guides/docker/linux/machine/load-dev-machine.sh

This file was deleted.

46 changes: 46 additions & 0 deletions 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 <peterwwillis@gmail.com>
#
# 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

0 comments on commit c5f226c

Please sign in to comment.