diff --git a/cmd/status.go b/cmd/status.go index c85a31fe..405d4199 100644 --- a/cmd/status.go +++ b/cmd/status.go @@ -145,12 +145,14 @@ func parseTableInt(i int) string { } func colorStatus(s string) string { - if s == "up" || s == "Established" { + switch { + case s == "up" || s == "Established": return color.GreenString(s) - } else if strings.Contains(s, "Error") || s == "down" { + case strings.Contains(s, "Error") || s == "down": return color.RedString(s) - } else if strings.Contains(s, "Connect") || s == "start" { + case strings.Contains(s, "Connect") || s == "start": return color.YellowString(s) + default: + return s } - return s } diff --git a/pkg/bird/bird.go b/pkg/bird/bird.go index 85c3d692..7d6eca3e 100644 --- a/pkg/bird/bird.go +++ b/pkg/bird/bird.go @@ -280,21 +280,22 @@ func restoreIndent(input string) string { for i := range lines { line := strings.TrimSpace(lines[i]) - if line == "" || line == "\n" { + switch { + case line == "" || line == "\n": continue - } else if strings.HasSuffix(line, "{") && strings.HasPrefix(line, "}") { + case strings.HasSuffix(line, "{") && strings.HasPrefix(line, "}"): if indent == 0 { formatted += strings.Repeat(" ", indent) + line + "\n" } else { formatted += strings.Repeat(" ", indent-1) + line + "\n" } - } else if strings.HasSuffix(line, "{") || strings.HasSuffix(line, "[") { // Opening + case strings.HasSuffix(line, "{") || strings.HasSuffix(line, "["): // Opening formatted += strings.Repeat(" ", indent) + line + "\n" indent++ - } else if strings.HasPrefix(line, "}") || strings.HasPrefix(line, "]") { + case strings.HasPrefix(line, "}") || strings.HasPrefix(line, "]"): indent-- formatted += strings.Repeat(" ", indent) + line + "\n" - } else { + default: formatted += strings.Repeat(" ", indent) + line + "\n" } } diff --git a/pkg/block/block.go b/pkg/block/block.go index 79ebba44..b5b11817 100644 --- a/pkg/block/block.go +++ b/pkg/block/block.go @@ -68,13 +68,15 @@ func Parse(blocklist []string) ([]uint32, []string, error) { // Remove whitespace token = strings.TrimSpace(token) - if asn := parseASN(token); asn != -1 { + asn := parseASN(token) + switch { + case asn != -1: log.Debugf("Adding ASN to blocklist: %d", asn) asns = append(asns, uint32(asn)) - } else if validPrefix(token) { + case validPrefix(token): log.Debugf("Adding prefix to blocklist: %s", token) prefixes = append(prefixes, token) - } else if validIP(token) { + case validIP(token): log.Debugf("Adding IP to blocklist: %s", token) afiSuffix := "/32" @@ -82,7 +84,7 @@ func Parse(blocklist []string) ([]uint32, []string, error) { afiSuffix = "/128" } prefixes = append(prefixes, token+afiSuffix) - } else { + default: return nil, nil, fmt.Errorf("invalid blocklist token: %s", token) } } diff --git a/pkg/irr/irr.go b/pkg/irr/irr.go index 0afbe759..1d905d28 100644 --- a/pkg/irr/irr.go +++ b/pkg/irr/irr.go @@ -81,11 +81,12 @@ func Update(peerData *config.Peer, irrServer string, queryTimeout uint, bgpqBin, var hasNeighbor4, hasNeighbor6 bool if peerData.NeighborIPs != nil { for _, n := range *peerData.NeighborIPs { - if strings.Contains(n, ".") { + switch { + case strings.Contains(n, "."): hasNeighbor4 = true - } else if strings.Contains(n, ":") { + case strings.Contains(n, ":"): hasNeighbor6 = true - } else { + default: log.Fatalf("Invalid neighbor IP %s", n) } } diff --git a/pkg/process/process_test.go b/pkg/process/process_test.go index ace2b54a..bc40b5c5 100644 --- a/pkg/process/process_test.go +++ b/pkg/process/process_test.go @@ -245,24 +245,23 @@ peers: t.Error(err) } - for peerName, peerData := range globalConfig.Peers { - if peerName == "Upstream 1" { - assert.Equal(t, 65510, *peerData.ASN) - assert.Equal(t, 90, *peerData.LocalPref) - assert.False(t, *peerData.FilterIRR) - assert.True(t, *peerData.FilterRPKI) - } else if peerName == "Upstream 2" { - assert.Equal(t, 65520, *peerData.ASN) - assert.Equal(t, 90, *peerData.LocalPref) - assert.True(t, *peerData.FilterIRR) - assert.True(t, *peerData.FilterRPKI) - } else if peerName == "Upstream 3" { - assert.Equal(t, 65530, *peerData.ASN) - assert.Equal(t, 2, *peerData.LocalPref) - assert.False(t, *peerData.FilterIRR) - assert.True(t, *peerData.FilterRPKI) - } else { - t.Errorf("unexpected peer %s", peerName) - } - } + assert.Len(t, globalConfig.Peers, 3) + + upstream1 := globalConfig.Peers["Upstream 1"] + assert.Equal(t, 65510, *upstream1.ASN) + assert.Equal(t, 90, *upstream1.LocalPref) + assert.False(t, *upstream1.FilterIRR) + assert.True(t, *upstream1.FilterRPKI) + + upstream2 := globalConfig.Peers["Upstream 2"] + assert.Equal(t, 65520, *upstream2.ASN) + assert.Equal(t, 90, *upstream2.LocalPref) + assert.True(t, *upstream2.FilterIRR) + assert.True(t, *upstream2.FilterRPKI) + + upstream3 := globalConfig.Peers["Upstream 3"] + assert.Equal(t, 65530, *upstream3.ASN) + assert.Equal(t, 2, *upstream3.LocalPref) + assert.False(t, *upstream3.FilterIRR) + assert.True(t, *upstream3.FilterRPKI) }