Skip to content

Commit

Permalink
fix linting
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelshobbs committed Dec 9, 2019
1 parent 287495c commit 621524e
Show file tree
Hide file tree
Showing 32 changed files with 322 additions and 255 deletions.
77 changes: 77 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
linters-settings:
errcheck:
# ignore cases where we truly don't care about the returned error
ignore: net/http:^Write$,io:^WriteString$
govet:
check-shadowing: true
settings:
printf:
funcs:
- (github.com/golangci/golangci-lint/pkg/logutils.Log).Infof
- (github.com/golangci/golangci-lint/pkg/logutils.Log).Warnf
- (github.com/golangci/golangci-lint/pkg/logutils.Log).Errorf
- (github.com/golangci/golangci-lint/pkg/logutils.Log).Fatalf
golint:
min-confidence: 0
gocyclo:
min-complexity: 12
maligned:
suggest-new: true
dupl:
threshold: 100
goconst:
min-len: 2
min-occurrences: 2
depguard:
list-type: blacklist
misspell:
locale: US
lll:
line-length: 160
goimports:
local-prefixes: github.com/gliderlabs/logspout
gocritic:
settings:
hugeParam:
sizeThreshold: 160
enabled-tags:
- performance
nakedret:
max-func-lines: 65

linters:
enable-all: true
disable:
- maligned
- prealloc
- gochecknoglobals
- funlen
- gochecknoinits

issues:
# Excluding configuration per-path, per-linter, per-text and per-source
exclude-rules:
# Exclude some linters from running on tests files.
- path: _test\.go
linters:
- dupl
- errcheck
- goconst
- gocyclo
- gosec
- lll
- nakedret
- unparam
- funlen

run:
deadline: 2m
issues-exit-code: 1

# golangci.com configuration
# https://github.com/golangci/golangci/wiki/Configuration
service:
golangci-lint-version: 1.18.x # use the fixed version to not introduce new linters unexpectedly
prepare:
- echo "here I can run custom commands, but no preparation needed for this repo"

2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM alpine:3.9
FROM alpine:3.10
ENTRYPOINT ["/bin/logspout"]
VOLUME /mnt/routes
EXPOSE 80
Expand Down
15 changes: 6 additions & 9 deletions Dockerfile.dev
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
FROM alpine:3.9
FROM alpine:3.10
VOLUME /mnt/routes
EXPOSE 80

ENV GOPATH /go
RUN apk --no-cache add go build-base git mercurial ca-certificates
COPY . /go/src/github.com/gliderlabs/logspout
WORKDIR /go/src/github.com/gliderlabs/logspout
RUN go get
CMD go get \
&& go build -ldflags "-X main.Version=dev" -o /bin/logspout \
&& exec /bin/logspout
RUN apk --no-cache add go build-base git mercurial ca-certificates curl
COPY . /src
WORKDIR /src
CMD go build -ldflags "-X main.Version=dev" -o /bin/logspout \
&& exec /bin/logspout
29 changes: 20 additions & 9 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ VERSION=$(shell cat VERSION)
# max image size of 40MB
MAX_IMAGE_SIZE := 40000000

GOBIN := $(shell go env GOPATH | awk -F ":" '{ print $$1 }')/bin
GOLANGCI_LINT_VERSION := v1.18.0

ifeq ($(shell uname), Darwin)
XARGS_ARG="-L1"
endif
GOPACKAGES ?= $(shell go list ./... | egrep -v 'custom|vendor')
GOLINT := go list ./... | egrep -v '/custom/|/vendor/' | xargs $(XARGS_ARG) golint | egrep -v 'extpoints.go|types.go'
TEST_ARGS ?= -race

ifdef TEST_RUN
Expand Down Expand Up @@ -37,12 +39,21 @@ build-custom:
docker tag $(NAME):$(VERSION) gliderlabs/$(NAME):master
cd custom && docker build -t $(NAME):custom .

lint:
test -x $(GOPATH)/bin/golint || go get github.com/golang/lint/golint
go get \
&& go install $(GOPACKAGES) \
&& go tool vet -v $(shell ls -d */ | egrep -v 'custom|vendor/' | xargs $(XARGS_ARG))
@if [ -n "$(shell $(GOLINT) | cut -d ':' -f 1)" ]; then $(GOLINT) && exit 1 ; fi
lint-requirements:
ifeq ($(shell which golangci-lint), )
curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s -- -b $(GOBIN) $(GOLANGCI_LINT_VERSION)
endif

lint: lint-requirements
$(GOBIN)/golangci-lint run

lint-ci-direct: lint-requirements
$(GOBIN)/golangci-lint --verbose run

lint-ci: build-dev
docker run \
-v $(PWD):/go/src/github.com/gliderlabs/logspout \
$(NAME):dev make -e lint-ci-direct

test: build-dev
docker run \
Expand Down Expand Up @@ -76,7 +87,7 @@ test-healthcheck:
-p 8000:80 \
-v /var/run/docker.sock:/var/run/docker.sock \
$(NAME):$(VERSION)
sleep 2
sleep 5
docker logs $(NAME)-healthcheck
docker inspect --format='{{ .State.Running }}' $(NAME)-healthcheck | grep true
curl --head --silent localhost:8000/health | grep "200 OK"
Expand All @@ -85,7 +96,7 @@ test-healthcheck:

test-custom:
docker run --name $(NAME)-custom $(NAME):custom || true
docker logs $(NAME)-custom | grep -q logstash
docker logs $(NAME)-custom 2>&1 | grep -q logstash
docker rmi gliderlabs/$(NAME):master || true
docker rm $(NAME)-custom || true

Expand Down
14 changes: 7 additions & 7 deletions adapters/multiline/multiline.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import (
"sync"
"time"

"github.com/fsouza/go-dockerclient"
docker "github.com/fsouza/go-dockerclient"

"github.com/gliderlabs/logspout/router"
)

Expand Down Expand Up @@ -40,11 +41,10 @@ type Adapter struct {
}

// NewMultilineAdapter returns a configured multiline.Adapter
func NewMultilineAdapter(route *router.Route) (a router.LogAdapter, err error) {
func NewMultilineAdapter(route *router.Route) (a router.LogAdapter, err error) { //nolint:gocyclo
enableByDefault := true
enableStr := os.Getenv("MULTILINE_ENABLE_DEFAULT")
if enableStr != "" {
var err error
enableByDefault, err = strconv.ParseBool(enableStr)
if err != nil {
return nil, errors.New("multiline: invalid value for MULTILINE_ENABLE_DEFAULT (must be true|false): " + enableStr)
Expand Down Expand Up @@ -92,8 +92,8 @@ func NewMultilineAdapter(route *router.Route) (a router.LogAdapter, err error) {
flushAfter := 500 * time.Millisecond
flushAfterStr := os.Getenv("MULTILINE_FLUSH_AFTER")
if flushAfterStr != "" {
timeoutMS, err := strconv.Atoi(flushAfterStr)
if err != nil {
timeoutMS, errConv := strconv.Atoi(flushAfterStr)
if errConv != nil {
return nil, errors.New("multiline: invalid value for multiline_timeout (must be number): " + flushAfterStr)
}
flushAfter = time.Duration(timeoutMS) * time.Millisecond
Expand Down Expand Up @@ -135,7 +135,7 @@ func NewMultilineAdapter(route *router.Route) (a router.LogAdapter, err error) {
}

// Stream sends log data to the next adapter
func (a *Adapter) Stream(logstream chan *router.Message) {
func (a *Adapter) Stream(logstream chan *router.Message) { //nolint:gocyclo
wg := sync.WaitGroup{}
wg.Add(1)
go func() {
Expand Down Expand Up @@ -173,7 +173,7 @@ func (a *Adapter) Stream(logstream chan *router.Message) {
a.buffers[cID] = message
} else {
isLastLine := a.isLastLine(message)

if oldExists {
old.Data += a.separator + message.Data
message = old
Expand Down
3 changes: 2 additions & 1 deletion adapters/multiline/multiline_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import (
"testing"
"time"

"github.com/fsouza/go-dockerclient"
docker "github.com/fsouza/go-dockerclient"

"github.com/gliderlabs/logspout/router"
)

Expand Down
2 changes: 1 addition & 1 deletion adapters/raw/raw.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ var funcs = template.FuncMap{
"toJSON": func(value interface{}) string {
bytes, err := json.Marshal(value)
if err != nil {
log.Println("error marshalling to JSON: ", err)
log.Println("error marshaling to JSON: ", err)
return "null"
}
return string(bytes)
Expand Down
27 changes: 10 additions & 17 deletions adapters/syslog/syslog.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"text/template"
"time"

"github.com/gliderlabs/logspout/cfg"
"github.com/gliderlabs/logspout/router"
)

Expand All @@ -34,22 +35,14 @@ func init() {
}

func setRetryCount() {
if count, err := strconv.Atoi(getopt("RETRY_COUNT", strconv.Itoa(defaultRetryCount))); err != nil {
if count, err := strconv.Atoi(cfg.GetEnvDefault("RETRY_COUNT", strconv.Itoa(defaultRetryCount))); err != nil {
retryCount = uint(defaultRetryCount)
} else {
retryCount = uint(count)
}
debug("setting retryCount to:", retryCount)
}

func getopt(name, dfault string) string {
value := os.Getenv(name)
if value == "" {
value = dfault
}
return value
}

func debug(v ...interface{}) {
if os.Getenv("DEBUG") != "" {
log.Println(v...)
Expand All @@ -61,7 +54,7 @@ func getHostname() string {
if err == nil && len(content) > 0 {
hostname = strings.TrimRight(string(content), "\r\n")
} else {
hostname = getopt("SYSLOG_HOSTNAME", "{{.Container.Config.Hostname}}")
hostname = cfg.GetEnvDefault("SYSLOG_HOSTNAME", "{{.Container.Config.Hostname}}")
}
return hostname
}
Expand All @@ -77,18 +70,18 @@ func NewSyslogAdapter(route *router.Route) (router.LogAdapter, error) {
return nil, err
}

format := getopt("SYSLOG_FORMAT", "rfc5424")
priority := getopt("SYSLOG_PRIORITY", "{{.Priority}}")
pid := getopt("SYSLOG_PID", "{{.Container.State.Pid}}")
format := cfg.GetEnvDefault("SYSLOG_FORMAT", "rfc5424")
priority := cfg.GetEnvDefault("SYSLOG_PRIORITY", "{{.Priority}}")
pid := cfg.GetEnvDefault("SYSLOG_PID", "{{.Container.State.Pid}}")
hostname = getHostname()

tag := getopt("SYSLOG_TAG", "{{.ContainerName}}"+route.Options["append_tag"])
structuredData := getopt("SYSLOG_STRUCTURED_DATA", "")
tag := cfg.GetEnvDefault("SYSLOG_TAG", "{{.ContainerName}}"+route.Options["append_tag"])
structuredData := cfg.GetEnvDefault("SYSLOG_STRUCTURED_DATA", "")
if route.Options["structured_data"] != "" {
structuredData = route.Options["structured_data"]
}
data := getopt("SYSLOG_DATA", "{{.Data}}")
timestamp := getopt("SYSLOG_TIMESTAMP", "{{.Timestamp}}")
data := cfg.GetEnvDefault("SYSLOG_DATA", "{{.Data}}")
timestamp := cfg.GetEnvDefault("SYSLOG_TIMESTAMP", "{{.Timestamp}}")

if structuredData == "" {
structuredData = "-"
Expand Down
20 changes: 6 additions & 14 deletions adapters/syslog/syslog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package syslog

import (
"bufio"
"fmt"
"io"
"io/ioutil"
"log"
Expand All @@ -15,22 +14,17 @@ import (
"text/template"
"time"

docker "github.com/fsouza/go-dockerclient"
"github.com/gliderlabs/logspout/router"

_ "github.com/gliderlabs/logspout/transports/tcp"
_ "github.com/gliderlabs/logspout/transports/tls"
_ "github.com/gliderlabs/logspout/transports/udp"

docker "github.com/fsouza/go-dockerclient"

"github.com/gliderlabs/logspout/router"
)

const (
testPriority = "{{.Priority}}"
testTimestamp = "{{.Timestamp}}"
testHostname = "{{.Container.Config.Hostname}}"
testTag = "{{.ContainerName}}"
testPid = "{{.Container.State.Pid}}"
testData = "{{.Data}}"
connCloseIdx = 5
connCloseIdx = 5
)

var (
Expand All @@ -41,8 +35,6 @@ var (
Hostname: "8dfafdbc3a40",
},
}
testTmplStr = fmt.Sprintf("<%s>%s %s %s[%s]: %s\n",
testPriority, testTimestamp, testHostname, testTag, testPid, testData)
hostHostnameFilename = "/etc/host_hostname"
hostnameContent = "hostname"
badHostnameContent = "hostname\r\n"
Expand Down Expand Up @@ -89,7 +81,7 @@ func TestSyslogReconnectOnClose(t *testing.T) {
case msg := <-done:
// Don't check a message that we know was dropped
if msgnum%connCloseIdx == 0 {
_ = <-messages
<-messages
msgnum++
}
check(t, adapter.(*Adapter).tmpl, <-messages, msg)
Expand Down
8 changes: 2 additions & 6 deletions build.sh
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
#!/bin/sh
set -e
apk add --update go build-base git mercurial ca-certificates
mkdir -p /go/src/github.com/gliderlabs
cp -r /src /go/src/github.com/gliderlabs/logspout
cd /go/src/github.com/gliderlabs/logspout
export GOPATH=/go
go get github.com/Masterminds/glide && $GOPATH/bin/glide install
cd /src
go build -ldflags "-X main.Version=$1" -o /bin/logspout
apk del go git mercurial build-base
rm -rf /go /var/cache/apk/* /root/.glide
rm -rf /root/go /var/cache/apk/*

# backwards compatibility
ln -fs /tmp/docker.sock /var/run/docker.sock
11 changes: 11 additions & 0 deletions cfg/cfg.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package cfg

import "os"

// GetEnvDefault is a helper function to retrieve an env variable value OR return a default value
func GetEnvDefault(name, dfault string) string {
if val := os.Getenv(name); val != "" {
return val
}
return dfault
}
Loading

0 comments on commit 621524e

Please sign in to comment.