Skip to content
This repository has been archived by the owner on Nov 16, 2020. It is now read-only.

Add version command #289

Merged
merged 1 commit into from
Mar 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# To be bumped by every release
VERSION_MAJOR ?= 0
VERSION_MINOR ?= 1
VERSION_BUILD ?= 7
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're going to forget to do this. I'd like a future version of this change to pull the version from a file which effectively is written by the build system.


VERSION ?= v$(VERSION_MAJOR).$(VERSION_MINOR).$(VERSION_BUILD)

GO ?= go
GOVERSION ?= go1.9
OS := $(shell uname)
Expand All @@ -10,6 +17,13 @@ GOPATH := $(firstword $(subst :, ,$(GOPATH)))
SWAGGER := $(GOPATH)/bin/swagger
GOBINDATA := $(GOPATH)/bin/go-bindata
BUILD := $(shell date +%s)
VERSION_PACKAGE := github.com/vmware/dispatch/pkg/version

GO_LDFLAGS :="
GO_LDFLAGS += -X $(VERSION_PACKAGE).version=$(VERSION)
GO_LDFLAGS += -X $(VERSION_PACKAGE).buildDate=$(shell date +'%Y-%m-%dT%H:%M:%SZ')
GO_LDFLAGS += -X $(VERSION_PACKAGE).commit=$(shell git rev-parse HEAD)
GO_LDFLAGS +="

PKGS := pkg

Expand Down Expand Up @@ -85,16 +99,16 @@ linux: $(LINUX_BINS)
darwin: $(DARWIN_BINS)

$(LINUX_BINS):
GOOS=linux go build -o bin/$@ ./cmd/$(subst -linux,,$@)
GOOS=linux go build -ldflags $(GO_LDFLAGS) -o bin/$@ ./cmd/$(subst -linux,,$@)

$(DARWIN_BINS):
GOOS=darwin go build -o bin/$@ ./cmd/$(subst -darwin,,$@)
GOOS=darwin go build -ldflags $(GO_LDFLAGS) -o bin/$@ ./cmd/$(subst -darwin,,$@)

cli-darwin:
GOOS=darwin go build -o bin/$(CLI)-darwin ./cmd/$(CLI)
GOOS=darwin go build -ldflags $(GO_LDFLAGS) -o bin/$(CLI)-darwin ./cmd/$(CLI)

cli-linux:
GOOS=linux go build -o bin/$(CLI)-linux ./cmd/$(CLI)
GOOS=linux go build -ldflags $(GO_LDFLAGS) -o bin/$(CLI)-linux ./cmd/$(CLI)

.PHONY: images
images: linux ci-images
Expand Down
1 change: 1 addition & 0 deletions pkg/dispatchcli/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ func NewCLI(in io.Reader, out, errOut io.Writer) *cobra.Command {
cmds.AddCommand(NewCmdEmit(out, errOut))
cmds.AddCommand(NewCmdInstall(out, errOut))
cmds.AddCommand(NewCmdUninstall(out, errOut))
cmds.AddCommand(NewCmdVersion(out))
return cmds
}

Expand Down
29 changes: 29 additions & 0 deletions pkg/dispatchcli/cmd/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
///////////////////////////////////////////////////////////////////////
// Copyright (c) 2017 VMware, Inc. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
///////////////////////////////////////////////////////////////////////

package cmd

import (
"fmt"
"io"

"github.com/spf13/cobra"

"github.com/vmware/dispatch/pkg/dispatchcli/i18n"
"github.com/vmware/dispatch/pkg/version"
)

// NewCmdVersion creates a version command for CLI
func NewCmdVersion(out io.Writer) *cobra.Command {
cmd := &cobra.Command{
Use: "version",
Short: i18n.T("Print Dispatch CLI version."),
Run: func(cmd *cobra.Command, args []string) {
v := version.Get()
fmt.Fprintf(out, "%s-%s\n", v.Version, v.Commit[0:7])
},
}
return cmd
}
42 changes: 42 additions & 0 deletions pkg/version/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
///////////////////////////////////////////////////////////////////////
// Copyright (c) 2017 VMware, Inc. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
///////////////////////////////////////////////////////////////////////

package version

import (
"fmt"
"runtime"
)

var (
version string
commit string
buildDate string
)

// NO TESTS

// BuildInfo describes build metadata
type BuildInfo struct {
Version string
Commit string
BuildDate string
GoVersion string
Compiler string
Platform string
}

// Get returns information about the build
func Get() *BuildInfo {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How can we get the server version?

// Filled by -ldflags passed to `go build`
return &BuildInfo{
Version: version,
Commit: commit,
BuildDate: buildDate,
GoVersion: runtime.Version(),
Compiler: runtime.Compiler,
Platform: fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH),
}
}