Skip to content

Commit

Permalink
Merge pull request #1477 from rsteube/update-spec
Browse files Browse the repository at this point in the history
updated carapace-spec
  • Loading branch information
rsteube authored Jan 14, 2023
2 parents bd4810f + c9c96fb commit 00a4ddd
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 13 deletions.
8 changes: 6 additions & 2 deletions cmd/carapace/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,9 @@ var rootCmd = &cobra.Command{
}
case "--spec":
if len(args) > 1 {
specCompletion(args[1], args[2:]...)
if err := specCompletion(args[1], args[2:]...); err != nil {
fmt.Fprintln(cmd.ErrOrStderr(), err.Error())
}
}
case "--macros":
if len(args) > 1 {
Expand Down Expand Up @@ -162,7 +164,9 @@ var rootCmd = &cobra.Command{
}
default:
if specPath, err := completers.SpecPath(args[0]); err == nil {
specCompletion(specPath, args[1:]...)
if err := specCompletion(specPath, args[1:]...); err != nil {
fmt.Fprintln(cmd.ErrOrStderr(), err.Error())
}
} else {
invokeCompleter(args[0])
}
Expand Down
14 changes: 6 additions & 8 deletions cmd/carapace/cmd/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func scrape(path string) {
}
}

func specCompletion(path string, args ...string) {
func specCompletion(path string, args ...string) error {
old := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w
Expand All @@ -51,13 +51,11 @@ func specCompletion(path string, args ...string) {

abs, spec, err := loadSpec(path)
if err != nil {
return /// TODO handle error
}
cmd, err := spec.ToCobra()
if err != nil {
return /// TODO handle error
return err
}

cmd := spec.ToCobra()

a := []string{"_carapace"}
a = append(a, args...)
cmd.SetArgs(a)
Expand All @@ -69,12 +67,12 @@ func specCompletion(path string, args ...string) {

executable, err := os.Executable()
if err != nil {
panic(err.Error()) // TODO exit with error message
return err
}

executableName := filepath.Base(executable)
patched := strings.Replace(string(out), fmt.Sprintf("%v _carapace", executableName), fmt.Sprintf("%v --spec '%v'", executableName, abs), -1) // general callback
patched = strings.Replace(patched, fmt.Sprintf("'%v', '_carapace'", executableName), fmt.Sprintf("'%v', '--spec', '%v'", executableName, abs), -1) // xonsh callback
fmt.Print(patched)

return nil
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510
github.com/pelletier/go-toml v1.9.5
github.com/rsteube/carapace v0.31.0
github.com/rsteube/carapace-spec v0.4.0
github.com/rsteube/carapace-spec v0.5.0
github.com/spf13/cobra v1.6.1
github.com/spf13/pflag v1.0.5
gopkg.in/ini.v1 v1.67.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ github.com/rsteube/carapace v0.31.0 h1:cy+AOgYgZVsQTJ6OGA0FfGQOU+EUFC+THTGrt8D9k
github.com/rsteube/carapace v0.31.0/go.mod h1:/ALYHicIpak6TjQnKl7HupclqJydy2LQb6CkawYBxDo=
github.com/rsteube/carapace-pflag v0.0.4 h1:Onb0cLNLxg1xJr2EsMlBldAI5KkybrvZ89b5cRElZXI=
github.com/rsteube/carapace-pflag v0.0.4/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/rsteube/carapace-spec v0.4.0 h1:96BvQjg2iQiPMFiH40fXtjCy300LIB0nG6/9o+mxZxk=
github.com/rsteube/carapace-spec v0.4.0/go.mod h1:1+DqPSGtMF7VZ5cFu0TGjdYFYcm6wskzF5xdUqQzWwQ=
github.com/rsteube/carapace-spec v0.5.0 h1:XCQsgOK/DSzFcrUfclaOAbukuT3bDH/vKWJX/5SGNM0=
github.com/rsteube/carapace-spec v0.5.0/go.mod h1:1+DqPSGtMF7VZ5cFu0TGjdYFYcm6wskzF5xdUqQzWwQ=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/spf13/cobra v1.6.1 h1:o94oiPyS4KD1mPy2fmcYYHHfCxLqYjJOhGsCHFZtEzA=
github.com/spf13/cobra v1.6.1/go.mod h1:IOw/AERYS7UzyrGinqmz6HLUo219MORXGxhbaJUqzrY=
Expand Down

0 comments on commit 00a4ddd

Please sign in to comment.