Skip to content

Commit

Permalink
glide, makefile, and circleCI (#3)
Browse files Browse the repository at this point in the history
* glide and makefile

Signed-off-by: Shriram Rajagopalan <[email protected]>

* circle ci config

Signed-off-by: Shriram Rajagopalan <[email protected]>

* tweak Makefile

Signed-off-by: Shriram Rajagopalan <[email protected]>

* custom build image

Signed-off-by: Shriram Rajagopalan <[email protected]>

* typo

Signed-off-by: Shriram Rajagopalan <[email protected]>

* depend install

Signed-off-by: Shriram Rajagopalan <[email protected]>

* install glide in custom image

Signed-off-by: Shriram Rajagopalan <[email protected]>

* update circle ci config

Signed-off-by: Shriram Rajagopalan <[email protected]>

* committing protos

Signed-off-by: Shriram Rajagopalan <[email protected]>

* fix copyright

Signed-off-by: Shriram Rajagopalan <[email protected]>

* Revert "committing protos"

This reverts commit e73ec18.

Signed-off-by: Shriram Rajagopalan <[email protected]>

* version fix

Signed-off-by: Shriram Rajagopalan <[email protected]>

* feedback

Signed-off-by: Shriram Rajagopalan <[email protected]>

* fix makefile

Signed-off-by: Shriram Rajagopalan <[email protected]>

* fix all

Signed-off-by: Shriram Rajagopalan <[email protected]>

* typo fix

Signed-off-by: Shriram Rajagopalan <[email protected]>

* simplification

Signed-off-by: Shriram Rajagopalan <[email protected]>
  • Loading branch information
rshriram authored Oct 24, 2017
1 parent 4dfbb6e commit 5cf1c03
Show file tree
Hide file tree
Showing 7 changed files with 212 additions and 0 deletions.
12 changes: 12 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
version: 2
jobs:
build:
working_directory: /go/src/github.com/envoyproxy/go-control-plane
docker:
- image: circleci/golang:1.9
steps:
- checkout
- run: make check
- run: make depend.install
- run: make build
- run: make test
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/bazel-*
vendor
bin
build
.idea*
*.iml
*.bak
Expand Down
115 changes: 115 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
.DEFAULT_GOAL := build

#------------------------------------------------------------------------------
# Variables
#------------------------------------------------------------------------------

SHELL := /bin/bash
BINDIR := bin
BUILDDIR := build
DOCKERDIR := docker
RELEASEDIR := release
OUTPUT_NAME := go-control-plane
GOHOSTOS := $(shell go env GOHOSTOS)

ifndef GOOS
GOOS := $(GOHOSTOS)
endif

ifndef GOARCH
GOARCH := $(shell go env GOHOSTARCH)
endif

GOFILES = $(shell find . -type f -name '*.go' -not -path "./vendor/*")
GODIRS = $(shell go list -f '{{.Dir}}' ./... | grep -vFf <(go list -f '{{.Dir}}' ./vendor/...))
GOPKGS = $(shell go list ./... | grep -vFf <(go list ./vendor/...))

APP_VER := $(shell git describe --always 2> /dev/null || echo "unknown")

# linker flags to set build info variables
BUILD_SYM := github.com/envoyproxy/go-control-plane/pkg/version
LDFLAGS += -X $(BUILD_SYM).version=$(APP_VER)
LDFLAGS += -X $(BUILD_SYM).gitRevision=$(shell git rev-parse --short HEAD 2> /dev/null || echo unknown)
LDFLAGS += -X $(BUILD_SYM).branch=$(shell git rev-parse --abbrev-ref HEAD 2> /dev/null || echo unknown)
LDFLAGS += -X $(BUILD_SYM).buildUser=$(shell whoami || echo nobody)@$(shell hostname -f || echo builder)
LDFLAGS += -X $(BUILD_SYM).buildDate=$(shell date +%Y-%m-%dT%H:%M:%S%:z)
LDFLAGS += -X $(BUILD_SYM).goVersion=$(word 3,$(shell go version))

.PHONY: build
build:
@go build -ldflags '$(LDFLAGS)' -o $(BINDIR)/$(OUTPUT_NAME)

clean:
@echo "--> cleaning compiled objects and binaries"
@go clean -tags netgo -i $(GOPKGS)
@rm -rf $(BINDIR)/*
@rm -rf $(BUILDDIR)/*
@rm -rf $(RELEASEDIR)/*

.PHONY: test
test:
@echo "--> running unit tests"
@go test -v $(GOPKGS)

.PHONY: check
check: format.check vet lint

format: tools.goimports
@echo "--> formatting code with 'goimports' tool"
@goimports -w -l $(GOFILES)

format.check: tools.goimports
@echo "--> checking code formatting with 'goimports' tool"
@goimports -l $(GOFILES) | sed -e "s/^/\?\t/" | tee >(test -z)

vet: tools.govet
@echo "--> checking code correctness with 'go vet' tool"
@go vet $(GOPKGS)

lint: tools.golint
@echo "--> checking code style with 'golint' tool"
@echo $(GODIRS) | xargs -n 1 golint

#------------------
#-- dependencies
#------------------
.PHONY: depend.update depend.install

depend.update: tools.glide
@echo "--> updating dependencies from glide.yaml"
@glide update --strip-vendor

depend.install: tools.glide
@echo "--> installing dependencies from glide.lock "
@glide install --strip-vendor

#---------------
#-- tools
#---------------
.PHONY: tools tools.goimports tools.golint tools.govet

tools: tools.goimports tools.golint tools.govet

tools.goimports:
@command -v goimports >/dev/null ; if [ $$? -ne 0 ]; then \
echo "--> installing goimports"; \
go get golang.org/x/tools/cmd/goimports; \
fi

tools.govet:
@go tool vet 2>/dev/null ; if [ $$? -eq 3 ]; then \
echo "--> installing govet"; \
go get golang.org/x/tools/cmd/vet; \
fi

tools.golint:
@command -v golint >/dev/null ; if [ $$? -ne 0 ]; then \
echo "--> installing golint"; \
go get github.com/golang/lint/golint; \
fi

tools.glide:
@command -v glide >/dev/null ; if [ $$? -ne 0 ]; then \
echo "--> installing glide"; \
curl https://glide.sh/get | sh; \
fi
4 changes: 4 additions & 0 deletions glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions glide.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
package: github.com/envoyproxy/go-control-plane
import: []
11 changes: 11 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package main

import (
"fmt"

"github.com/envoyproxy/go-control-plane/pkg/version"
)

func main() {
fmt.Printf("Hello Envoy %s!\n", version.Build.Version)
}
66 changes: 66 additions & 0 deletions pkg/version/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Copyright 2017 Envoyproxy Authors
//
// 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 version provides build time version information.
package version

import (
"time"
)

// Build variable exposes the build-time information
var Build BuildInfo

// BuildInfo provides build information and metadata.
// It is Populated by Makefile at build-time using -ldflags -X.
// Symbol names are reported by 'go tool nm <binary object>".
// Note that unless the cluster of instances is completely homogeneous, different instances can return
// different values.
type BuildInfo struct {
Version string `json:"version"`
GitRevision string `json:"revision"`
Branch string `json:"branch"`
BuildUser string `json:"user"`
BuildDate time.Time `json:"date"`
GoVersion string `json:"go"`
}

var (
version string
gitRevision string
branch string
buildUser string
buildDate string
goVersion string
)

// Unfortunately, it seems that struct fields cannot be assigned directly using ldflags, so we resort to setting
// package scope (local) variables and then assigning their corresponding external values at initialization
func init() {
if version == "" { // this is most likely running in a test...
Build.Version = "unknown"
Build.GitRevision = "unknown"
Build.Branch = "unknown"
Build.BuildUser = "unknown"
Build.BuildDate = time.Time{}
Build.GoVersion = "unknown"
} else {
Build.Version = version
Build.GitRevision = gitRevision
Build.Branch = branch
Build.BuildUser = buildUser
Build.BuildDate, _ = time.Parse(time.RFC3339, buildDate)
Build.GoVersion = goVersion
}
}

0 comments on commit 5cf1c03

Please sign in to comment.