Skip to content

Commit

Permalink
refactor: cleanup conditionals
Browse files Browse the repository at this point in the history
  • Loading branch information
natesales committed Sep 2, 2024
1 parent 63557f7 commit fbe69b6
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 36 deletions.
10 changes: 6 additions & 4 deletions cmd/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
11 changes: 6 additions & 5 deletions pkg/bird/bird.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
Expand Down
10 changes: 6 additions & 4 deletions pkg/block/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,21 +68,23 @@ 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"
if strings.Contains(token, ":") {
afiSuffix = "/128"
}
prefixes = append(prefixes, token+afiSuffix)
} else {
default:
return nil, nil, fmt.Errorf("invalid blocklist token: %s", token)
}
}
Expand Down
7 changes: 4 additions & 3 deletions pkg/irr/irr.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down
39 changes: 19 additions & 20 deletions pkg/process/process_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit fbe69b6

Please sign in to comment.