Skip to content

Commit

Permalink
all: address some issues identified by gocritic
Browse files Browse the repository at this point in the history
Signed-off-by: Dave Cheney <dave@cheney.net>
  • Loading branch information
davecheney committed Mar 4, 2019
1 parent 1f25cdd commit 6f2e1b6
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 66 deletions.
3 changes: 2 additions & 1 deletion cmd/contour/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ func watchstream(st stream, typeURL string, resources []string) {
check(err)
resp, err := st.Recv()
check(err)
m.Marshal(os.Stdout, resp)
err = m.Marshal(os.Stdout, resp)
check(err)
}
}
2 changes: 1 addition & 1 deletion cmd/contour/contour.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ func main() {
defer log.Println("stopped")
return s.Serve(l)
})
g.Run()
_ = g.Run()
default:
app.Usage(args)
os.Exit(2)
Expand Down
10 changes: 4 additions & 6 deletions internal/contour/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,8 @@ func (v *routeVisitor) visit(vertex dag.Vertex) {
switch vh := vertex.(type) {
case *dag.VirtualHost:
vhost := envoy.VirtualHost(vh.Name, l.Port)
vh.Visit(func(r dag.Vertex) {
switch r := r.(type) {
case *dag.Route:
vh.Visit(func(v dag.Vertex) {
if r, ok := v.(*dag.Route); ok {
var svcs []*dag.HTTPService
r.Visit(func(s dag.Vertex) {
if s, ok := s.(*dag.HTTPService); ok {
Expand Down Expand Up @@ -145,9 +144,8 @@ func (v *routeVisitor) visit(vertex dag.Vertex) {
v.routes["ingress_http"].VirtualHosts = append(v.routes["ingress_http"].VirtualHosts, vhost)
case *dag.SecureVirtualHost:
vhost := envoy.VirtualHost(vh.VirtualHost.Name, l.Port)
vh.Visit(func(r dag.Vertex) {
switch r := r.(type) {
case *dag.Route:
vh.Visit(func(v dag.Vertex) {
if r, ok := v.(*dag.Route); ok {
var svcs []*dag.HTTPService
r.Visit(func(s dag.Vertex) {
if s, ok := s.(*dag.HTTPService); ok {
Expand Down
4 changes: 2 additions & 2 deletions internal/dag/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -808,10 +808,10 @@ func matchesPathPrefix(path, prefix string) bool {
return false
}
if prefix[len(prefix)-1] != '/' {
prefix = prefix + "/"
prefix += "/"
}
if path[len(path)-1] != '/' {
path = path + "/"
path += "/"
}
return strings.HasPrefix(path, prefix)
}
Expand Down
58 changes: 23 additions & 35 deletions internal/dag/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2763,17 +2763,15 @@ func TestDAGInsert(t *testing.T) {

got := make(map[int]*Listener)
dag.Visit(func(v Vertex) {
switch v := v.(type) {
case *Listener:
got[v.Port] = v
if l, ok := v.(*Listener); ok {
got[l.Port] = l
}
})

want := make(map[int]*Listener)
for _, v := range tc.want {
switch v := v.(type) {
case *Listener:
want[v.Port] = v
if l, ok := v.(*Listener); ok {
want[l.Port] = l
}
}

Expand Down Expand Up @@ -2925,9 +2923,8 @@ func TestDAGIngressRouteCycle(t *testing.T) {

got := make(map[int]*Listener)
dag.Visit(func(v Vertex) {
switch v := v.(type) {
case *Listener:
got[v.Port] = v
if l, ok := v.(*Listener); ok {
got[l.Port] = l
}
})

Expand Down Expand Up @@ -2976,9 +2973,8 @@ func TestDAGIngressRouteCycleSelfEdge(t *testing.T) {

got := make(map[int]*Listener)
dag.Visit(func(v Vertex) {
switch v := v.(type) {
case *Listener:
got[v.Port] = v
if l, ok := v.(*Listener); ok {
got[l.Port] = l
}
})

Expand Down Expand Up @@ -3017,9 +3013,8 @@ func TestDAGIngressRouteDelegatesToNonExistent(t *testing.T) {

got := make(map[int]*Listener)
dag.Visit(func(v Vertex) {
switch v := v.(type) {
case *Listener:
got[v.Port] = v
if l, ok := v.(*Listener); ok {
got[l.Port] = l
}
})

Expand Down Expand Up @@ -3073,9 +3068,8 @@ func TestDAGIngressRouteDelegatePrefixDoesntMatch(t *testing.T) {

got := make(map[int]*Listener)
dag.Visit(func(v Vertex) {
switch v := v.(type) {
case *Listener:
got[v.Port] = v
if l, ok := v.(*Listener); ok {
got[l.Port] = l
}
})

Expand Down Expand Up @@ -3186,14 +3180,11 @@ func TestDAGRootNamespaces(t *testing.T) {

var count int
dag.Visit(func(v Vertex) {
switch v := v.(type) {
case *Listener:
v.Visit(func(v Vertex) {
if _, ok := v.(*VirtualHost); ok {
count++
}
})
}
v.Visit(func(v Vertex) {
if _, ok := v.(*VirtualHost); ok {
count++
}
})
})

if tc.want != count {
Expand Down Expand Up @@ -3249,9 +3240,8 @@ func TestDAGIngressRouteDelegatePrefixMatchesStringPrefixButNotPathPrefix(t *tes

got := make(map[int]*Listener)
dag.Visit(func(v Vertex) {
switch v := v.(type) {
case *Listener:
got[v.Port] = v
if l, ok := v.(*Listener); ok {
got[l.Port] = l
}
})

Expand Down Expand Up @@ -3835,17 +3825,15 @@ func TestDAGIngressRouteUniqueFQDNs(t *testing.T) {
dag := b.Build()
got := make(map[int]*Listener)
dag.Visit(func(v Vertex) {
switch v := v.(type) {
case *Listener:
got[v.Port] = v
if l, ok := v.(*Listener); ok {
got[l.Port] = l
}
})

want := make(map[int]*Listener)
for _, v := range tc.want {
switch v := v.(type) {
case *Listener:
want[v.Port] = v
if l, ok := v.(*Listener); ok {
want[l.Port] = l
}
}

Expand Down
6 changes: 1 addition & 5 deletions internal/debug/dot.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,7 @@ func (c *ctx) writeEdge(parent, child dag.Vertex) {
return
}
c.edges[pair{parent, child}] = true
switch child := child.(type) {
default:
fmt.Fprintf(c.w, `"%p" -> "%p"`+"\n", parent, child)
}

fmt.Fprintf(c.w, `"%p" -> "%p"`+"\n", parent, child)
}

func (dw *dotWriter) writeDot(w io.Writer) {
Expand Down
2 changes: 1 addition & 1 deletion internal/httpsvc/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (svc *Service) Start(stop <-chan struct{}) (err error) {
ctx := context.Background()
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
s.Shutdown(ctx)
_ = s.Shutdown(ctx) // ignored, will always be a cancelation error
}()

svc.WithField("address", s.Addr).Info("started")
Expand Down
33 changes: 18 additions & 15 deletions internal/metrics/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,15 +216,16 @@ func TestWriteIngressRouteMetric(t *testing.T) {
gotOrphaned := []*io_prometheus_client.Metric{}
gotRoot := []*io_prometheus_client.Metric{}
for _, mf := range gathering {
if mf.GetName() == tc.total.metric {
switch mf.GetName() {
case tc.total.metric:
gotTotal = mf.Metric
} else if mf.GetName() == tc.valid.metric {
case tc.valid.metric:
gotValid = mf.Metric
} else if mf.GetName() == tc.invalid.metric {
case tc.invalid.metric:
gotInvalid = mf.Metric
} else if mf.GetName() == tc.orphaned.metric {
case tc.orphaned.metric:
gotOrphaned = mf.Metric
} else if mf.GetName() == tc.root.metric {
case tc.root.metric:
gotRoot = mf.Metric
}
}
Expand Down Expand Up @@ -640,15 +641,16 @@ func TestRemoveMetric(t *testing.T) {
gotOrphaned := []*io_prometheus_client.Metric{}
gotRoot := []*io_prometheus_client.Metric{}
for _, mf := range gathering {
if mf.GetName() == total.metric {
switch mf.GetName() {
case total.metric:
gotTotal = mf.Metric
} else if mf.GetName() == valid.metric {
case valid.metric:
gotValid = mf.Metric
} else if mf.GetName() == invalid.metric {
case invalid.metric:
gotInvalid = mf.Metric
} else if mf.GetName() == orphaned.metric {
case orphaned.metric:
gotOrphaned = mf.Metric
} else if mf.GetName() == root.metric {
case root.metric:
gotRoot = mf.Metric
}
}
Expand Down Expand Up @@ -688,15 +690,16 @@ func TestRemoveMetric(t *testing.T) {
gotOrphaned = []*io_prometheus_client.Metric{}
gotRoot = []*io_prometheus_client.Metric{}
for _, mf := range gathering {
if mf.GetName() == total.metric {
switch mf.GetName() {
case total.metric:
gotTotal = mf.Metric
} else if mf.GetName() == valid.metric {
case valid.metric:
gotValid = mf.Metric
} else if mf.GetName() == invalid.metric {
case invalid.metric:
gotInvalid = mf.Metric
} else if mf.GetName() == orphaned.metric {
case orphaned.metric:
gotOrphaned = mf.Metric
} else if mf.GetName() == root.metric {
case root.metric:
gotRoot = mf.Metric
}
}
Expand Down

0 comments on commit 6f2e1b6

Please sign in to comment.