Permalink
Browse files

build: setup linter

  • Loading branch information...
moul committed Jan 10, 2019
1 parent abd94f1 commit 28410587235a2b105783077dac6ff3e7c746b819
Showing with 90 additions and 42 deletions.
  1. +3 −0 .circleci/config.yml
  2. +34 −0 .golangci.yml
  3. +4 −0 Makefile
  4. +7 −7 go.mod
  5. +14 −13 go.sum
  6. +6 −3 server/cobra.go
  7. +6 −6 server/interceptor.go
  8. +6 −6 server/server.go
  9. +6 −3 sql/cobra.go
  10. +4 −4 sql/sql.go
@@ -30,6 +30,9 @@ jobs:
- run: go get
- run: make _ci_prepare
- run: make test
# FIXME: setup coverage
- run: curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s v1.12.2
- run: PATH=$PATH:$(pwd)/bin make lint
# FIXME: save cache
# FIXME: store_test_results

@@ -0,0 +1,34 @@
run:
deadline: 1m
tests: false
#skip-files:
# - ".*\\.gen\\.go"

linters-settings:
golint:
min-confidence: 0
maligned:
suggest-new: true
goconst:
min-len: 5
min-occurrences: 4
misspell:
locale: US

linters:
disable-all: true
enable:
- goconst
- misspell
- deadcode
- misspell
- structcheck
- errcheck
- unused
- varcheck
- staticcheck
- unconvert
- gofmt
- goimports
- golint
- ineffassign
@@ -93,3 +93,7 @@ docker.integration:
.PHONY: integration
integration:
./test/integration.sh

.PHONY: lint
lint:
golangci-lint run --verbose ./...
14 go.mod
@@ -11,22 +11,22 @@ require (
github.com/gogo/protobuf v1.2.0
github.com/golang/protobuf v1.2.0
github.com/google/go-cmp v0.2.0 // indirect
github.com/grpc-ecosystem/go-grpc-middleware v1.0.1-0.20181112102510-3304cc886352
github.com/grpc-ecosystem/grpc-gateway v1.6.2
github.com/grpc-ecosystem/go-grpc-middleware v1.0.1-0.20190104160321-4832df01553a
github.com/grpc-ecosystem/grpc-gateway v1.6.4
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/jinzhu/gorm v1.9.2
github.com/jinzhu/inflection v0.0.0-20180308033659-04140366298a // indirect
github.com/jinzhu/now v0.0.0-20181116074157-8ec929ed50c3 // indirect
github.com/lib/pq v1.0.0 // indirect
github.com/mattn/go-sqlite3 v1.10.0
github.com/pkg/errors v0.8.0
github.com/sirupsen/logrus v1.2.0 // indirect
github.com/pkg/errors v0.8.1
github.com/sirupsen/logrus v1.3.0 // indirect
github.com/spf13/cobra v0.0.3
github.com/spf13/pflag v1.0.3
github.com/spf13/viper v1.3.1
go.uber.org/zap v1.9.1
golang.org/x/net v0.0.0-20181217023233-e147a9138326
google.golang.org/genproto v0.0.0-20181219182458-5a97ab628bfb // indirect
golang.org/x/net v0.0.0-20190110044637-be1c187aa6c6
google.golang.org/genproto v0.0.0-20190108161440-ae2f86662275
google.golang.org/grpc v1.17.0
moul.io/zapgorm v0.0.0-20181003053625-c808c1c4adc6
moul.io/zapgorm v0.0.0-20181220094309-2149c5ded6fc
)
27 go.sum
@@ -35,10 +35,10 @@ github.com/google/go-cmp v0.2.0 h1:+dTQ8DZQJz0Mb/HjFlkptS1FeQ4cWSnN941F8aEG4SQ=
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
github.com/grpc-ecosystem/go-grpc-middleware v1.0.0 h1:BWIsLfhgKhV5g/oF34aRjniBHLTZe5DNekSjbAjIS6c=
github.com/grpc-ecosystem/go-grpc-middleware v1.0.0/go.mod h1:FiyG127CGDf3tlThmgyCl78X/SZQqEOJBCDaAfeWzPs=
github.com/grpc-ecosystem/go-grpc-middleware v1.0.1-0.20181112102510-3304cc886352 h1:2RuZr3wEO3ZsCx48VepiL6VdOJep1jAX5vUZICky22A=
github.com/grpc-ecosystem/go-grpc-middleware v1.0.1-0.20181112102510-3304cc886352/go.mod h1:FiyG127CGDf3tlThmgyCl78X/SZQqEOJBCDaAfeWzPs=
github.com/grpc-ecosystem/grpc-gateway v1.6.2 h1:8KyC64BiO8ndiGHY5DlFWWdangUPC9QHPakFRre/Ud0=
github.com/grpc-ecosystem/grpc-gateway v1.6.2/go.mod h1:RSKVYQBd5MCa4OVpNdGskqpgL2+G+NZTnrVHpWWfpdw=
github.com/grpc-ecosystem/go-grpc-middleware v1.0.1-0.20190104160321-4832df01553a h1:2rNI7/w3PcLz1T96hzGaFRhtKwtB28ELZIrK5oW/euE=
github.com/grpc-ecosystem/go-grpc-middleware v1.0.1-0.20190104160321-4832df01553a/go.mod h1:FiyG127CGDf3tlThmgyCl78X/SZQqEOJBCDaAfeWzPs=
github.com/grpc-ecosystem/grpc-gateway v1.6.4 h1:xlu6C2WU6gvXt3XLyVpsgweaIL4VCmTjEsEAIt7qFqQ=
github.com/grpc-ecosystem/grpc-gateway v1.6.4/go.mod h1:RSKVYQBd5MCa4OVpNdGskqpgL2+G+NZTnrVHpWWfpdw=
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
@@ -62,12 +62,13 @@ github.com/mitchellh/mapstructure v1.1.2 h1:fmNYVwqnSfB9mZU6OS2O6GsXM+wcskZDuKQz
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
github.com/pelletier/go-toml v1.2.0 h1:T5zMGML61Wp+FlcbWjRDT7yAxhJNAiPPLOFECq181zc=
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
github.com/pkg/errors v0.8.0 h1:WdK/asTD0HN+q6hsWO3/vpuAkAr+tw6aNJNDFFf0+qw=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sirupsen/logrus v1.2.0 h1:juTguoYk5qI21pwyTXY3B3Y5cOTH3ZUyZCg1v/mihuo=
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
github.com/sirupsen/logrus v1.3.0 h1:hI/7Q+DtNZ2kINb6qt/lS+IyXnHQe9e90POfeewL/ME=
github.com/sirupsen/logrus v1.3.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
github.com/spf13/afero v1.1.2 h1:m8/z1t7/fwjysjQRYbP0RD+bUIF/8tJwPdEZsI83ACI=
github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ=
github.com/spf13/cast v1.3.0 h1:oget//CVOEoFewqQxwr0Ej5yjygnqGkvggSE/gB35Q8=
@@ -98,8 +99,8 @@ golang.org/x/lint v0.0.0-20180702182130-06c8688daad7/go.mod h1:UVdnD1Gm6xHRNCYTk
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20181106065722-10aee1819953/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20181217023233-e147a9138326 h1:iCzOf0xz39Tstp+Tu/WwyGjUXCk34QhQORRxBeXXTA4=
golang.org/x/net v0.0.0-20181217023233-e147a9138326/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190110044637-be1c187aa6c6 h1:ubmJw47bgQA7wuO44xiH7CR+teQ71HAVifUbGo40YG8=
golang.org/x/net v0.0.0-20190110044637-be1c187aa6c6/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be h1:vEDujvNQGv4jgYKudGeI/+DAX4Jffq6hpD55MmoEvKs=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f h1:wMNYb4v58l5UBM7MYRLPG6ZhfOqbKu7X5eyFl8ZhKvA=
@@ -114,8 +115,8 @@ golang.org/x/tools v0.0.0-20180828015842-6cd1fcedba52/go.mod h1:n7NCudcB/nEzxVGm
google.golang.org/appengine v1.1.0 h1:igQkv0AAhEIvTEpD5LIpAfav2eeVO9HBTjvKHVJPRSs=
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc=
google.golang.org/genproto v0.0.0-20181219182458-5a97ab628bfb h1:dQshZyyJ5W/Xk8myF4GKBak1pZW6EywJuQ8+44EQhGA=
google.golang.org/genproto v0.0.0-20181219182458-5a97ab628bfb/go.mod h1:7Ep/1NZk928CDR8SjdVbjWNpdIf6nzjE3BTgJDr2Atg=
google.golang.org/genproto v0.0.0-20190108161440-ae2f86662275 h1:9oFlwfEGIvmxXTcY53ygNyxIQtWciRHjrnUvZJCYXYU=
google.golang.org/genproto v0.0.0-20190108161440-ae2f86662275/go.mod h1:7Ep/1NZk928CDR8SjdVbjWNpdIf6nzjE3BTgJDr2Atg=
google.golang.org/grpc v1.16.0/go.mod h1:0JHn/cJsOMiMfNA9+DeHDlAU7KAAB5GDlYFpa9MZMio=
google.golang.org/grpc v1.17.0 h1:TRJYBgMclJvGYn2rIMjj+h9KtMt5r1Ij7ODVRIZkwhk=
google.golang.org/grpc v1.17.0/go.mod h1:6QZJwpn2B+Zp71q/5VxRsJ6NXXVCE5NRUHRo+f3cWCs=
@@ -124,5 +125,5 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
honnef.co/go/tools v0.0.0-20180728063816-88497007e858/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
moul.io/zapgorm v0.0.0-20181003053625-c808c1c4adc6 h1:Ljm5aWTlBca3uc62R9myafsLzwakKumgU/6B6fFWTsA=
moul.io/zapgorm v0.0.0-20181003053625-c808c1c4adc6/go.mod h1:5XkuYCrMvJcscMD3+T4QGDaWsCZiql8k2Pv5iNhTB/g=
moul.io/zapgorm v0.0.0-20181220094309-2149c5ded6fc h1:Ddo1Fa0ruGmrNMWVOzEfbMcp3yy3XrspnmQMJ4o7mfg=
moul.io/zapgorm v0.0.0-20181220094309-2149c5ded6fc/go.mod h1:5XkuYCrMvJcscMD3+T4QGDaWsCZiql8k2Pv5iNhTB/g=
@@ -4,17 +4,20 @@ import (
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/spf13/viper"
"go.uber.org/zap"
)

func serverSetupFlags(flags *pflag.FlagSet, opts *serverOptions) {
func serverSetupFlags(flags *pflag.FlagSet, opts *Options) {
flags.StringVar(&opts.GRPCBind, "grpc-bind", ":9111", "gRPC server address")
flags.StringVar(&opts.HTTPBind, "http-bind", ":8000", "HTTP server address")
flags.StringVar(&opts.JWTKey, "jwt-key", "", "JWT secure key")
viper.BindPFlags(flags)
if err := viper.BindPFlags(flags); err != nil {
zap.L().Warn("failed to bind viper flags", zap.Error(err))
}
}

func NewServerCommand() *cobra.Command {
opts := &serverOptions{}
opts := &Options{}
cmd := &cobra.Command{
Use: "server",
RunE: func(cmd *cobra.Command, args []string) error {
@@ -7,9 +7,9 @@ import (
"strings"

jwt "github.com/dgrijalva/jwt-go"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"
"pathwar.pw/entity"
)

@@ -26,29 +26,29 @@ func (s *svc) AuthFuncOverride(ctx context.Context, fullMethodName string) (cont

md, ok := metadata.FromIncomingContext(ctx)
if !ok {
return nil, grpc.Errorf(codes.Unauthenticated, "cannot get metadata/headers")
return nil, status.Errorf(codes.Unauthenticated, "cannot get metadata/headers")
}
auth, ok := md["authorization"]
if !ok || len(auth) < 1 {
return nil, grpc.Errorf(codes.Unauthenticated, "no token provided")
return nil, status.Errorf(codes.Unauthenticated, "no token provided")
}
if strings.HasPrefix(auth[0], "Bearer ") {
auth[0] = auth[0][7:]
}

token, err := jwt.Parse(auth[0], func(token *jwt.Token) (interface{}, error) {
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
return nil, grpc.Errorf(codes.Unauthenticated, "there was an error")
return nil, status.Errorf(codes.Unauthenticated, "there was an error")
}
return s.jwtKey, nil
})
if err != nil {
return nil, grpc.Errorf(codes.Unauthenticated, err.Error())
return nil, status.Errorf(codes.Unauthenticated, err.Error())
}

claims, ok := token.Claims.(jwt.MapClaims)
if !ok || !token.Valid {
return nil, grpc.Errorf(codes.Unauthenticated, "invalid token")
return nil, status.Errorf(codes.Unauthenticated, "invalid token")
}
ctx = context.WithValue(ctx, sessionCtx, entity.Session{
// FIXME: use mapstructure
@@ -23,19 +23,19 @@ import (

var _ = gogoproto.IsStdTime

type serverOptions struct {
type Options struct {
GRPCBind string
HTTPBind string
JWTKey string
WithReflection bool
}

func (opts serverOptions) String() string {
func (opts Options) String() string {
out, _ := json.Marshal(opts)
return string(out)
}

func server(opts *serverOptions) error {
func server(opts *Options) error {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

@@ -45,7 +45,7 @@ func server(opts *serverOptions) error {
return <-errs
}

func startHTTPServer(ctx context.Context, opts *serverOptions) error {
func startHTTPServer(ctx context.Context, opts *Options) error {
gwmux := runtime.NewServeMux(
runtime.WithMarshalerOption(runtime.MIMEWildcard, &gateway.JSONPb{
EmitDefaults: false,
@@ -64,7 +64,7 @@ func startHTTPServer(ctx context.Context, opts *serverOptions) error {
return http.ListenAndServe(opts.HTTPBind, mux)
}

func startGRPCServer(ctx context.Context, opts *serverOptions) error {
func startGRPCServer(ctx context.Context, opts *Options) error {
listener, err := net.Listen("tcp", opts.GRPCBind)
if err != nil {
return errors.Wrap(err, "failed to listen")
@@ -117,7 +117,7 @@ func startGRPCServer(ctx context.Context, opts *serverOptions) error {
return grpcServer.Serve(listener)
}

func newSvc(opts *serverOptions) (*svc, error) {
func newSvc(opts *Options) (*svc, error) {
jwtKey := []byte(opts.JWTKey)
if len(jwtKey) == 0 { // generate random JWT key
jwtKey = make([]byte, 128)
@@ -4,11 +4,14 @@ import (
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/spf13/viper"
"go.uber.org/zap"
)

func sqlSetupFlags(flags *pflag.FlagSet, opts *SQLOptions) {
func sqlSetupFlags(flags *pflag.FlagSet, opts *Options) {
flags.StringVar(&opts.Path, "sql-path", "/tmp/pathwar.db", "SQL db path")
viper.BindPFlags(flags)
if err := viper.BindPFlags(flags); err != nil {
zap.L().Warn("failed to bind viper flags", zap.Error(err))
}
}

func NewSQLCommand() *cobra.Command {
@@ -20,7 +23,7 @@ func NewSQLCommand() *cobra.Command {
}

func NewSQLDumpCommand() *cobra.Command {
opts := &SQLOptions{}
opts := &Options{}
cmd := &cobra.Command{
Use: "dump",
RunE: func(cmd *cobra.Command, args []string) error {
@@ -8,22 +8,22 @@ import (
"os"

"github.com/jinzhu/gorm"
_ "github.com/mattn/go-sqlite3"
_ "github.com/mattn/go-sqlite3" // required by gorm
"go.uber.org/zap"
"moul.io/zapgorm"

"pathwar.pw/entity"
)

type SQLOptions struct {
type Options struct {
Path string
}

type dump struct {
Levels []*entity.Level
}

func FromOpts(opts *SQLOptions) (*gorm.DB, error) {
func FromOpts(opts *Options) (*gorm.DB, error) {
db, err := gorm.Open("sqlite3", opts.Path)
if err != nil {
return nil, err
@@ -48,7 +48,7 @@ func FromOpts(opts *SQLOptions) (*gorm.DB, error) {
return db, nil
}

func sqlDump(opts *SQLOptions) error {
func sqlDump(opts *Options) error {
d := dump{}

db, err := FromOpts(opts)

0 comments on commit 2841058

Please sign in to comment.