Skip to content

Commit

Permalink
Version command (#38)
Browse files Browse the repository at this point in the history
Added Makefile and version command.
  • Loading branch information
marcinc authored Jun 11, 2020
1 parent b2252f8 commit f8ab419
Show file tree
Hide file tree
Showing 9 changed files with 314 additions and 32 deletions.
128 changes: 128 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
SHELL=/bin/sh -e
NAME=kev
AUTHOR=appvia
AUTHOR_EMAIL[email protected]
BUILD_TIME=$(shell date '+%s')
CURRENT_TAG=$(shell git tag --points-at HEAD)
GIT_BRANCH=$(shell git rev-parse --abbrev-ref HEAD)
GIT_SHA=$(shell git --no-pager describe --always --dirty)
GIT_LAST_TAG_SHA=$(shell git rev-list --tags='v[0.9]*.[0-9]*.[0-9]*' --max-count=1)
GIT_LAST_TAG=$(shell git describe --tags $(GIT_LAST_TAG_SHA))
HARDWARE=$(shell uname -m)
PACKAGES=$(shell go list ./...)
VETARGS ?= -asmdecl -atomic -bool -buildtags -copylocks -methods -nilfunc -printf -rangeloops -unsafeptr
ifeq ($(USE_GIT_VERSION),true)
ifeq ($(CURRENT_TAG),)
VERSION ?= $(GIT_LAST_TAG)-$(GIT_SHA)
else
VERSION ?= $(CURRENT_TAG)
endif
else
VERSION ?= $(GIT_LAST_TAG)
endif
LFLAGS ?= -X github.com/appvia/kube-devx/pkg/version.Tag=${GIT_LAST_TAG} -X github.com/appvia/kube-devx/pkg/version.GitSHA=${GIT_SHA} -X github.com/appvia/kube-devx/pkg/version.Compiled=${BUILD_TIME} -X github.com/appvia/kube-devx/pkg/version.Release=${VERSION} -X github.com/appvia/kube-devx/pkg/version.GitBranch=${GIT_BRANCH}
CLI_PLATFORMS=darwin linux windows
CLI_ARCHITECTURES=386 amd64
export GOFLAGS = -mod=vendor

.PHONY: test authors changelog build release check vet golangci-lint

default: build

golang:
@echo "--> Go Version"
@go version
@echo "GOFLAGS: $$GOFLAGS"

build: golang
@echo "--> Compiling the project ($(VERSION))"
@mkdir -p bin
go build -ldflags "${LFLAGS}" -tags=jsoniter -o bin/${NAME} cmd/${NAME}/*.go || exit 1;

package:
@rm -rf ./release
@mkdir ./release
@$(MAKE) package-cli
cd ./release && sha256sum * > kev.sha256sums

package-cli:
@echo "--> Compiling CLI static binaries"
CGO_ENABLED=0 go run github.com/mitchellh/gox -parallel=4 -arch="${CLI_ARCHITECTURES}" -os="${CLI_PLATFORMS}" -ldflags "-w ${LFLAGS}" -output=./release/{{.Dir}}-cli-{{.OS}}-{{.Arch}} ./cmd/${NAME}/

push-release-packages:
@echo "--> Pushing compiled CLI binaries to draft release (requires github token set in .gitconfig or GITHUB_TOKEN env variable)"
go run github.com/tcnksm/ghr -replace -draft -n "Release ${VERSION}" "${VERSION}" ./release

release: build
mkdir -p release
gzip -c bin/${NAME} > release/${NAME}_${VERSION}_linux_${HARDWARE}.gz
rm -f release/${NAME}

clean:
@echo "--> Cleaning up the environment"
rm -rf ./bin 2>/dev/null
rm -rf ./release 2>/dev/null

authors:
@echo "--> Updating the AUTHORS"
git log --format='%aN <%aE>' | sort -u > AUTHORS

vet:
@echo "--> Running go vet $(VETARGS) $(PACKAGES)"
@go vet $(VETARGS) $(PACKAGES)

gofmt:
@echo "--> Running gofmt check"
@if gofmt -s -l $$(go list -f '{{.Dir}}' ./...) | grep -q \.go ; then \
echo "You need to run the make format, we have file unformatted"; \
gofmt -s -l $$(go list -f '{{.Dir}}' ./...); \
exit 1; \
fi

format:
@echo "--> Running go fmt"
@gofmt -s -w $$(go list -f '{{.Dir}}' ./...)

bench:
@echo "--> Running go bench"
@go test -bench=. -benchmem

verify-licence:
@echo "--> Verifying the licence headers"
@hack/verify-licence.sh

coverage:
@echo "--> Running go coverage"
@go test -coverprofile cover.out
@go tool cover -html=cover.out -o cover.html

spelling:
@echo "--> Checking the spelling"
@find . -name "*.go" -type f -not -path "./vendor/*" -not -path "./ui/node_modules/*" | xargs go run github.com/client9/misspell/cmd/misspell -error -source=go *.go
@find . -name "*.md" -type f -not -path "./vendor/*" -not -path "./ui/node_modules/*" | xargs go run github.com/client9/misspell/cmd/misspell -error -source=text *.md

golangci-lint:
@echo "--> Checking against the golangci-lint"
@go run github.com/golangci/golangci-lint/cmd/golangci-lint run --timeout 5m -j 2 ./...

check:
@echo "--> Running code checkers"
@$(MAKE) golang
@$(MAKE) gofmt
@$(MAKE) golangci-lint
@$(MAKE) spelling
@$(MAKE) vet
@$(MAKE) verify-licences
@$(MAKE) check-generate-assets

test:
@echo "--> Running the tests"
@go test --cover -v $(PACKAGES)

all: test
@echo "--> Performing all tests"
@$(MAKE) bench
@$(MAKE) coverage

changelog: release
git log $(shell git tag | tail -n1)..HEAD --no-merges --format=%B >> changelog
1 change: 1 addition & 0 deletions cmd/kev/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ func init() {
})
}

// Execute command
func Execute() {
if err := rootCmd.Execute(); err != nil {
if err != silentErr {
Expand Down
37 changes: 37 additions & 0 deletions cmd/kev/cmd/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* Copyright 2020 Appvia Ltd <[email protected]>
*
* 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 cmd

import (
"fmt"

"github.com/appvia/kube-devx/pkg/version"
"github.com/spf13/cobra"
)

func init() {
rootCmd.AddCommand(versionCmd)
}

var versionCmd = &cobra.Command{
Use: "version",
Short: "Print Kev version",
Long: `All software has versions. This is Kev's`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println(version.Version())
},
}
8 changes: 2 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,11 @@ require (
github.com/compose-spec/compose-go v0.0.0-20200608091248-554a39de0b3a
github.com/disiqueira/gotree v1.0.0
github.com/fatih/color v1.9.0 // indirect
github.com/fsnotify/fsnotify v1.4.9 // indirect
github.com/goccy/go-yaml v1.7.5
github.com/kr/text v0.2.0 // indirect
github.com/mattn/go-colorable v0.1.6 // indirect
github.com/mitchellh/mapstructure v1.3.2 // indirect
github.com/pelletier/go-toml v1.8.0 // indirect
github.com/spf13/afero v1.2.2 // indirect
github.com/spf13/cast v1.3.1 // indirect
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
github.com/spf13/cobra v1.0.0
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/spf13/viper v1.7.0 // indirect
golang.org/x/sys v0.0.0-20200602225109-6fdc65e7d980 // indirect
Expand Down
Loading

0 comments on commit f8ab419

Please sign in to comment.