[rulesctl] Restructure `List` command to make nil check meaningful

This is an often-surprising Go quirk, so I will explain a bit:

An interface has two components: the concrete type, and the value. If
both of these are `nil`, then the interface is equal to `nil`. If the
concrete type is non-nil, even if the value for that type is nil, then
the interface is non-nil. For a toy example, see:
https://play.golang.org/p/0uLrBekVg_y.

In this specific case, we were taking `rules.Find`, which returns
`*rules.Rule` and assigning that to a variable with the type of
`interface{}`. Since we have a concrete type for the interface, the `out
!= nil` check will never be false. The fix here is to use a second
variable, which will have the concrete type, and check if the pointer is
nil, which is what we actually wanted here.

Signed-off-by: Andrew Mason <amason@slack-corp.com>
This commit is contained in:
Andrew Mason 2021-05-29 11:57:18 -04:00
Родитель 703e8f10d6
Коммит f15e7a6c16
1 изменённых файлов: 4 добавлений и 2 удалений

Просмотреть файл

@ -40,11 +40,13 @@ func List() *cobra.Command {
out = rules
}
} else {
out = rules.Find(listOptName)
if listOptNamesOnly && out != nil {
rule := rules.Find(listOptName)
if listOptNamesOnly && rule != nil {
out = listOptName
} else if listOptNamesOnly {
out = ""
} else {
out = rule
}
}