Skip to content

Commit

Permalink
Merge pull request #30 from Peripli/inject-version
Browse files Browse the repository at this point in the history
Inject version
  • Loading branch information
dotchev authored Jul 8, 2019
2 parents 8bddaa8 + 068caf4 commit 4d8147d
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 23 deletions.
10 changes: 5 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,19 @@ script:
# - gas ./...

# Linux i686 build
- CGO_ENABLED=0 GOARCH=386 GOOS=linux go build -o out/smctl_linux_i686 .
- CGO_ENABLED=0 GOARCH=386 GOOS=linux go build -ldflags "$(build/ldflags)" -o out/smctl_linux_i686 .

# Linux x86-64 build
- CGO_ENABLED=0 GOARCH=amd64 GOOS=linux go build -o out/smctl_linux_x86-64 .
- CGO_ENABLED=0 GOARCH=amd64 GOOS=linux go build -ldflags "$(build/ldflags)" -o out/smctl_linux_x86-64 .

# OSX build
- GOARCH=amd64 GOOS=darwin go build -o out/smctl_osx .
- GOARCH=amd64 GOOS=darwin go build -ldflags "$(build/ldflags)" -o out/smctl_osx .

# Windows x32 build
- GOARCH=386 GOOS=windows go build -o out/smctl_win32.exe .
- GOARCH=386 GOOS=windows go build -ldflags "$(build/ldflags)" -o out/smctl_win32.exe .

# Windows x64 build
- GOARCH=amd64 GOOS=windows go build -o out/smctl_win64.exe .
- GOARCH=amd64 GOOS=windows go build -ldflags "$(build/ldflags)" -o out/smctl_win64.exe .

deploy:
provider: releases
Expand Down
9 changes: 9 additions & 0 deletions build/ldflags
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/sh

# this script injects the version by generating proper go build options

VERSION_PACKAGE=github.com/Peripli/service-manager-cli/internal/cmd/version
GIT_COMMIT=$(git rev-list -1 HEAD)
VERSION=$(git describe --tags)
>&2 echo Version=$VERSION GitCommit=$GIT_COMMIT
echo "-X $VERSION_PACKAGE.GitCommit=$GIT_COMMIT -X $VERSION_PACKAGE.Version=$VERSION"
7 changes: 4 additions & 3 deletions internal/cmd/broker/delete_broker.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@ package broker

import (
"fmt"
"github.com/Peripli/service-manager-cli/internal/output"
"github.com/Peripli/service-manager-cli/pkg/errors"
"io"
"net/http"
"strings"

"github.com/Peripli/service-manager-cli/internal/output"
"github.com/Peripli/service-manager-cli/pkg/errors"

"github.com/Peripli/service-manager-cli/internal/util"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -66,7 +67,7 @@ func (dbc *DeleteBrokerCmd) Validate(args []string) error {
func (dbc *DeleteBrokerCmd) Run() error {
fieldQuery := util.GetResourceByNamesQuery(dbc.names)
err := dbc.Client.DeleteBrokersByFieldQuery(fieldQuery)
if respErr, ok := err.(errors.ResponseError); ok && respErr.StatusCode == http.StatusNotFound{
if respErr, ok := err.(errors.ResponseError); ok && respErr.StatusCode == http.StatusNotFound {
output.PrintMessage(dbc.Output, "Service Broker(s) not found.\n")
return nil
} else if err != nil {
Expand Down
5 changes: 3 additions & 2 deletions internal/cmd/platform/delete_platform.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ package platform

import (
"fmt"
"github.com/Peripli/service-manager-cli/pkg/errors"
"io"
"net/http"
"strings"

"github.com/Peripli/service-manager-cli/pkg/errors"

"github.com/Peripli/service-manager-cli/internal/output"
"github.com/Peripli/service-manager-cli/internal/util"

Expand Down Expand Up @@ -61,7 +62,7 @@ func (dpc *DeletePlatformCmd) Validate(args []string) error {
func (dpc *DeletePlatformCmd) Run() error {
fieldQuery := util.GetResourceByNamesQuery(dpc.names)
err := dpc.Client.DeletePlatformsByFieldQuery(fieldQuery)
if respErr, ok := err.(errors.ResponseError); ok && respErr.StatusCode == http.StatusNotFound{
if respErr, ok := err.(errors.ResponseError); ok && respErr.StatusCode == http.StatusNotFound {
output.PrintMessage(dpc.Output, "Platform(s) not found.\n")
return nil
} else if err != nil {
Expand Down
16 changes: 10 additions & 6 deletions internal/cmd/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,17 @@ import (
// Cmd wraps the smctl version command
type Cmd struct {
*cmd.Context

clientVersion string
}

// NewVersionCmd returns new version command with context and client version
func NewVersionCmd(context *cmd.Context, clientVersion string) *Cmd {
return &Cmd{context, clientVersion}
// Version is the tool version, injected by the build
var Version = "local.build"

// GitCommit is the git commit id, injected by the build
var GitCommit string

// NewVersionCmd returns new version command
func NewVersionCmd(context *cmd.Context) *Cmd {
return &Cmd{context}
}

// Prepare returns cobra command
Expand All @@ -52,7 +56,7 @@ func (vc *Cmd) Prepare(prepare cmd.PrepareFunc) *cobra.Command {

// Run runs command's logic
func (vc *Cmd) Run() error {
output.PrintMessage(vc.Output, "Service Manager Client %s\n", vc.clientVersion)
output.PrintMessage(vc.Output, "Service Manager Client %s (%s)\n", Version, GitCommit)

return nil
}
9 changes: 5 additions & 4 deletions internal/cmd/version/version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ var _ = Describe("Login Command test", func() {

var command *Cmd
var buffer *bytes.Buffer
var clientVersion string

BeforeEach(func() {
buffer = &bytes.Buffer{}
context := &cmd.Context{Output: buffer}
clientVersion = "TEST VERSION"
command = NewVersionCmd(context, clientVersion)
Version = "v1.2.3"
GitCommit = "987654321"
command = NewVersionCmd(context)
})

Describe("Valid request", func() {
Expand All @@ -35,7 +35,8 @@ var _ = Describe("Login Command test", func() {
vc := command.Prepare(cmd.CommonPrepare)
err := vc.Execute()

Expect(buffer.String()).To(Equal(fmt.Sprintf("Service Manager Client %s\n", clientVersion)))
Expect(buffer.String()).To(Equal(fmt.Sprintf("Service Manager Client %s (%s)\n",
Version, GitCommit)))
Expect(err).ShouldNot(HaveOccurred())
})
})
Expand Down
4 changes: 1 addition & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,14 @@ func oidcAuthBuilder(options *auth.Options) (auth.Authenticator, *auth.Options,
}

func main() {
clientVersion := "0.0.1"

context := &cmd.Context{}
rootCmd := cmd.BuildRootCommand(context)
fs := afero.NewOsFs()

normalCommandsGroup := cmd.Group{
Commands: []cmd.CommandPreparator{
login.NewLoginCmd(context, os.Stdin, oidcAuthBuilder),
version.NewVersionCmd(context, clientVersion),
version.NewVersionCmd(context),
info.NewInfoCmd(context),
},
PrepareFn: cmd.CommonPrepare,
Expand Down

0 comments on commit 4d8147d

Please sign in to comment.