Skip to content

Commit 6acc633

Browse files
committed
Add --version flag
Signed-off-by: Aylei <[email protected]>
1 parent 71aea33 commit 6acc633

File tree

4 files changed

+63
-2
lines changed

4 files changed

+63
-2
lines changed

Makefile

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
.PHONY: build plugin agent check
22

3+
LDFLAGS = $(shell ./version.sh)
34
GOENV := GO15VENDOREXPERIMENT="1" GO111MODULE=on CGO_ENABLED=0 GOOS=linux GOARCH=amd64
45
GO := $(GOENV) go
56

@@ -8,13 +9,13 @@ default: build
89
build: plugin agent-docker
910

1011
plugin:
11-
GO111MODULE=on CGO_ENABLED=0 go build -o kubectl-debug cmd/plugin/main.go
12+
GO111MODULE=on CGO_ENABLED=0 go build -ldflags '$(LDFLAGS)' -o kubectl-debug cmd/plugin/main.go
1213

1314
agent-docker: agent
1415
docker build . -t aylei/debug-agent:latest
1516

1617
agent:
17-
$(GO) build -o debug-agent cmd/agent/main.go
18+
$(GO) build -ldflags '$(LDFLAGS)' -o debug-agent cmd/agent/main.go
1819

1920
check:
2021
find . -iname '*.go' -type f | grep -v /vendor/ | xargs gofmt -l

pkg/plugin/cmd.go

+5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"encoding/json"
66
"fmt"
7+
"github.com/aylei/kubectl-debug/version"
78
"io"
89
"net/http"
910
"net/url"
@@ -51,6 +52,9 @@ const (
5152
5253
# override the debug config file
5354
kubectl debug POD_NAME --debug-config ./debug-config.yml
55+
56+
# check version
57+
kubectl --version
5458
`
5559
longDesc = `
5660
Run a container in a running pod, this container will join the namespaces of an existing container of the pod.
@@ -137,6 +141,7 @@ func NewDebugCmd(streams genericclioptions.IOStreams) *cobra.Command {
137141
Short: "Run a container in a running pod",
138142
Long: longDesc,
139143
Example: example,
144+
Version: version.Version(),
140145
Run: func(c *cobra.Command, args []string) {
141146
argsLenAtDash := c.ArgsLenAtDash()
142147
cmdutil.CheckErr(opts.Complete(c, args, argsLenAtDash))

version.sh

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
4+
if [[ -n ${GIT_COMMIT-} ]] || GIT_COMMIT=$(git rev-parse "HEAD^{commit}" 2>/dev/null); then
5+
if [[ -z ${GIT_TREE_STATE-} ]]; then
6+
# Check if the tree is dirty. default to dirty
7+
if git_status=$(git status --porcelain 2>/dev/null) && [[ -z ${git_status} ]]; then
8+
GIT_TREE_STATE="clean"
9+
else
10+
GIT_TREE_STATE="dirty"
11+
fi
12+
fi
13+
14+
# Use git describe to find the version based on tags.
15+
if [[ -n ${GIT_VERSION-} ]] || GIT_VERSION=$(git describe --tags --abbrev=14 "${GIT_COMMIT}^{commit}" 2>/dev/null); then
16+
# This translates the "git describe" to an actual semver.org
17+
# compatible semantic version that looks something like this:
18+
# v1.0.0-beta.0.10+4c183422345d8f
19+
#
20+
# downstream consumers are expecting it there.
21+
DASHES_IN_VERSION=$(echo "${GIT_VERSION}" | sed "s/[^-]//g")
22+
if [[ "${DASHES_IN_VERSION}" == "---" ]] ; then
23+
# We have distance to subversion (v1.1.0-subversion-1-gCommitHash)
24+
GIT_VERSION=$(echo "${GIT_VERSION}" | sed "s/-\([0-9]\{1,\}\)-g\([0-9a-f]\{14\}\)$/.\1\+\2/")
25+
elif [[ "${DASHES_IN_VERSION}" == "--" ]] ; then
26+
# We have distance to base tag (v1.1.0-1-gCommitHash)
27+
GIT_VERSION=$(echo "${GIT_VERSION}" | sed "s/-g\([0-9a-f]\{14\}\)$/+\1/")
28+
fi
29+
if [[ "${GIT_TREE_STATE}" == "dirty" ]]; then
30+
# git describe --dirty only considers changes to existing files, but
31+
# that is problematic since new untracked .go files affect the build,
32+
# so use our idea of "dirty" from git status instead.
33+
GIT_VERSION+="-dirty"
34+
fi
35+
36+
37+
# If GIT_VERSION is not a valid Semantic Version, then refuse to build.
38+
if ! [[ "${GIT_VERSION}" =~ ^v([0-9]+)\.([0-9]+)(\.[0-9]+)?(-[0-9A-Za-z.-]+)?(\+[0-9A-Za-z.-]+)?$ ]]; then
39+
echo "GIT_VERSION should be a valid Semantic Version. Current value: ${GIT_VERSION}"
40+
echo "Please see more details here: https://semver.org"
41+
exit 1
42+
fi
43+
fi
44+
fi
45+
46+
echo "-X 'github.com/aylei/kubectl-debug/version.gitVersion=${GIT_VERSION}'"

version/version.go

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package version
2+
3+
var (
4+
gitVersion = "v0.0.0-master+$Format:%h$"
5+
)
6+
7+
func Version() string {
8+
return gitVersion
9+
}

0 commit comments

Comments
 (0)