From 5d34cf5be0617420e3f6b8a11219ea4a2978a6c4 Mon Sep 17 00:00:00 2001 From: Wenhao Ji Date: Thu, 21 May 2020 21:31:13 +0800 Subject: [PATCH] Added version option --- .gitignore | 1 + Makefile | 18 +++++++++++++----- bin/kubectl-tmux_exec | 27 +++++++++++++++++++++++++-- 3 files changed, 39 insertions(+), 7 deletions(-) diff --git a/.gitignore b/.gitignore index 16be8f2..1fae3f1 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ /output/ +VERSION diff --git a/Makefile b/Makefile index fdea73a..ca04096 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,7 @@ NAME=kubectl-tmux-exec -VERSION=0.1.0 +GIT_TAG := $(shell git describe --tags --abbrev=0) +GIT_COMMIT_ID := $(shell git rev-parse HEAD) +VERSION := $(GIT_TAG:v%=%) OUTPUT_DIR=output RELEASE_FILE_NAME=$(NAME)-$(VERSION).tar.gz @@ -8,6 +10,7 @@ SIG_FILE_NAME=$(NAME)-$(VERSION).asc SIG_FILE_PATH=$(OUTPUT_DIR)/$(SIG_FILE_NAME) CHECKSUM_FILE_NAME=$(RELEASE_FILE_NAME).sha256 CHECKSUM_FILE_PATH=$(OUTPUT_DIR)/$(CHECKSUM_FILE_NAME) +VERSION_FILE_PATH=VERSION ifeq ($(OS), Windows_NT) OS_UNAME := Windows @@ -15,12 +18,14 @@ else OS_UNAME := $(shell uname -s) endif -.PHONY: build sign checksum clean mk-output-dir test +.PHONY: build version sign checksum clean mk-output-dir test all: test $(RELEASE_FILE_PATH) $(CHECKSUM_FILE_PATH) build: $(RELEASE_FILE_PATH) +version: $(VERSION_FILE_PATH) + test: bats test/ @@ -32,10 +37,13 @@ mk-output-dir: mkdir -p $(OUTPUT_DIR) clean: - rm -rf $(RELEASE_FILE_PATH) $(CHECKSUM_FILE_PATH) $(SIG_FILE_PATH) + rm -rf $(VERSION_FILE_PATH) $(RELEASE_FILE_PATH) $(CHECKSUM_FILE_PATH) $(SIG_FILE_PATH) + +$(VERSION_FILE_PATH): + echo "$(VERSION) (commit = $(GIT_COMMIT_ID))" > $(VERSION_FILE_PATH) -$(RELEASE_FILE_PATH): mk-output-dir - tar czvf $(RELEASE_FILE_PATH) bin/ LICENSE +$(RELEASE_FILE_PATH): mk-output-dir $(VERSION_FILE_PATH) + tar czvf $(RELEASE_FILE_PATH) bin/ LICENSE $(VERSION_FILE_PATH) $(SIG_FILE_PATH): $(RELEASE_FILE_PATH) gpg -ab $(RELEASE_FILE_PATH) diff --git a/bin/kubectl-tmux_exec b/bin/kubectl-tmux_exec index b7ade5e..4abdde8 100755 --- a/bin/kubectl-tmux_exec +++ b/bin/kubectl-tmux_exec @@ -21,6 +21,15 @@ set -euf -o pipefail +# Stack Overflow: https://stackoverflow.com/a/246128/1122665 +SOURCE="${BASH_SOURCE[0]}" +while [[ -h "$SOURCE" ]]; do + PROG_DIR="$( cd -P "$( dirname "$SOURCE" )" >/dev/null 2>&1 && pwd )" + SOURCE="$(readlink "$SOURCE")" + [[ $SOURCE != /* ]] && SOURCE="$PROG_DIR/$SOURCE" +done +PROG_DIR="$( cd -P "$( dirname "$SOURCE" )" >/dev/null 2>&1 && pwd )" + readonly PROG_NAME='kubectl tmux-exec' declare -ra KUBECTL_SHORT_OPTS=( @@ -71,6 +80,7 @@ Examples: ${PROG_NAME} -f - /bin/bash # read from stdin Options: + -V, --version: Print the version information -c, --container='': Container name. If omitted, the first container in the pod will be chosen -i, --stdin=true: Pass stdin to the container (deprecated, since it's enabled by default) -t, --tty=true: Stdin is a TTY (deprecated, since it's enabled by default) @@ -88,6 +98,15 @@ Use "kubectl options" for a list of global command-line options (applies to all EOF } +function print_version() { + local version_file="${PROG_DIR}/../VERSION" + if [[ -f "${version_file}" ]]; then + cat "${version_file}" + else + echo "unknown" + fi +} + function check_required_executables() { for exe in "$@"; do if ! which "${exe}" 2>&1 >/dev/null; then @@ -137,8 +156,8 @@ function array_contains() { function main() { local opts - opts=$(ggetopt -o hitdc:l:f:"$(printf '%s:' "${KUBECTL_SHORT_OPTS[@]}")" --long \ - help,stdin,tty,detach,container:,selector:,remain-on-exit,select-layout:,file:,"$(printf '%s:,' "${KUBECTL_LONG_OPTS[@]}")","$(printf '%s,' "${KUBECTL_NOARG_LONG_OPTS[@]}")" -- "$@") + opts=$(ggetopt -o hVitdc:l:f:"$(printf '%s:' "${KUBECTL_SHORT_OPTS[@]}")" --long \ + help,version,stdin,tty,detach,container:,selector:,remain-on-exit,select-layout:,file:,"$(printf '%s:,' "${KUBECTL_LONG_OPTS[@]}")","$(printf '%s,' "${KUBECTL_NOARG_LONG_OPTS[@]}")" -- "$@") eval set -- $opts local selector @@ -155,6 +174,10 @@ function main() { usage exit 0 ;; + -V|--version) + print_version + exit 0 + ;; -c|--container) shift container_name="$1"