Skip to content

Commit

Permalink
Merge pull request #2390 from carapace-sh/gh-api-fix-repo-flag
Browse files Browse the repository at this point in the history
gh: api - fix fake repo flag
  • Loading branch information
rsteube authored May 27, 2024
2 parents 3da8f9b + a621885 commit 3ecb266
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 7 deletions.
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

0 comments on commit 3ecb266

Please sign in to comment.