Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh: api - fix fake repo flag #2390

Merged
merged 1 commit into from
May 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions completers/gh_completer/cmd/action/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
}
}

Expand Down
6 changes: 2 additions & 4 deletions completers/gh_completer/cmd/action/gist.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion completers/gh_completer/cmd/action/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions completers/gh_completer/cmd/action/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down