Skip to content
This repository has been archived by the owner on Jul 19, 2022. It is now read-only.

Commit

Permalink
Support --version flag
Browse files Browse the repository at this point in the history
Fixes #41
  • Loading branch information
glyn committed Nov 4, 2019
1 parent 2212ca4 commit a28ed69
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 5 deletions.
14 changes: 10 additions & 4 deletions Makefile
Expand Up @@ -4,6 +4,12 @@ all: test

OUTPUT = ./irel
GO_SOURCES = $(shell find . -type f -name '*.go')
VERSION ?= $(shell cat VERSION)
GITSHA = $(shell git rev-parse HEAD)
GITDIRTY = $(shell git diff --quiet HEAD || echo "dirty")
LDFLAGS_VERSION = -X github.com/pivotal/image-relocation/pkg/irel.cli_version=$(VERSION) \
-X github.com/pivotal/image-relocation/pkg/irel.cli_gitsha=$(GITSHA) \
-X github.com/pivotal/image-relocation/pkg/irel.cli_gitdirty=$(GITDIRTY)

test:
GO111MODULE=on go test ./... -coverprofile=coverage.txt -covermode=atomic
Expand All @@ -20,9 +26,9 @@ gen-mocks: check-counterfeiter
counterfeiter -o pkg/registry/ggcr/registryclientfakes/fake_registry_client.go ./pkg/registry/ggcr RegistryClient

irel: $(GO_SOURCES)
GO111MODULE=on go build -o $(OUTPUT) cmd/irel/main.go
GO111MODULE=on go build -ldflags "$(LDFLAGS_VERSION)" -o $(OUTPUT) cmd/irel/main.go

release: $(GO_SOURCES) test
GOOS=darwin GOARCH=amd64 go build -o $(OUTPUT) cmd/irel/main.go && tar -czf irel-darwin-amd64.tgz $(OUTPUT) && rm -f $(OUTPUT)
GOOS=linux GOARCH=amd64 go build -o $(OUTPUT) cmd/irel/main.go && tar -czf irel-linux-amd64.tgz $(OUTPUT) && rm -f $(OUTPUT)
GOOS=windows GOARCH=amd64 go build -o $(OUTPUT).exe cmd/irel/main.go && zip -mq irel-windows-amd64.zip $(OUTPUT).exe && rm -f $(OUTPUT).exe
GOOS=darwin GOARCH=amd64 go build -ldflags "$(LDFLAGS_VERSION)" -o $(OUTPUT) cmd/irel/main.go && tar -czf irel-darwin-amd64.tgz $(OUTPUT) && rm -f $(OUTPUT)
GOOS=linux GOARCH=amd64 go build -ldflags "$(LDFLAGS_VERSION)" -o $(OUTPUT) cmd/irel/main.go && tar -czf irel-linux-amd64.tgz $(OUTPUT) && rm -f $(OUTPUT)
GOOS=windows GOARCH=amd64 go build -ldflags "$(LDFLAGS_VERSION)" -o $(OUTPUT).exe cmd/irel/main.go && zip -mq irel-windows-amd64.zip $(OUTPUT).exe && rm -f $(OUTPUT).exe
1 change: 1 addition & 0 deletions VERSION
@@ -0,0 +1 @@
0.7.0-snapshot
7 changes: 6 additions & 1 deletion cmd/irel/main.go
Expand Up @@ -23,7 +23,12 @@ import (
)

func main() {
if err := irel.Root.Execute(); err != nil {
cmd := irel.Root

cmd.Version = irel.CliVersion()
cmd.Flags().Bool("version", false, "display CLI version")

if err := cmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
Expand Down
35 changes: 35 additions & 0 deletions pkg/irel/env.go
@@ -0,0 +1,35 @@
/*
* Copyright (c) 2019-Present Pivotal Software, Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package irel

import "fmt"

var (
cli_version = "unknown"
cli_gitsha = "unknown sha"
cli_gitdirty = ""
)

func CliVersion() string {
var version string
if cli_gitdirty == "" {
version = fmt.Sprintf("%s (%s)", cli_version, cli_gitsha)
} else {
version = fmt.Sprintf("%s (%s, with local modifications)", cli_version, cli_gitsha)
}
return version
}
64 changes: 64 additions & 0 deletions pkg/irel/env_test.go
@@ -0,0 +1,64 @@
/*
* Copyright (c) 2019-Present Pivotal Software, Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package irel

import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

var _ = Describe("CliVersion", func() {
var version string

JustBeforeEach(func() {
version = CliVersion()
})

It("should default correctly", func() {
Expect(version).To(Equal("unknown (unknown sha)"))
})

Context("when version is known", func() {
BeforeEach(func() {
cli_version = "0.0.0"
})

It("should include the version", func() {
Expect(version).To(Equal("0.0.0 (unknown sha)"))
})

Context("when SHA is known", func() {
BeforeEach(func() {
cli_gitsha = "abc"
})

It("should include the SHA", func() {
Expect(version).To(Equal("0.0.0 (abc)"))
})

Context("when repo is dirty", func() {
BeforeEach(func() {
cli_gitdirty = "dirty"
})

It("should note the repo is dirty", func() {
Expect(version).To(Equal("0.0.0 (abc, with local modifications)"))
})
})
})
})
})
29 changes: 29 additions & 0 deletions pkg/irel/irel_suite_test.go
@@ -0,0 +1,29 @@
/*
* Copyright (c) 2019-Present Pivotal Software, Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package irel_test

import (
"testing"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

func TestIrel(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Irel Suite")
}

0 comments on commit a28ed69

Please sign in to comment.