From 6b2bad209018eaf1cbeba66de5f7f592e7c3d192 Mon Sep 17 00:00:00 2001 From: Tom Wilkie Date: Tue, 19 May 2015 10:41:04 +0000 Subject: [PATCH 1/2] gofmt fixes --- experimental/demoprobe/generate.go | 2 +- experimental/fixprobe/main.go | 2 +- experimental/genreport/generate.go | 2 +- probe/process_mapper.go | 2 +- probe/spy_test.go | 8 ++++---- 5 files changed, 8 insertions(+), 8 deletions(-) diff --git a/experimental/demoprobe/generate.go b/experimental/demoprobe/generate.go index 0aa57a58a..1a44c9e10 100644 --- a/experimental/demoprobe/generate.go +++ b/experimental/demoprobe/generate.go @@ -20,7 +20,7 @@ func DemoReport(nodeCount int) report.Report { // Make up some plausible IPv4 numbers hosts := []string{} ip := [4]int{192, 168, 1, 1} - for _ = range make([]struct{}, nodeCount) { + for range make([]struct{}, nodeCount) { hosts = append(hosts, fmt.Sprintf("%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3])) ip[3]++ if ip[3] > 200 { diff --git a/experimental/fixprobe/main.go b/experimental/fixprobe/main.go index 47ee9b19f..83d2cc527 100644 --- a/experimental/fixprobe/main.go +++ b/experimental/fixprobe/main.go @@ -46,7 +46,7 @@ func main() { log.Printf("listening on %s", *listenAddress) - for _ = range time.Tick(*publishInterval) { + for range time.Tick(*publishInterval) { publisher.Publish(fixedReport) } } diff --git a/experimental/genreport/generate.go b/experimental/genreport/generate.go index 4fddb417c..0de3008f3 100644 --- a/experimental/genreport/generate.go +++ b/experimental/genreport/generate.go @@ -20,7 +20,7 @@ func DemoReport(nodeCount int) report.Report { // Make up some plausible IPv4 numbers hosts := []string{} ip := [4]int{192, 168, 1, 1} - for _ = range make([]struct{}, nodeCount) { + for range make([]struct{}, nodeCount) { hosts = append(hosts, fmt.Sprintf("%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3])) ip[3]++ if ip[3] > 200 { diff --git a/probe/process_mapper.go b/probe/process_mapper.go index a4df18ce2..b42964487 100644 --- a/probe/process_mapper.go +++ b/probe/process_mapper.go @@ -57,7 +57,7 @@ func (m *cgroupMapper) Map(pid uint) (string, error) { } func (m *cgroupMapper) loop(d time.Duration) { - for _ = range time.Tick(d) { + for range time.Tick(d) { m.update() } } diff --git a/probe/spy_test.go b/probe/spy_test.go index 18c7dbfe0..d6f7239af 100644 --- a/probe/spy_test.go +++ b/probe/spy_test.go @@ -38,14 +38,14 @@ var ( fixProcessPIDB = uint(4243) fixConnections = []procspy.Connection{ - procspy.Connection{ + { Transport: "tcp", LocalAddress: fixLocalAddress, LocalPort: fixLocalPort, RemoteAddress: fixRemoteAddress, RemotePort: fixRemotePort, }, - procspy.Connection{ + { Transport: "tcp", LocalAddress: fixLocalAddress, LocalPort: fixLocalPort, @@ -55,7 +55,7 @@ var ( } fixConnectionsWithProcesses = []procspy.Connection{ - procspy.Connection{ + { Transport: "tcp", LocalAddress: fixLocalAddress, LocalPort: fixLocalPort, @@ -66,7 +66,7 @@ var ( Name: fixProcessName, }, }, - procspy.Connection{ + { Transport: "tcp", LocalAddress: fixLocalAddress, LocalPort: fixLocalPort, From 4c3912edf078b599f16ca7594dfcff50f4dcadaa Mon Sep 17 00:00:00 2001 From: Tom Wilkie Date: Mon, 18 May 2015 17:58:53 +0000 Subject: [PATCH 2/2] Add lint script; run it on circle. Intentionally don't lint static.go. --- Makefile | 8 ++--- bin/lint | 87 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ circle.yml | 3 ++ 3 files changed, 94 insertions(+), 4 deletions(-) create mode 100755 bin/lint diff --git a/Makefile b/Makefile index b8b360eb4..ec19367bb 100644 --- a/Makefile +++ b/Makefile @@ -16,15 +16,15 @@ dist: client static $(APP_EXE) $(PROBE_EXE) client: cd client && make build && rm -f dist/.htaccess -static: +app/static.go: go get github.com/mjibson/esc - cd app && esc -o static.go -prefix ../client/dist ../client/dist + esc -o app/static.go -prefix client/dist client/dist test: $(APP_EXE) $(FIXPROBE_EXE) # app and fixprobe needed for integration tests go test ./... -$(APP_EXE): app/*.go report/*.go xfer/*.go +$(APP_EXE): app/*.go app/static.go report/*.go xfer/*.go $(PROBE_EXE): probe/*.go report/*.go xfer/*.go $(APP_EXE) $(PROBE_EXE): @@ -41,4 +41,4 @@ $(SCOPE_EXPORT): $(APP_EXE) $(PROBE_EXE) docker/Dockerfile docker/entrypoint.sh clean: go clean ./... - rm -f $(SCOPE_EXPORT) + rm -f $(SCOPE_EXPORT) app/static.go diff --git a/bin/lint b/bin/lint new file mode 100755 index 000000000..d5555e905 --- /dev/null +++ b/bin/lint @@ -0,0 +1,87 @@ +#!/bin/bash +# This scipt lints go files for common errors. +# +# Its runs gofmt and go vet, and optionally golint and +# gocyclo, if they are installed. +# +# With no arguments, it lints the current files staged +# for git commit. Or you can pass it explicit filenames +# (or directories) and it will lint them. +# +# To use this script automatically, run: +# ln -s ../../bin/lint .git/hooks/pre-commit + +set -eu + +function lint_go { + filename="$1" + result=0 + + if [ -n "$(gofmt -s -l "${filename}")" ]; then + result=1 + echo "${filename}: run gofmt -s -w ${filename}!" + fi + + go tool vet "${filename}" || result=$? + + # golint is completely optional. If you don't like it + # don't have it installed. + if type golint >/dev/null 2>&1; then + # golint doesn't set an exit code it seems + lintoutput=$(golint "${filename}") + if [ "$lintoutput" != "" ]; then + result=1 + echo "$lintoutput" + fi + fi + + # gocyclo is completely optional. If you don't like it + # don't have it installed. Also never blocks a commit, + # it just warns. + if type gocyclo >/dev/null 2>&1; then + gocyclo -over 25 "${filename}" | while read line; do + echo "${filename}": higher than 25 cyclomatic complexity - "${line}" + done + fi + + return $result +} + +function lint { + filename="$1" + ext="${filename##*\.}" + + # Don't lint deleted files + if [ ! -f "$filename" ]; then + return + fi + + # Don't lint this script or static.go + case "${filename}" in + bin/lint) return;; + ./app/static.go) return;; + esac + + case "$ext" in + go) lint_go "${filename}" + ;; + esac +} + +function lint_files { + lint_result=0 + while read filename; do + lint "${filename}" || lint_result=1 + done + exit $lint_result +} + +function list_files { + if [ $# -gt 0 ]; then + find "$@" -type f + else + git diff --cached --name-only + fi +} + +list_files "$@" | lint_files diff --git a/circle.yml b/circle.yml index 476bf079e..069e0d9ed 100644 --- a/circle.yml +++ b/circle.yml @@ -13,12 +13,15 @@ machine: dependencies: post: + - go get github.com/golang/lint/golint + - go get github.com/fzipp/gocyclo - go get github.com/mattn/goveralls - mkdir -p $(dirname $SRCDIR) - cp -r $(pwd)/ $SRCDIR test: override: + - cd $SRCDIR; ./bin/lint . - cd $SRCDIR; make - cd $SRCDIR; ./bin/test post: