From fbb1cac624ccdf28e468179430c984b40303bbb0 Mon Sep 17 00:00:00 2001 From: rsteube Date: Sat, 16 Sep 2023 19:21:43 +0200 Subject: [PATCH] storage: use defer --- storage.go | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/storage.go b/storage.go index 36a17f2c..082d986b 100644 --- a/storage.go +++ b/storage.go @@ -140,13 +140,16 @@ func (s _storage) getPositional(cmd *cobra.Command, index int) Action { func (s _storage) check() []string { errors := make([]string, 0) for cmd, entry := range s { - entry.flagMutex.RLock() - for name := range entry.flag { - if flag := cmd.LocalFlags().Lookup(name); flag == nil { - errors = append(errors, fmt.Sprintf("unknown flag for %s: %s\n", uid.Command(cmd), name)) + func() { + entry.flagMutex.RLock() + defer entry.flagMutex.RUnlock() + + for name := range entry.flag { + if flag := cmd.LocalFlags().Lookup(name); flag == nil { + errors = append(errors, fmt.Sprintf("unknown flag for %s: %s\n", uid.Command(cmd), name)) + } } - } - entry.flagMutex.RUnlock() + }() } return errors }