From a6218855722e8082fd7dc6c7ba7c05b0a6ebf897 Mon Sep 17 00:00:00 2001 From: rsteube Date: Mon, 27 May 2024 18:18:40 +0200 Subject: [PATCH] gh: api - fix fake repo flag --- completers/gh_completer/cmd/action/api.go | 12 ++++++++++-- completers/gh_completer/cmd/action/gist.go | 6 ++---- completers/gh_completer/cmd/action/repository.go | 2 +- completers/gh_completer/cmd/action/search.go | 1 + 4 files changed, 14 insertions(+), 7 deletions(-) diff --git a/completers/gh_completer/cmd/action/api.go b/completers/gh_completer/cmd/action/api.go index 0f951cd673..46a7d907cc 100644 --- a/completers/gh_completer/cmd/action/api.go +++ b/completers/gh_completer/cmd/action/api.go @@ -3,6 +3,7 @@ package action import ( "fmt" "strings" + "sync" "github.com/carapace-sh/carapace" "github.com/carapace-sh/carapace-bin/pkg/actions/tools/gh" @@ -107,10 +108,17 @@ func ActionApiV3Paths(cmd *cobra.Command) carapace.Action { }) } +var fakeRepoFlagMutex sync.Mutex + func fakeRepoFlag(cmd *cobra.Command, owner, repo string) { if cmd.Flag("repo") == nil { - cmd.Flags().String("repo", fmt.Sprintf("%v/%v", owner, repo), "fake repo flag") - cmd.Flag("repo").Changed = true + fakeRepoFlagMutex.Lock() + defer fakeRepoFlagMutex.Unlock() + + if cmd.Flag("repo") == nil { + cmd.Flags().String("repo", fmt.Sprintf("%v/%v", owner, repo), "fake repo flag") + cmd.Flag("repo").Changed = true + } } } diff --git a/completers/gh_completer/cmd/action/gist.go b/completers/gh_completer/cmd/action/gist.go index 30a57c8de8..22a33f4c7b 100644 --- a/completers/gh_completer/cmd/action/gist.go +++ b/completers/gh_completer/cmd/action/gist.go @@ -52,8 +52,7 @@ func ActionGistUrls(cmd *cobra.Command) carapace.Action { case 1: return gh.ActionUsers(gh.HostOpts{}).Invoke(c).Suffix("/").ToA() case 2: - cmd.Flags().String("repo", fmt.Sprintf("%v/%v/", c.Parts[0], c.Parts[1]), "fake repo flag") - cmd.Flag("repo").Changed = true + fakeRepoFlag(cmd, c.Parts[0], c.Parts[1]) return ActionGistIds(cmd) default: return carapace.ActionValues() @@ -104,8 +103,7 @@ func ActionGistFiles(cmd *cobra.Command, name string) carapace.Action { switch len(splitted) { case 1: case 5: - cmd.Flags().String("repo", fmt.Sprintf("%v/%v/", splitted[2], splitted[3]), "fake repo flag") - cmd.Flag("repo").Changed = true + fakeRepoFlag(cmd, splitted[2], splitted[3]) default: return carapace.ActionMessage("invalid gist id/url: " + name) } diff --git a/completers/gh_completer/cmd/action/repository.go b/completers/gh_completer/cmd/action/repository.go index 51ba960ae4..25eb8dd7bb 100644 --- a/completers/gh_completer/cmd/action/repository.go +++ b/completers/gh_completer/cmd/action/repository.go @@ -10,7 +10,7 @@ func ActionOwnerRepositories(cmd *cobra.Command) carapace.Action { return carapace.ActionMultiParts("/", func(c carapace.Context) carapace.Action { // TODO hack to enable completion outside git repo - this needs to be fixed in GraphQlAction/repooverride though if cmd.Flag("repo") == nil { - cmd.Flags().String("repo", "", "") + fakeRepoFlag(cmd, "", "") } switch len(c.Parts) { diff --git a/completers/gh_completer/cmd/action/search.go b/completers/gh_completer/cmd/action/search.go index aa7d7b336e..278db6da4d 100644 --- a/completers/gh_completer/cmd/action/search.go +++ b/completers/gh_completer/cmd/action/search.go @@ -25,6 +25,7 @@ func ActionSearchMultiRepo(cmd *cobra.Command, f func(cmd *cobra.Command) carapa name := repo batch = append(batch, carapace.ActionCallback(func(c carapace.Context) carapace.Action { dummyCmd := &cobra.Command{} + // TODO use fakeRepoFlag? dummyCmd.Flags().String("repo", name, "fake repo flag") if name != "" { dummyCmd.Flag("repo").Changed = true