Skip to content

Commit

Permalink
added gonew
Browse files Browse the repository at this point in the history
  • Loading branch information
rsteube committed Aug 26, 2023
1 parent 8c5a2c9 commit 92f23df
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 5 deletions.
26 changes: 26 additions & 0 deletions completers/gonew_completer/cmd/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package cmd

import (
"github.com/rsteube/carapace"
"github.com/rsteube/carapace-bin/pkg/actions/tools/golang"
"github.com/spf13/cobra"
)

var rootCmd = &cobra.Command{
Use: "gonew",
Short: "Gonew starts a new Go module by copying a template module",
Long: "https://pkg.go.dev/golang.org/x/tools/cmd/gonew",
Run: func(cmd *cobra.Command, args []string) {},
}

func Execute() error {
return rootCmd.Execute()
}
func init() {
carapace.Gen(rootCmd).Standalone()

carapace.Gen(rootCmd).PositionalCompletion(
golang.ActionModuleSearch(),
golang.ActionModuleSearch(),
)
}
7 changes: 7 additions & 0 deletions completers/gonew_completer/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package main

import "github.com/rsteube/carapace-bin/completers/gonew_completer/cmd"

func main() {
cmd.Execute()
}
4 changes: 2 additions & 2 deletions pkg/actions/tools/gh/content.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type ContentOpts struct {
Owner string
Name string
Branch string
Path string
Path string // TODO should be handled using `Context.Value`
}

func (c ContentOpts) repo() RepoOpts {
Expand Down Expand Up @@ -46,7 +46,7 @@ func ActionContents(opts ContentOpts) carapace.Action {
}
vals = append(vals, name, style.ForPathExt(name, c))
}
return carapace.ActionStyledValues(vals...)
return carapace.ActionStyledValues(vals...).NoSpace('/')
})
})
}
36 changes: 33 additions & 3 deletions pkg/actions/tools/golang/search.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package golang

import (
"net/url"
"path/filepath"
"strings"

"github.com/rsteube/carapace"
"github.com/rsteube/carapace-bin/pkg/actions/tools/gh"
"github.com/rsteube/carapace-bin/pkg/actions/tools/git"
)

Expand All @@ -13,9 +18,34 @@ func ActionModuleSearch() carapace.Action {
return carapace.ActionMultiParts("@", func(c carapace.Context) carapace.Action {
switch len(c.Parts) {
case 0:
opts := git.SearchOpts{}.Default()
opts.Prefix = false
return git.ActionRepositorySearch(opts).NoSpace()
if strings.Count(c.Value, "/") < 3 {
opts := git.SearchOpts{}.Default()
opts.Prefix = false
return git.ActionRepositorySearch(opts).NoSpace()
}

if strings.HasPrefix(c.Value, "github.com") {
splitted := strings.Split(c.Value, "/")
prefix := strings.Join(splitted[:3], "/") + "/"
return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
// TODO this should be done in ActionContents using `Context.Value` instead of `ContentOpts.Path`
dir := filepath.Dir(c.Value)
dirPrefix := dir + "/"
if dir == "." {
dir = ""
dirPrefix = ""
}
return gh.ActionContents(gh.ContentOpts{
Host: splitted[0],
Owner: splitted[1],
Name: splitted[2],
Branch: "",
Path: url.PathEscape(dir),
}).Prefix(dirPrefix)
}).Prefix(prefix)
}

return carapace.ActionValues()
case 1:
return carapace.Batch(
git.ActionLsRemoteRefs(git.LsRemoteRefOption{Url: "https://" + c.Parts[0], Tags: true}),
Expand Down

0 comments on commit 92f23df

Please sign in to comment.