Skip to content

Commit 5d34cf5

Browse files
committed
Added version option
1 parent eb60dd7 commit 5d34cf5

File tree

3 files changed

+39
-7
lines changed

3 files changed

+39
-7
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
/output/
2+
VERSION

Makefile

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
NAME=kubectl-tmux-exec
2-
VERSION=0.1.0
2+
GIT_TAG := $(shell git describe --tags --abbrev=0)
3+
GIT_COMMIT_ID := $(shell git rev-parse HEAD)
4+
VERSION := $(GIT_TAG:v%=%)
35

46
OUTPUT_DIR=output
57
RELEASE_FILE_NAME=$(NAME)-$(VERSION).tar.gz
@@ -8,19 +10,22 @@ SIG_FILE_NAME=$(NAME)-$(VERSION).asc
810
SIG_FILE_PATH=$(OUTPUT_DIR)/$(SIG_FILE_NAME)
911
CHECKSUM_FILE_NAME=$(RELEASE_FILE_NAME).sha256
1012
CHECKSUM_FILE_PATH=$(OUTPUT_DIR)/$(CHECKSUM_FILE_NAME)
13+
VERSION_FILE_PATH=VERSION
1114

1215
ifeq ($(OS), Windows_NT)
1316
OS_UNAME := Windows
1417
else
1518
OS_UNAME := $(shell uname -s)
1619
endif
1720

18-
.PHONY: build sign checksum clean mk-output-dir test
21+
.PHONY: build version sign checksum clean mk-output-dir test
1922

2023
all: test $(RELEASE_FILE_PATH) $(CHECKSUM_FILE_PATH)
2124

2225
build: $(RELEASE_FILE_PATH)
2326

27+
version: $(VERSION_FILE_PATH)
28+
2429
test:
2530
bats test/
2631

@@ -32,10 +37,13 @@ mk-output-dir:
3237
mkdir -p $(OUTPUT_DIR)
3338

3439
clean:
35-
rm -rf $(RELEASE_FILE_PATH) $(CHECKSUM_FILE_PATH) $(SIG_FILE_PATH)
40+
rm -rf $(VERSION_FILE_PATH) $(RELEASE_FILE_PATH) $(CHECKSUM_FILE_PATH) $(SIG_FILE_PATH)
41+
42+
$(VERSION_FILE_PATH):
43+
echo "$(VERSION) (commit = $(GIT_COMMIT_ID))" > $(VERSION_FILE_PATH)
3644

37-
$(RELEASE_FILE_PATH): mk-output-dir
38-
tar czvf $(RELEASE_FILE_PATH) bin/ LICENSE
45+
$(RELEASE_FILE_PATH): mk-output-dir $(VERSION_FILE_PATH)
46+
tar czvf $(RELEASE_FILE_PATH) bin/ LICENSE $(VERSION_FILE_PATH)
3947

4048
$(SIG_FILE_PATH): $(RELEASE_FILE_PATH)
4149
gpg -ab $(RELEASE_FILE_PATH)

bin/kubectl-tmux_exec

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,15 @@
2121

2222
set -euf -o pipefail
2323

24+
# Stack Overflow: https://stackoverflow.com/a/246128/1122665
25+
SOURCE="${BASH_SOURCE[0]}"
26+
while [[ -h "$SOURCE" ]]; do
27+
PROG_DIR="$( cd -P "$( dirname "$SOURCE" )" >/dev/null 2>&1 && pwd )"
28+
SOURCE="$(readlink "$SOURCE")"
29+
[[ $SOURCE != /* ]] && SOURCE="$PROG_DIR/$SOURCE"
30+
done
31+
PROG_DIR="$( cd -P "$( dirname "$SOURCE" )" >/dev/null 2>&1 && pwd )"
32+
2433
readonly PROG_NAME='kubectl tmux-exec'
2534

2635
declare -ra KUBECTL_SHORT_OPTS=(
@@ -71,6 +80,7 @@ Examples:
7180
${PROG_NAME} -f - /bin/bash # read from stdin
7281
7382
Options:
83+
-V, --version: Print the version information
7484
-c, --container='': Container name. If omitted, the first container in the pod will be chosen
7585
-i, --stdin=true: Pass stdin to the container (deprecated, since it's enabled by default)
7686
-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
8898
EOF
8999
}
90100

101+
function print_version() {
102+
local version_file="${PROG_DIR}/../VERSION"
103+
if [[ -f "${version_file}" ]]; then
104+
cat "${version_file}"
105+
else
106+
echo "unknown"
107+
fi
108+
}
109+
91110
function check_required_executables() {
92111
for exe in "$@"; do
93112
if ! which "${exe}" 2>&1 >/dev/null; then
@@ -137,8 +156,8 @@ function array_contains() {
137156

138157
function main() {
139158
local opts
140-
opts=$(ggetopt -o hitdc:l:f:"$(printf '%s:' "${KUBECTL_SHORT_OPTS[@]}")" --long \
141-
help,stdin,tty,detach,container:,selector:,remain-on-exit,select-layout:,file:,"$(printf '%s:,' "${KUBECTL_LONG_OPTS[@]}")","$(printf '%s,' "${KUBECTL_NOARG_LONG_OPTS[@]}")" -- "$@")
159+
opts=$(ggetopt -o hVitdc:l:f:"$(printf '%s:' "${KUBECTL_SHORT_OPTS[@]}")" --long \
160+
help,version,stdin,tty,detach,container:,selector:,remain-on-exit,select-layout:,file:,"$(printf '%s:,' "${KUBECTL_LONG_OPTS[@]}")","$(printf '%s,' "${KUBECTL_NOARG_LONG_OPTS[@]}")" -- "$@")
142161
eval set -- $opts
143162

144163
local selector
@@ -155,6 +174,10 @@ function main() {
155174
usage
156175
exit 0
157176
;;
177+
-V|--version)
178+
print_version
179+
exit 0
180+
;;
158181
-c|--container)
159182
shift
160183
container_name="$1"

0 commit comments

Comments
 (0)