Skip to content

Commit

Permalink
storage: fix flag mutex
Browse files Browse the repository at this point in the history
  • Loading branch information
rsteube committed Sep 16, 2023
1 parent 2b5ca80 commit c3751f4
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
7 changes: 6 additions & 1 deletion storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,10 @@ func (s _storage) getFlag(cmd *cobra.Command, name string) Action {
if flag := cmd.LocalFlags().Lookup(name); flag == nil && cmd.HasParent() {
return s.getFlag(cmd.Parent(), name)
} else {
a := s.preinvoke(cmd, flag, s.get(cmd).flag[name])
entry := s.get(cmd)
entry.flagMutex.RLock()
defer entry.flagMutex.RUnlock()
a := s.preinvoke(cmd, flag, entry.flag[name])

return ActionCallback(func(c Context) Action { // TODO verify order of execution is correct
invoked := a.Invoke(c)
Expand Down Expand Up @@ -137,11 +140,13 @@ 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))
}
}
entry.flagMutex.RUnlock()
}
return errors
}
Expand Down
5 changes: 5 additions & 0 deletions storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ func BenchmarkStorage(b *testing.B) {
"flag2": ActionValues("a", "b"),
})
Gen(cmd2).PositionalCompletion(ActionValues("a", "b"))

storage.getFlag(cmd, "flag1")
storage.getPositional(cmd, 0)
storage.getFlag(cmd2, "flag2")
storage.getPositional(cmd2, 0)
}
})

Expand Down

0 comments on commit c3751f4

Please sign in to comment.