#!/bin/bash # Source: https://github.com/lukaszlach/clip/ # Docker Client Plugin Manager (CLIP) # # (c) 2019 Łukasz Lach # llach@llach.pl # https://lach.dev if [[ "$1" == "docker-cli-plugin-metadata" ]]; then cat <&2 break ;; : ) echo "Invalid option: $OPTARG requires an argument" 1>&2 break ;; esac done shift $((OPTIND -1)) PLUGIN_REPOSITORY=$(echo "$PLUGIN_TAG" | cut -d ':' -f 1) PLUGIN_CONTEXT="$1" if [ ! -e "$PLUGIN_CONTEXT/docker-$PLUGIN_COMMAND" ]; then echo "Error: docker-$PLUGIN_COMMAND file does not exist in the build context" exit 1 elif [ ! -x "$PLUGIN_CONTEXT/docker-$PLUGIN_COMMAND" ]; then echo "Error: docker-$PLUGIN_COMMAND file is not executable" exit 1 elif [ ! -e "$PLUGIN_CONTEXT/docker-$PLUGIN_COMMAND.json" ]; then echo "Error: docker-$PLUGIN_COMMAND.json file does not exist in the build context" exit 1 fi BUILD_FILE=$(mktemp) cat > "$BUILD_FILE" </dev/null; then docker pull "$PLUGIN_TAG" REMOVE_PLUGIN_IMAGE=1 fi PLUGIN_FLAG=$(docker image inspect --format '{{index .Config.Labels "com.docker.clip"}}' "$PLUGIN_TAG") PLUGIN_COMMAND=$(docker image inspect --format '{{index .Config.Labels "com.docker.clip.command"}}' "$PLUGIN_TAG") PLUGIN_BIN="$USER_LOCAL_DIR/docker-$PLUGIN_COMMAND" if [ "$PLUGIN_FLAG" != "1" ]; then echo "Error: Image $PLUGIN_TAG is not a valid plugin image" exit 1 fi if [ -e "$PLUGIN_BIN" ]; then echo "Error: $PLUGIN_TAG exports '$PLUGIN_COMMAND' client command that already exists" exit 1 fi PLUGIN_LOCAL_DIR="$USER_LOCAL_DIR/.command/$PLUGIN_COMMAND" rm -rf "$PLUGIN_LOCAL_DIR" mkdir -p "$PLUGIN_LOCAL_DIR" IMAGE_DIR=$(mktemp -d) docker save "$PLUGIN_TAG" | tar -C "$IMAGE_DIR" -x find "$IMAGE_DIR" -name 'layer.tar' -exec tar -C "$PLUGIN_LOCAL_DIR" -xf {} \; rm -rf "$IMAGE_DIR" echo "$PLUGIN_TAG" > "$PLUGIN_LOCAL_DIR/.plugin-image" if [[ "$REMOVE_PLUGIN_IMAGE" == "1" ]]; then docker rmi "$PLUGIN_TAG" >/dev/null fi # Install command cat > "$USER_LOCAL_DIR/docker-$PLUGIN_COMMAND" </dev/null || true cmd_install "$PLUGIN_TAG" echo "Successfully updated the $PLUGIN_COMMAND client command" } # # docker clip rm # cmd_rm() { PLUGIN_COMMAND="$1" if [ -z "$PLUGIN_COMMAND" ]; then echo "Error: Command name was not specified" exit 1 fi PLUGIN_LOCAL_DIR="$USER_LOCAL_DIR/.command/$PLUGIN_COMMAND" if [ -d "$PLUGIN_LOCAL_DIR" ]; then rm -rf "$PLUGIN_LOCAL_DIR" rm -f "$USER_LOCAL_DIR/docker-$PLUGIN_COMMAND" echo "Successfully removed '$PLUGIN_COMMAND' client command" else if [ -e "$USER_LOCAL_DIR/docker-$PLUGIN_COMMAND" ]; then echo "Error: Client command '$PLUGIN_COMMAND' is not maintained by docker-clip" exit 1 fi echo "Error: Client command '$PLUGIN_COMMAND' is not installed" exit 1 fi } # # docker clip ls # cmd_ls() { printf "%-20s %s\n" COMMAND IMAGE while read PLUGIN_IMAGE_FILE; do PLUGIN_TAG=$(cat "$PLUGIN_IMAGE_FILE") PLUGIN_COMMAND=$(basename $(dirname "$PLUGIN_IMAGE_FILE")) printf "%-20s %s\n" "$PLUGIN_COMMAND" "$PLUGIN_TAG" done < <(find "$USER_LOCAL_DIR/.command" -name .plugin-image) } # # docker clip search # cmd_search() { PATTERN="$1" printf "COMMAND IMAGE DESCRIPTION\n" curl -sSfL https://raw.githubusercontent.com/lukaszlach/clip/master/clips/catalog | \ grep --color=always -ie "$PATTERN" exit $? } # # docker clip run # Used internally only # cmd_run() { PLUGIN_COMMAND="$1" PLUGIN_LOCAL_DIR="$USER_LOCAL_DIR/.command/$PLUGIN_COMMAND" shift if [[ "$1" == "docker-cli-plugin-metadata" ]]; then cat "$PLUGIN_LOCAL_DIR/docker-$PLUGIN_COMMAND.json" exit 0 fi exec "$PLUGIN_LOCAL_DIR/docker-$PLUGIN_COMMAND" "$@" exit 0 } usage() { cat <