Skip to content

Commit 7580efb

Browse files
committed
Display version flag
1 parent 9a44d27 commit 7580efb

File tree

5 files changed

+29
-3
lines changed

5 files changed

+29
-3
lines changed

.dockerignore

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
.git
21
Dockerfile
32
README.md
43
docker-bake.hcl

Dockerfile

+5-1
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,13 @@ FROM --platform=$BUILDPLATFORM golang:1.15-alpine AS binfmt
9090
COPY --from=xgo / /
9191
ENV CGO_ENABLED=0
9292
ARG TARGETPLATFORM
93+
ARG QEMU_VERSION
9394
WORKDIR /src
95+
RUN apk add --no-cache git
9496
RUN --mount=target=. \
95-
TARGETPLATFORM=$TARGETPLATFORM go build -o /go/bin/binfmt ./cmd/binfmt
97+
TARGETPLATFORM=$TARGETPLATFORM go build \
98+
-ldflags "-X main.revision=$(git rev-parse --short HEAD) -X main.qemuVersion=${QEMU_VERSION}" \
99+
-o /go/bin/binfmt ./cmd/binfmt
96100

97101
FROM scratch AS binaries
98102
ARG BINARY_PREFIX

README.md

+9
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,15 @@ You can also uninstall all archs for a specific emulator:
6767
docker run --privileged --rm tonistiigi/binfmt --uninstall qemu-*
6868
```
6969

70+
## Display version
71+
72+
```bash
73+
docker run --privileged --rm tonistiigi/binfmt --version
74+
```
75+
```
76+
binfmt/9a44d27 qemu/v6.0.0 go/1.15.11
77+
```
78+
7079
## Development commands
7180

7281
```bash

cmd/binfmt/main.go

+9-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"log"
99
"os"
1010
"path/filepath"
11+
"runtime"
1112
"strings"
1213
"syscall"
1314

@@ -20,12 +21,14 @@ var (
2021
mount string
2122
toInstall string
2223
toUninstall string
24+
flVersion bool
2325
)
2426

2527
func init() {
2628
flag.StringVar(&mount, "mount", "/proc/sys/fs/binfmt_misc", "binfmt_misc mount point")
2729
flag.StringVar(&toInstall, "install", "", "architectures to install")
2830
flag.StringVar(&toUninstall, "uninstall", "", "architectures to uninstall")
31+
flag.BoolVar(&flVersion, "version", false, "display version")
2932
}
3033

3134
func uninstall(arch string) error {
@@ -155,14 +158,19 @@ func parseUninstall(in string) (out []string) {
155158
}
156159

157160
func main() {
161+
log.SetFlags(0) // no timestamps in logs
158162
flag.Parse()
159-
160163
if err := run(); err != nil {
161164
log.Printf("error: %+v", err)
162165
}
163166
}
164167

165168
func run() error {
169+
if flVersion {
170+
log.Printf("binfmt/%s qemu/%s go/%s", revision, qemuVersion, runtime.Version()[2:])
171+
return nil
172+
}
173+
166174
if _, err := os.Stat(filepath.Join(mount, "status")); err != nil {
167175
if err := syscall.Mount("binfmt_misc", mount, "binfmt_misc", 0, ""); err != nil {
168176
return errors.Wrapf(err, "cannot mount binfmt_misc filesystem at %s", mount)

cmd/binfmt/version.go

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package main
2+
3+
var (
4+
revision = "unknown"
5+
qemuVersion = "unknown"
6+
)

0 commit comments

Comments
 (0)