From 40391c57c2a8afcc92eb15c71ee401e14e4ab8e8 Mon Sep 17 00:00:00 2001 From: Vincent Demeester Date: Fri, 6 Nov 2015 18:11:57 +0100 Subject: [PATCH] =?UTF-8?q?Add=20validate-golint=20target=20and=20script?= =?UTF-8?q?=20=E2=80=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit … and *lint* the latest piece of code. Signed-off-by: Vincent Demeester --- Makefile | 5 ++++- adapters.go | 17 +++++++---------- build.Dockerfile | 1 + configuration.go | 14 ++++++++++++++ script/validate-golint | 31 +++++++++++++++++++++++++++++++ traefik.go | 35 ++++++++++++++--------------------- version.go | 4 +++- web.go | 4 ++++ 8 files changed, 78 insertions(+), 33 deletions(-) create mode 100755 script/validate-golint diff --git a/Makefile b/Makefile index 486004295..91e11c604 100644 --- a/Makefile +++ b/Makefile @@ -40,7 +40,7 @@ test-integration: build $(DOCKER_RUN_TRAEFIK) ./script/make.sh generate test-integration validate: build - $(DOCKER_RUN_TRAEFIK) ./script/make.sh validate-gofmt validate-govet + $(DOCKER_RUN_TRAEFIK) ./script/make.sh validate-gofmt validate-govet validate-golint validate-gofmt: build $(DOCKER_RUN_TRAEFIK) ./script/make.sh validate-gofmt @@ -48,6 +48,9 @@ validate-gofmt: build validate-govet: build $(DOCKER_RUN_TRAEFIK) ./script/make.sh validate-govet +validate-golint: build + $(DOCKER_RUN_TRAEFIK) ./script/make.sh validate-golint + build: dist docker build -t "$(TRAEFIK_DEV_IMAGE)" -f build.Dockerfile . diff --git a/adapters.go b/adapters.go index 7f9f4d8f8..bebe4e7d1 100644 --- a/adapters.go +++ b/adapters.go @@ -4,40 +4,37 @@ Copyright package main import ( + "net/http" + log "github.com/Sirupsen/logrus" "github.com/gorilla/mux" - "github.com/mailgun/oxy/utils" - "net/http" ) +// OxyLogger implements oxy Logger interface with logrus. type OxyLogger struct { } +// Infof logs specified string as Debug level in logrus. func (oxylogger *OxyLogger) Infof(format string, args ...interface{}) { log.Debugf(format, args...) } +// Warningf logs specified string as Warning level in logrus. func (oxylogger *OxyLogger) Warningf(format string, args ...interface{}) { log.Warningf(format, args...) } +// Errorf logs specified string as Error level in logrus. func (oxylogger *OxyLogger) Errorf(format string, args ...interface{}) { log.Errorf(format, args...) } -type ErrorHandler struct { -} - -func (e *ErrorHandler) ServeHTTP(w http.ResponseWriter, req *http.Request, err error) { - log.Error("server error ", err.Error()) - utils.DefaultHandler.ServeHTTP(w, req, err) -} - func notFoundHandler(w http.ResponseWriter, r *http.Request) { http.NotFound(w, r) //templatesRenderer.HTML(w, http.StatusNotFound, "notFound", nil) } +// LoadDefaultConfig returns a default gorrilla.mux router from the specified configuration. func LoadDefaultConfig(globalConfiguration *GlobalConfiguration) *mux.Router { router := mux.NewRouter() router.NotFoundHandler = http.HandlerFunc(notFoundHandler) diff --git a/build.Dockerfile b/build.Dockerfile index eddcac4f8..32b1f32e1 100644 --- a/build.Dockerfile +++ b/build.Dockerfile @@ -4,6 +4,7 @@ RUN go get github.com/Masterminds/glide RUN go get github.com/mitchellh/gox RUN go get github.com/tcnksm/ghr RUN go get github.com/jteeuwen/go-bindata/... +RUN go get github.com/golang/lint/golint # Which docker version to test on ENV DOCKER_VERSION 1.6.2 diff --git a/configuration.go b/configuration.go index db86c5ad0..25b4d035c 100644 --- a/configuration.go +++ b/configuration.go @@ -1,12 +1,16 @@ package main import ( + fmtlog "log" "time" + "github.com/BurntSushi/toml" "github.com/emilevauge/traefik/provider" "github.com/emilevauge/traefik/types" ) +// GlobalConfiguration holds global configuration (with providers, etc.). +// It's populated from the traefik configuration file passed as an argument to the binary. type GlobalConfiguration struct { Port string GraceTimeOut int64 @@ -25,6 +29,7 @@ type GlobalConfiguration struct { Boltdb *provider.BoltDb } +// NewGlobalConfiguration returns a GlobalConfiguration with default values. func NewGlobalConfiguration() *GlobalConfiguration { globalConfiguration := new(GlobalConfiguration) // default values @@ -36,4 +41,13 @@ func NewGlobalConfiguration() *GlobalConfiguration { return globalConfiguration } +// LoadFileConfig returns a GlobalConfiguration from reading the specified file (a toml file). +func LoadFileConfig(file string) *GlobalConfiguration { + configuration := NewGlobalConfiguration() + if _, err := toml.DecodeFile(file, configuration); err != nil { + fmtlog.Fatalf("Error reading file: %s", err) + } + return configuration +} + type configs map[string]*types.Configuration diff --git a/script/validate-golint b/script/validate-golint new file mode 100755 index 000000000..2f7769fdd --- /dev/null +++ b/script/validate-golint @@ -0,0 +1,31 @@ +#!/bin/bash + +source "$(dirname "$BASH_SOURCE")/.validate" + +IFS=$'\n' +files=( $(validate_diff --diff-filter=ACMR --name-only -- '*.go' | grep -v '^vendor/\|autogen' || true) ) +unset IFS + +errors=() +for f in "${files[@]}"; do + # we use "git show" here to validate that what's committed passes go vet + failedLint=$(golint "$f") + if [ "$failedLint" ]; then + errors+=( "$failedLint" ) + fi +done + +if [ ${#errors[@]} -eq 0 ]; then + echo 'Congratulations! All Go source files have been linted.' +else + { + echo "Errors from golint:" + for err in "${errors[@]}"; do + echo "$err" + done + echo + echo 'Please fix the above errors. You can test via "golint" and commit the result.' + echo + } >&2 + false +fi diff --git a/traefik.go b/traefik.go index ac64421ba..65bc4e0c9 100644 --- a/traefik.go +++ b/traefik.go @@ -14,7 +14,6 @@ import ( "syscall" "time" - "github.com/BurntSushi/toml" log "github.com/Sirupsen/logrus" "github.com/codegangsta/negroni" "github.com/emilevauge/traefik/middlewares" @@ -266,20 +265,20 @@ func prepareServer(router *mux.Router, globalConfiguration *GlobalConfiguration, Handler: negroni, TLSConfig: tlsConfig, }), nil - } else { - server, err := oldServer.HijackListener(&http.Server{ - Addr: globalConfiguration.Port, - Handler: negroni, - }, tlsConfig) - if err != nil { - log.Fatalf("Error hijacking server %s", err) - return nil, err - } else { - return server, nil - } } + server, err := oldServer.HijackListener(&http.Server{ + Addr: globalConfiguration.Port, + Handler: negroni, + }, tlsConfig) + if err != nil { + log.Fatalf("Error hijacking server %s", err) + return nil, err + } + return server, nil } +// LoadConfig returns a new gorrilla.mux Route from the specified global configuration and the dynamic +// provider configurations. func LoadConfig(configurations configs, globalConfiguration *GlobalConfiguration) (*mux.Router, error) { router := mux.NewRouter() router.NotFoundHandler = http.HandlerFunc(notFoundHandler) @@ -346,13 +345,15 @@ func LoadConfig(configurations configs, globalConfiguration *GlobalConfiguration newRoute.Handler(backends[frontend.Backend]) err := newRoute.GetError() if err != nil { - log.Error("Error building route: %s", err) + log.Errorf("Error building route: %s", err) } } } return router, nil } +// Invoke calls the specified method with the specified arguments on the specified interface. +// It uses the go(lang) reflect package. func Invoke(any interface{}, name string, args ...interface{}) []reflect.Value { inputs := make([]reflect.Value, len(args)) for i := range args { @@ -360,11 +361,3 @@ func Invoke(any interface{}, name string, args ...interface{}) []reflect.Value { } return reflect.ValueOf(any).MethodByName(name).Call(inputs) } - -func LoadFileConfig(file string) *GlobalConfiguration { - configuration := NewGlobalConfiguration() - if _, err := toml.DecodeFile(file, configuration); err != nil { - fmtlog.Fatalf("Error reading file: %s", err) - } - return configuration -} diff --git a/version.go b/version.go index 1b9d6464b..15a6ee9dd 100644 --- a/version.go +++ b/version.go @@ -1,6 +1,8 @@ package main var ( - Version = "" + // Version holds the current version of traefik. + Version = "" + // BuildDate holds the build date of traefik. BuildDate = "" ) diff --git a/web.go b/web.go index ecc5c08c0..7605da19a 100644 --- a/web.go +++ b/web.go @@ -14,6 +14,8 @@ import ( "github.com/unrolled/render" ) +// WebProvider is a provider.Provider implementation that provides the UI. +// FIXME to be handled another way. type WebProvider struct { Address string CertFile, KeyFile string @@ -25,6 +27,8 @@ var ( }) ) +// Provide allows the provider to provide configurations to traefik +// using the given configuration channel. func (provider *WebProvider) Provide(configurationChan chan<- types.ConfigMessage) error { systemRouter := mux.NewRouter()