Skip to content

Commit

Permalink
Merge pull request #71 from peter-edge/linting
Browse files Browse the repository at this point in the history
Get ghz compliant with errcheck
  • Loading branch information
bojand authored Feb 24, 2019
2 parents 063c563 + 9005ad2 commit 645ad20
Show file tree
Hide file tree
Showing 10 changed files with 213 additions and 134 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ staticcheck:
# Lint runs all linters. This is the main lint target to run.
# TODO: add errcheck and staticcheck when the code is updated to pass them
.PHONY: lint
lint: golint
lint: golint errcheck

# Test runs go test on GO_PKGS. This does not produce code coverage.
.PHONY: test
Expand Down
19 changes: 15 additions & 4 deletions cmd/ghz-web/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,16 @@ func main() {

conf, err := config.Read(cfgPath)
if err != nil {
panic(err)
handleError(err)
}

db, err := database.New(conf.Database.Type, conf.Database.Connection, conf.Log.Level == "debug")
if err != nil {
panic(err)
handleError(err)
}
defer db.Close()
defer func() {
handleError(db.Close())
}()

info := &api.ApplicationInfo{
Version: version,
Expand All @@ -70,11 +72,20 @@ func main() {

server, err := router.New(db, info, conf)
if err != nil {
panic(err)
handleError(err)
}

router.PrintRoutes(server)

hostPort := net.JoinHostPort("", strconv.FormatUint(uint64(conf.Server.Port), 10))
server.Logger.Fatal(server.Start(hostPort))
}

func handleError(err error) {
if err != nil {
if errString := err.Error(); errString != "" {
fmt.Fprintln(os.Stderr, errString)
}
os.Exit(1)
}
}
28 changes: 17 additions & 11 deletions cmd/ghz/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ func main() {
var conf config
err := configor.Load(&conf, cfgPath)
if err != nil {
errAndExit(err.Error())
handleError(err)
}

cfg = &conf
Expand All @@ -159,7 +159,7 @@ func main() {
var err error
cfg, err = createConfigFromArgs()
if err != nil {
errAndExit(err.Error())
handleError(err)
}
}

Expand Down Expand Up @@ -220,31 +220,37 @@ func main() {

report, err := runner.Run(cfg.Call, cfg.Host, options...)
if err != nil {
errAndExit(err.Error())
handleError(err)
}

output := os.Stdout
outputPath := strings.TrimSpace(cfg.Output)
if outputPath != "" {
f, err := os.Create(outputPath)
if err != nil {
errAndExit(err.Error())
handleError(err)
}
defer f.Close()
defer func() {
handleError(f.Close())
}()
output = f
}

p := printer.ReportPrinter{
Report: report,
Out: output}
Out: output,
}

p.Print(cfg.Format)
handleError(p.Print(cfg.Format))
}

func errAndExit(msg string) {
fmt.Fprintf(os.Stderr, msg)
fmt.Fprintf(os.Stderr, "\n")
os.Exit(1)
func handleError(err error) {
if err != nil {
if errString := err.Error(); errString != "" {
fmt.Fprintln(os.Stderr, errString)
}
os.Exit(1)
}
}

func usageAndExit(msg string) {
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ require (
github.com/stretchr/testify v1.3.0
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasttemplate v0.0.0-20170224212429-dcecefd839c4 // indirect
go.uber.org/atomic v1.3.2 // indirect
go.uber.org/multierr v1.1.0
golang.org/x/crypto v0.0.0-20190103213133-ff983b9c42bc // indirect
golang.org/x/net v0.0.0-20190110200230-915654e7eabc
google.golang.org/grpc v1.17.0
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6Kllzaw
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
github.com/valyala/fasttemplate v0.0.0-20170224212429-dcecefd839c4 h1:gKMu1Bf6QINDnvyZuTaACm9ofY+PRh+5vFz4oxBZeF8=
github.com/valyala/fasttemplate v0.0.0-20170224212429-dcecefd839c4/go.mod h1:50wTf68f99/Zt14pr046Tgt3Lp2vLyFZKzbFXTOabXw=
go.uber.org/atomic v1.3.2 h1:2Oa65PReHzfn29GpvgsYwloV9AVFHPDk8tYxt2c2tr4=
go.uber.org/atomic v1.3.2/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=
go.uber.org/multierr v1.1.0 h1:HoEmRHQPVSqub6w2z2d2EOVs2fjyFRGyofhKuyDq0QI=
go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/0=
golang.org/x/crypto v0.0.0-20190103213133-ff983b9c42bc h1:F5tKCVGp+MUAHhKp5MZtGqAlGX3+oCsiL1Q629FL90M=
golang.org/x/crypto v0.0.0-20190103213133-ff983b9c42bc/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3 h1:x/bBzNauLQAlE3fLku/xy92Y8QwKX5HZymrMz2IiKFc=
Expand Down
10 changes: 6 additions & 4 deletions internal/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ import (
"github.com/bojand/ghz/internal/helloworld"
)

// TestPort is the port
// TestPort is the port.
var TestPort string

// TestLocalhost is the localhost
// TestLocalhost is the localhost.
var TestLocalhost string

// StartServer starts server
// StartServer starts the server.
//
// For testing only.
func StartServer(secure bool) (*helloworld.Greeter, *grpc.Server, error) {
lis, err := net.Listen("tcp", ":0")
if err != nil {
Expand All @@ -44,7 +46,7 @@ func StartServer(secure bool) (*helloworld.Greeter, *grpc.Server, error) {
TestLocalhost = "localhost:" + TestPort

go func() {
s.Serve(lis)
_ = s.Serve(lis)
}()

return gs, s, err
Expand Down
Loading

0 comments on commit 645ad20

Please sign in to comment.