exit 1
}
-CONTAINER=${1}
-[ "x${CONTAINER}" == "x" ] && die "You must supply a container name as the first argument."
-shift
-docker exec -it "${CONTAINER}" bash "${@}"
+usage() {
+ cat <<EOF
+dssh: log in to a docker node
+
+usage: dssh: [options]
+
+options:
+-h: show this help message.
+-i [ID]: ssh into a docker node with this container ID
+-n [name]: ssh into a docker node with this name
+EOF
+}
+
+DOCKER_IMAGE_ID=""
+DOCKER_IMAGE_NAME=""
+while getopts "hi:n:" flag; do
+ case $flag in
+ h) usage; exit 0;;
+ i) DOCKER_IMAGE_ID=${OPTARG};;
+ n) DOCKER_IMAGE_NAME=${OPTARG};;
+ *) echo "getopts error"
+ echo
+ usage
+ exit 1;;
+ esac
+done
+
+which docker &>/dev/null || die "docker must be on the PATH."
+
+if [ "x${DOCKER_IMAGE_NAME}" == "x" ]; then
+ if [ "x${DOCKER_IMAGE_ID}" == "x" ]; then
+ usage
+ exit 1
+ fi
+else
+ if [ "x${DOCKER_IMAGE_ID}" == "x" ]; then
+ :
+ else
+ echo "You must not supply both an ID and a name."
+ exit 1
+ fi
+ DOCKER_IMAGE_ID=$(docker ps -f name=${DOCKER_IMAGE_NAME} -q)
+ [ "x${DOCKER_IMAGE_ID}" == "x" ] && \
+ die "failed to find a docker image named ${DOCKER_IMAGE_NAME}"
+fi
+
+docker exec -it "${DOCKER_IMAGE_ID}" /bin/bash