Skip to content

Commit

Permalink
Merge pull request #14 from datawire/ark3/version-number
Browse files Browse the repository at this point in the history
Add tag-based version numbers to all the commands
  • Loading branch information
Abhay Saxena authored Jan 10, 2019
2 parents 28bf9d9 + dd9cf88 commit 74af28a
Show file tree
Hide file tree
Showing 17 changed files with 166 additions and 224 deletions.
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
*
!image
!intercept
!entrypoint.sh
11 changes: 6 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
NAME=ambassador-ratelimit
PROFILE ?= dev
include config.mk

include build-aux/common.mk
include build-aux/go-mod.mk
include build-aux/go-version.mk
include build-aux/help.mk
include build-aux/teleproxy.mk
include build-aux/kubernaut-ui.mk
include build-aux/kubeapply.mk

include build-aux/k8s.mk

export PATH:=$(CURDIR)/bin_$(GOOS)_$(GOARCH):$(PATH)
Expand Down Expand Up @@ -40,16 +41,16 @@ build-image: $(addprefix image/,$(notdir $(go.bins)))
image/%: bin_linux_amd64/%
@mkdir -p $(@D)
cp $< $@
docker: env build-image
docker: build-image
docker build . -t $(RATELIMIT_IMAGE)
docker build intercept --target telepresence-proxy -t $(PROXY_IMAGE)
docker build intercept --target telepresence-sidecar -t $(SIDECAR_IMAGE)
docker build . -f intercept/Dockerfile --target telepresence-proxy -t $(PROXY_IMAGE)
docker build . -f intercept/Dockerfile --target telepresence-sidecar -t $(SIDECAR_IMAGE)
.PHONY: docker

# Utility targets
docker-run: docker
docker run -it $(IMAGE)
.PHONY: docker-run

clean: $(CLUSTER).clean
clean:
rm -rf image
165 changes: 0 additions & 165 deletions build-aux/env.go

This file was deleted.

3 changes: 1 addition & 2 deletions build-aux/go-version.mk
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@
## Outputs ##
# - Variable: go.LDFLAGS += …
ifeq ($(words $(filter $(abspath $(lastword $(MAKEFILE_LIST))),$(abspath $(MAKEFILE_LIST)))),1)
include $(dir $(lastword $(MAKEFILE_LIST)))common.mk
include $(dir $(lastword $(MAKEFILE_LIST)))version.mk

VERSION ?= $(patsubst v%,%,$(shell git describe --tags --always --dirty))
go.LDFLAGS += -X main.Version=$(VERSION)

endif
13 changes: 1 addition & 12 deletions build-aux/k8s.mk
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,13 @@ ifeq ($(words $(filter $(abspath $(lastword $(MAKEFILE_LIST))),$(abspath $(MAKEF
include $(dir $(lastword $(MAKEFILE_LIST)))kubernaut-ui.mk
include $(dir $(lastword $(MAKEFILE_LIST)))kubeapply.mk

PROFILE?=dev

IMAGE_VARS=$(filter %_IMAGE,$(.VARIABLES))
IMAGES=$(foreach var,$(IMAGE_VARS),$($(var)))
IMAGE_DEFS=$(foreach var,$(IMAGE_VARS),$(var)=$($(var))$(NL))
IMAGE_DEFS_SH="$(subst $(SPACE),\n,$(foreach var,$(IMAGE_VARS),$(var)=$($(var))))\n"
MANIFESTS?=$(wildcard k8s/*.yaml)

env:
$(eval $(subst @NL,$(NL), $(shell go run build-aux/env.go -profile $(PROFILE) -newline "@NL" -input config.json)))
.PHONY: env

hash: ## Show the computed version hash. The hash is based on non gitignored files.
hash: env
@echo HASH=$(HASH)
.PHONY: hash

push_ok: env
push_ok:
@if [ "$(PROFILE)" == "prod" ]; then echo "CANNOT PUSH TO PROD"; exit 1; fi
.PHONY: push_ok

Expand Down
3 changes: 2 additions & 1 deletion build-aux/kubernaut.mk
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,10 @@
# clean: test-cluster.knaut.clean
#
ifeq ($(words $(filter $(abspath $(lastword $(MAKEFILE_LIST))),$(abspath $(MAKEFILE_LIST)))),1)
_kubernaut.mk := $(lastword $(MAKEFILE_LIST))
include $(dir $(lastword $(MAKEFILE_LIST)))common.mk

GUBERNAUT = GO111MODULE=off go run build-aux/gubernaut.go
GUBERNAUT = GO111MODULE=off go run $(dir $(_kubernaut.mk))gubernaut.go

%.knaut.claim:
echo $(subst /,_,$*)-$${USER}-$$(uuidgen) > $@
Expand Down
77 changes: 77 additions & 0 deletions build-aux/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// +build ignore

package main

import (
"crypto/md5"
"fmt"
"io"
"os"
"os/exec"
"strings"
)

var Verbose bool

func die(err error, args ...interface{}) {
if err != nil {
if args != nil {
panic(fmt.Errorf("%v: %v", err, args))
} else {
panic(err)
}
}
}

func main() {
fmt.Printf("%x\n", hash())
}

func hash() []byte {
standard, err := shell("git ls-files --exclude-standard")
die(err)
others, err := shell("git ls-files --exclude-standard --others")
die(err)

files := append(standard, others...)

h := md5.New()
for _, file := range files {
if strings.TrimSpace(file) == "" {
continue
}
if Verbose {
fmt.Printf("hashing %s\n", file)
}
h.Write([]byte(file))
info, err := os.Lstat(file)
if err != nil {
h.Write([]byte("error"))
h.Write([]byte(err.Error()))
} else {
if info.Mode()&os.ModeSymlink != 0 {
target, err := os.Readlink(file)
die(err)
h.Write([]byte("link"))
h.Write([]byte(target))
} else if !info.IsDir() {
h.Write([]byte("file"))
f, err := os.Open(file)
die(err, file)
_, err = io.Copy(h, f)
f.Close()
die(err)
}
}
}

return h.Sum(nil)
}

func shell(command string) ([]string, error) {
cmd := exec.Command("sh", "-c", command)
out, err := cmd.CombinedOutput()
str := string(out)
lines := strings.Split(str, "\n")
return lines, err
}
16 changes: 16 additions & 0 deletions build-aux/version.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Copyright 2018 Datawire. All rights reserved.
#
# Makefile snippet for automatically setting VERSION.
#
## Inputs ##
# (none)
## Outputs ##
# - Variable: VERSION
ifeq ($(words $(filter $(abspath $(lastword $(MAKEFILE_LIST))),$(abspath $(MAKEFILE_LIST)))),1)
_version.mk := $(lastword $(MAKEFILE_LIST))

VERSION ?= $(patsubst v%,%,$(shell git describe --tags --always))$(if $(shell git status -s),-dirty$(_version.dirty_hash))

_version.dirty_hash = $(shell GO111MODULE=off go run $(dir $(_version.mk))version.go)

endif
3 changes: 3 additions & 0 deletions cmd/apictl-key/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ var apictl_key = &cobra.Command{
Use: "apictl-key [command]",
}

// Version is inserted at build using --ldflags -X
var Version = "(unknown version)"

func main() {
apictl_key.Execute()
}
21 changes: 21 additions & 0 deletions cmd/apictl-key/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package main

import (
"fmt"

"github.com/spf13/cobra"
)

var version = &cobra.Command{
Use: "version",
Short: "Show the program's version number",
Run: showVersion,
}

func init() {
apictl_key.AddCommand(version)
}

func showVersion(cmd *cobra.Command, args []string) {
fmt.Println(Version)
}
Loading

0 comments on commit 74af28a

Please sign in to comment.