From c3751f46abbe5f0e1ce058a1aed91fe45546fe58 Mon Sep 17 00:00:00 2001 From: rsteube Date: Sat, 16 Sep 2023 19:03:30 +0200 Subject: [PATCH] storage: fix flag mutex --- storage.go | 7 ++++++- storage_test.go | 5 +++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/storage.go b/storage.go index fe4b8c22..36a17f2c 100644 --- a/storage.go +++ b/storage.go @@ -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) @@ -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 } diff --git a/storage_test.go b/storage_test.go index 729baf3b..48bb1ea5 100644 --- a/storage_test.go +++ b/storage_test.go @@ -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) } })