Skip to content

Commit 97663e3

Browse files
committed
Add a version command
It prints the version, git commit, build time, go version used and os/arch :) Signed-off-by: Vincent Demeester <[email protected]>
1 parent 72987e0 commit 97663e3

File tree

5 files changed

+75
-1
lines changed

5 files changed

+75
-1
lines changed

Diff for: cli/app/version.go

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package app
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"runtime"
7+
"text/template"
8+
9+
"github.com/Sirupsen/logrus"
10+
"github.com/codegangsta/cli"
11+
"github.com/docker/libcompose/version"
12+
)
13+
14+
var versionTemplate = `Version: {{.Version}} ({{.GitCommit}})
15+
Go version: {{.GoVersion}}
16+
Built: {{.BuildTime}}
17+
OS/Arch: {{.Os}}/{{.Arch}}`
18+
19+
// Version prints the libcompose version number and additionnal informations.
20+
func Version(c *cli.Context) {
21+
if c.Bool("short") {
22+
fmt.Println(version.VERSION)
23+
return
24+
}
25+
26+
tmpl, err := template.New("").Parse(versionTemplate)
27+
if err != nil {
28+
logrus.Fatal(err)
29+
}
30+
31+
v := struct {
32+
Version string
33+
GitCommit string
34+
GoVersion string
35+
BuildTime string
36+
Os string
37+
Arch string
38+
}{
39+
Version: version.VERSION,
40+
GitCommit: version.GITCOMMIT,
41+
GoVersion: runtime.Version(),
42+
BuildTime: version.BUILDTIME,
43+
Os: runtime.GOOS,
44+
Arch: runtime.GOARCH,
45+
}
46+
47+
if err := tmpl.Execute(os.Stdout, v); err != nil {
48+
logrus.Fatal(err)
49+
}
50+
fmt.Printf("\n")
51+
return
52+
}

Diff for: cli/command/command.go

+15
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,21 @@ func UnpauseCommand(factory app.ProjectFactory) cli.Command {
276276
}
277277
}
278278

279+
// VersionCommand defines the libcompose version subcommand.
280+
func VersionCommand(factory app.ProjectFactory) cli.Command {
281+
return cli.Command{
282+
Name: "version",
283+
Usage: "Show version informations",
284+
Action: app.Version,
285+
Flags: []cli.Flag{
286+
cli.BoolFlag{
287+
Name: "short",
288+
Usage: "Shows only Compose's version number.",
289+
},
290+
},
291+
}
292+
}
293+
279294
// CommonFlags defines the flags that are in common for all subcommands.
280295
func CommonFlags() []cli.Flag {
281296
return []cli.Flag{

Diff for: cli/main/main.go

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ func main() {
3939
command.StopCommand(factory),
4040
command.UnpauseCommand(factory),
4141
command.UpCommand(factory),
42+
command.VersionCommand(factory),
4243
}
4344

4445
app.Run(os.Args)

Diff for: script/binary

+4-1
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@ rm -f libcompose-cli
66

77
go generate
88

9+
BUILDTIME=$(date --rfc-3339 ns | sed -e 's/ /T/') &> /dev/null
10+
GITCOMMIT=$(git rev-parse --short HEAD)
11+
912
# Build binaries
1013
go build \
11-
-ldflags="-w -X github.com/docker/libcompose/version.GITCOMMIT=`git rev-parse --short HEAD`" \
14+
-ldflags="-w -X github.com/docker/libcompose/version.GITCOMMIT=${GITCOMMIT} -X github.com/docker/libcompose/version.BUILDTIME=${BUILDTIME}" \
1215
-o bundles/libcompose-cli \
1316
./cli/main

Diff for: version/version.go

+3
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,7 @@ var (
66

77
// GITCOMMIT will be overwritten automatically by the build system
88
GITCOMMIT = "HEAD"
9+
10+
// BUILDTIME will be overwritten automatically by the build system
11+
BUILDTIME = ""
912
)

0 commit comments

Comments
 (0)