From 1b12c25e434a609b2667299acfe93a5db2652233 Mon Sep 17 00:00:00 2001 From: Ludovic Fernandez Date: Thu, 31 May 2018 00:03:55 +0200 Subject: [PATCH] Add linters (#556) * feat: add linters. * fix: lint. --- .gometalinter.json | 25 +++++++++++++++++++++++ .travis.yml | 3 +++ Makefile | 18 ++++++++++------ acme/client.go | 2 +- cli_handlers.go | 9 ++++---- providers/dns/cloudflare/cloudflare.go | 4 ++-- providers/dns/pdns/pdns.go | 8 ++------ providers/dns/rackspace/rackspace_test.go | 1 - 8 files changed, 49 insertions(+), 21 deletions(-) create mode 100644 .gometalinter.json diff --git a/.gometalinter.json b/.gometalinter.json new file mode 100644 index 00000000..3817a8b7 --- /dev/null +++ b/.gometalinter.json @@ -0,0 +1,25 @@ +{ + "Vendor": true, + "Test": true, + "Sort": [ + "path", + "line", + "column", + "linter", + "severity" + ], + "Cyclo": 12, + "Enable": [ + "gotypex", + "varcheck", + "gotype", + "interfacer", + "misspell", + "ineffassign", + "golint", + "vet", + "gosimple" + ], + "Exclude": [], + "Deadline": "2m" +} diff --git a/.travis.yml b/.travis.yml index 9e81312d..a34a84b0 100644 --- a/.travis.yml +++ b/.travis.yml @@ -12,6 +12,9 @@ env: before_install: - '[ "${TRAVIS_PULL_REQUEST}" = "false" ] && openssl aes-256-cbc -K $encrypted_26c593b079d9_key -iv $encrypted_26c593b079d9_iv -in .gitcookies.enc -out .gitcookies -d || true' + # Install linters and misspell + - go get -u github.com/alecthomas/gometalinter + - gometalinter --install install: - go get -t ./... diff --git a/Makefile b/Makefile index e69ae04d..7eafee97 100644 --- a/Makefile +++ b/Makefile @@ -1,15 +1,21 @@ .PHONY: all -default: clean checks test build +GOFILES := $(shell go list -f '{{range $$index, $$element := .GoFiles}}{{$$.Dir}}/{{$$element}}{{"\n"}}{{end}}' ./... | grep -v '/vendor/') -test: clean - go test -v -cover ./... +default: clean checks test build clean: rm -rf dist/ builds/ cover.out -checks: - go vet ./... - build: clean go build + +test: clean + go test -v -cover ./... + +checks: check-fmt + gometalinter ./... + +check-fmt: SHELL := /bin/bash +check-fmt: + diff -u <(echo -n) <(gofmt -d $(GOFILES)) diff --git a/acme/client.go b/acme/client.go index 786e5b2d..75c8fb54 100644 --- a/acme/client.go +++ b/acme/client.go @@ -171,7 +171,7 @@ func (c *Client) Register(tosAgreed bool) (*RegistrationResource, error) { return reg, nil } -// Register the current account to the ACME server. +// RegisterWithExternalAccountBinding Register the current account to the ACME server. func (c *Client) RegisterWithExternalAccountBinding(tosAgreed bool, kid string, hmacEncoded string) (*RegistrationResource, error) { if c == nil || c.user == nil { return nil, errors.New("acme: cannot register a nil client or user") diff --git a/cli_handlers.go b/cli_handlers.go index c5aaafe8..8da34926 100644 --- a/cli_handlers.go +++ b/cli_handlers.go @@ -322,7 +322,8 @@ func run(c *cli.Context) error { cert, err = client.ObtainCertificate(c.GlobalStringSlice("domains"), !c.Bool("no-bundle"), nil, c.Bool("must-staple")) } else { // read the CSR - csr, err := readCSRFile(c.GlobalString("csr")) + var csr *x509.CertificateRequest + csr, err = readCSRFile(c.GlobalString("csr")) if err == nil { // obtain a certificate for this CSR cert, err = client.ObtainCertificateForCSR(*csr, !c.Bool("no-bundle")) @@ -330,12 +331,10 @@ func run(c *cli.Context) error { } if err != nil { - log.Printf("Could not obtain certificates\n\t%v", err) - // Make sure to return a non-zero exit code if ObtainSANCertificate // returned at least one error. Due to us not returning partial // certificate we can just exit here instead of at the end. - os.Exit(1) + log.Fatalf("Could not obtain certificates\n\t%v", err) } if err = checkFolder(conf.CertPath()); err != nil { @@ -408,7 +407,7 @@ func renew(c *cli.Context) error { log.Printf("Could not get Certification expiration for domain %s", domain) } - if int(expTime.Sub(time.Now()).Hours()/24.0) > c.Int("days") { + if int(time.Until(expTime).Hours()/24.0) > c.Int("days") { return nil } } diff --git a/providers/dns/cloudflare/cloudflare.go b/providers/dns/cloudflare/cloudflare.go index 2904bde6..9b6556a6 100644 --- a/providers/dns/cloudflare/cloudflare.go +++ b/providers/dns/cloudflare/cloudflare.go @@ -208,9 +208,9 @@ func (c *DNSProvider) makeRequest(method, uri string, body io.Reader) (json.RawM } strBody := "Unreadable body" if body, err := ioutil.ReadAll(resp.Body); err == nil { - strBody= string(body) + strBody = string(body) } - return nil, fmt.Errorf("Cloudflare API error. The request %s sent a response with a body which is not in JSON format : %s\n", req.URL.String(), strBody) + return nil, fmt.Errorf("Cloudflare API error: the request %s sent a response with a body which is not in JSON format: %s", req.URL.String(), strBody) } return r.Result, nil diff --git a/providers/dns/pdns/pdns.go b/providers/dns/pdns/pdns.go index 10124ae2..9810245b 100644 --- a/providers/dns/pdns/pdns.go +++ b/providers/dns/pdns/pdns.go @@ -90,7 +90,7 @@ func (c *DNSProvider) Present(domain, token, keyAuth string) error { rrsets := rrSets{ RRSets: []rrSet{ - rrSet{ + { Name: name, ChangeType: "REPLACE", Type: "TXT", @@ -139,11 +139,7 @@ func (c *DNSProvider) CleanUp(domain, token, keyAuth string) error { } _, err = c.makeRequest("PATCH", zone.URL, bytes.NewReader(body)) - if err != nil { - return err - } - - return nil + return err } func (c *DNSProvider) getHostedZone(fqdn string) (*hostedZone, error) { diff --git a/providers/dns/rackspace/rackspace_test.go b/providers/dns/rackspace/rackspace_test.go index ed7a12f9..0f8c7832 100644 --- a/providers/dns/rackspace/rackspace_test.go +++ b/providers/dns/rackspace/rackspace_test.go @@ -86,7 +86,6 @@ func dnsMux() *http.ServeMux { return } w.WriteHeader(http.StatusBadRequest) - return }) mux.HandleFunc("/123456/domains/112233/records", func(w http.ResponseWriter, r *http.Request) {