Skip to content
This repository was archived by the owner on Jul 18, 2025. It is now read-only.

Commit 2925113

Browse files
authored
Merge pull request #11 from chris-crone/add-version-cmd
Add version command
2 parents 19c7114 + ce9148d commit 2925113

File tree

4 files changed

+55
-4
lines changed

4 files changed

+55
-4
lines changed

Dockerfile

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
ARG ALPINE_VERSION=3.7
22
ARG GO_VERSION=1.10.1
3+
ARG COMMIT=unknown
34
ARG TAG=unknown
45

56
FROM golang:${GO_VERSION}-alpine${ALPINE_VERSION} AS build
@@ -10,9 +11,11 @@ WORKDIR /go/src/github.com/docker/lunchbox/
1011
COPY . .
1112

1213
FROM build AS bin-build
14+
ARG COMMIT
1315
ARG TAG
14-
RUN make TAG=${TAG} bin-all
16+
RUN make COMMIT=${COMMIT} TAG=${TAG} bin-all
1517

1618
FROM build AS test
19+
ARG COMMIT
1720
ARG TAG
18-
RUN make TAG=${TAG} unit-test e2e-test
21+
RUN make COMMIT=${COMMIT} TAG=${TAG} unit-test e2e-test

Makefile

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ PKG_NAME := github.com/docker/lunchbox
22
BIN_NAME := docker-app
33

44
TAG ?= $(shell git describe --always --dirty)
5+
COMMIT ?= $(shell git rev-parse --short HEAD)
56

67
IMAGE_NAME := docker-app
78

@@ -12,9 +13,12 @@ IMAGE_BUILD_ARGS := \
1213
--build-arg ALPINE_VERSION=$(ALPINE_VERSION) \
1314
--build-arg GO_VERSION=$(GO_VERSION) \
1415
--build-arg BIN_NAME=$(BIN_NAME) \
16+
--build-arg COMMIT=$(COMMIT) \
1517
--build-arg TAG=$(TAG)
1618

17-
LDFLAGS := "-s -w"
19+
LDFLAGS := "-s -w \
20+
-X $(PKG_NAME)/internal.GitCommit=$(COMMIT) \
21+
-X $(PKG_NAME)/internal.Version=$(TAG)"
1822

1923
#####################
2024
# Local Development #
@@ -59,7 +63,7 @@ unit-test:
5963
go test $(shell go list ./... | grep -vE '/vendor/|/e2e')
6064

6165
clean:
62-
rm -Rf ./_build
66+
rm -Rf ./_build docker-app-*.tar.gz
6367

6468
######
6569
# CI #

cmd/version.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/docker/lunchbox/internal"
7+
"github.com/spf13/cobra"
8+
)
9+
10+
var versionCmd = &cobra.Command{
11+
Use: "version",
12+
Short: "Print version information",
13+
Run: func(cmd *cobra.Command, args []string) {
14+
fmt.Println(internal.FullVersion())
15+
},
16+
}
17+
18+
func init() {
19+
rootCmd.AddCommand(versionCmd)
20+
}

internal/version.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package internal
2+
3+
import (
4+
"fmt"
5+
"runtime"
6+
"strings"
7+
)
8+
9+
var (
10+
// Version is the git tag that this was built from.
11+
Version = "unknown"
12+
// GitCommit is the commit that this was built from.
13+
GitCommit = "unknown"
14+
)
15+
16+
// FullVersion returns a string of version information.
17+
func FullVersion() string {
18+
res := []string{
19+
fmt.Sprintf("Version: %s", Version),
20+
fmt.Sprintf("Git commit: %s", GitCommit),
21+
fmt.Sprintf("OS/Arch: %s/%s", runtime.GOOS, runtime.GOARCH),
22+
}
23+
return strings.Join(res, "\n")
24+
}

0 commit comments

Comments
 (0)