From dae32c5293f4eb215e78195d087ec077ba12a41d Mon Sep 17 00:00:00 2001 From: Dict Xiong Date: Sun, 13 Nov 2022 17:11:39 +0800 Subject: [PATCH] dogo&doll: enhanced --- scripts/dogo | 14 +++++++++++--- scripts/doll | 19 +++++++++++++++++-- tools/msys2.sh | 2 +- 3 files changed, 29 insertions(+), 6 deletions(-) diff --git a/scripts/dogo b/scripts/dogo index 77a5e19..e2b45ca 100755 --- a/scripts/dogo +++ b/scripts/dogo @@ -3,20 +3,28 @@ THIS_DIR=$( cd "$( dirname "${BASH_SOURCE[0]:-${(%):-%x}}" )" && pwd ) source "$THIS_DIR/../tools/common.sh" - +DOCKER_FORMAT="{{.ID}} {{.Image}} {{.Names}}" if [[ -z "$1" || "$1" =~ ^(-h|--help)$ ]]; then fmt_info "usage: $0 or -[0-9]*" exit elif [[ "$1" =~ ^-[0-9]*$ ]]; then LAST="${1:1}" - CONTAINER=$(docker container ls -qn ${LAST:-1} | tail -n 1) + if [[ "$LAST" == "0" ]]; then + # the latest running container + CONTAINER_META=($($SUDO docker container ls -l --filter "status=running" --format "$DOCKER_FORMAT")) + else + # the last nth container (all status) + CONTAINER_META=($($SUDO docker container ls -n ${LAST:-1} --format "$DOCKER_FORMAT" | tail -n 1)) + fi + CONTAINER=${CONTAINER_META[0]} else + CONTAINER_META="$1" CONTAINER="$1" fi if [[ -z "$CONTAINER" ]]; then fmt_error "container not found" else - echo "--> $CONTAINER" + echo "--> ${CONTAINER_META[@]}" $SUDO docker exec -it $CONTAINER sh -c 'if [ -x "$(command -v zsh)" ]; then echo "--> zsh"; zsh; elif [ -x "$(command -v bash)" ]; then echo "--> bash"; bash; else echo "--> sh"; sh; fi' fi diff --git a/scripts/doll b/scripts/doll index f59671e..f1bc59e 100755 --- a/scripts/doll +++ b/scripts/doll @@ -3,8 +3,23 @@ THIS_DIR=$( cd "$( dirname "${BASH_SOURCE[0]:-${(%):-%x}}" )" && pwd ) source "$THIS_DIR/../tools/common.sh" +DOCKER_FORMAT="{{.ID}} {{.Repository}}:{{.Tag}}" if [[ -z "$1" || "$1" =~ ^(-h|--help)$ ]]; then - fmt_info "usage: $0 " + fmt_info "usage: $0 or -[0-9]*" + exit +elif [[ "$1" =~ ^-[0-9]*$ ]]; then + LAST="${1:1}" + # the last nth image + IMAGE_META=($($SUDO docker image ls --format "$DOCKER_FORMAT" | head -n ${LAST:-1} | tail -n 1)) + IMAGE=${IMAGE_META[0]} else - $SUDO docker run -itd $1 sh + IMAGE_META="$1" + IMAGE="$1" +fi + +if [[ -z "$IMAGE" ]]; then + fmt_error "image not found" +else + echo "--> ${IMAGE_META[@]}" + echo $SUDO docker run -itd $IMAGE sh fi diff --git a/tools/msys2.sh b/tools/msys2.sh index a0efa8c..48cc70d 100755 --- a/tools/msys2.sh +++ b/tools/msys2.sh @@ -13,7 +13,7 @@ set_mirror() pacman_S() { pacman -Syu - pacman -S tmux git zsh curl vim wget base-devel mingw-w64-x86_64-toolchain make cmake gcc zip unzip python3 python3-pip + pacman -S tmux git zsh curl vim wget base-devel mingw-w64-x86_64-toolchain make cmake gcc zip unzip python3 python3-pip man-pages-posix } router()