Skip to content

Commit b206b17

Browse files
committed
Autocompletion: fix panic by downgrading posener/complete to v1
1 parent 9139849 commit b206b17

9 files changed

+102
-74
lines changed

completion.go

+25-13
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ import (
77
"os"
88
"runtime/debug"
99

10-
"github.com/posener/complete/v2"
10+
"github.com/pkg/errors"
11+
"github.com/posener/complete"
12+
"github.com/rs/zerolog"
13+
"github.com/symfony-cli/terminal"
1114
)
1215

1316
func init() {
@@ -47,41 +50,50 @@ func registerAutocompleteCommands(a *Application) {
4750
}
4851

4952
func AutocompleteAppAction(c *Context) error {
53+
// connect posener/complete logger to our logging facilities
54+
logger := terminal.Logger.WithLevel(zerolog.DebugLevel)
55+
complete.Log = func(format string, args ...interface{}) {
56+
logger.Msgf("completion | "+format, args...)
57+
}
58+
5059
cmd := complete.Command{
51-
Flags: map[string]complete.Predictor{},
52-
Sub: map[string]*complete.Command{},
60+
GlobalFlags: make(complete.Flags),
61+
Sub: make(complete.Commands),
5362
}
5463

5564
// transpose registered commands and flags to posener/complete equivalence
5665
for _, command := range c.App.VisibleCommands() {
5766
subCmd := command.convertToPosenerCompleteCommand(c)
5867

5968
for _, name := range command.Names() {
60-
cmd.Sub[name] = &subCmd
69+
cmd.Sub[name] = subCmd
6170
}
6271
}
6372

6473
for _, f := range c.App.VisibleFlags() {
6574
if vf, ok := f.(*verbosityFlag); ok {
66-
vf.addToPosenerFlags(c, cmd.Flags)
75+
vf.addToPosenerFlags(c, cmd.GlobalFlags)
6776
continue
6877
}
6978

7079
predictor := ContextPredictor{f, c}
7180

7281
for _, name := range f.Names() {
7382
name = fmt.Sprintf("%s%s", prefixFor(name), name)
74-
cmd.Flags[name] = predictor
83+
cmd.GlobalFlags[name] = predictor
7584
}
7685
}
7786

78-
cmd.Complete(c.App.HelpName)
87+
if !complete.New(c.App.HelpName, cmd).Complete() {
88+
return errors.New("Could not run auto-completion")
89+
}
90+
7991
return nil
8092
}
8193

8294
func (c *Command) convertToPosenerCompleteCommand(ctx *Context) complete.Command {
8395
command := complete.Command{
84-
Flags: map[string]complete.Predictor{},
96+
Flags: make(complete.Flags, 0),
8597
}
8698

8799
for _, f := range c.VisibleFlags() {
@@ -98,16 +110,16 @@ func (c *Command) convertToPosenerCompleteCommand(ctx *Context) complete.Command
98110
return command
99111
}
100112

101-
func (c *Command) PredictArgs(ctx *Context, prefix string) []string {
113+
func (c *Command) PredictArgs(ctx *Context, a complete.Args) []string {
102114
if c.ShellComplete != nil {
103-
return c.ShellComplete(ctx, prefix)
115+
return c.ShellComplete(ctx, a)
104116
}
105117

106118
return nil
107119
}
108120

109121
type Predictor interface {
110-
PredictArgs(*Context, string) []string
122+
PredictArgs(*Context, complete.Args) []string
111123
}
112124

113125
// ContextPredictor determines what terms can follow a command or a flag
@@ -119,6 +131,6 @@ type ContextPredictor struct {
119131
}
120132

121133
// Predict invokes the predict function and implements the Predictor interface
122-
func (p ContextPredictor) Predict(prefix string) []string {
123-
return p.predictor.PredictArgs(p.ctx, prefix)
134+
func (p ContextPredictor) Predict(a complete.Args) []string {
135+
return p.predictor.PredictArgs(p.ctx, a)
124136
}

completion_installer.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"text/template"
1313

1414
"github.com/pkg/errors"
15+
"github.com/posener/complete"
1516
"github.com/symfony-cli/terminal"
1617
)
1718

@@ -27,7 +28,7 @@ var shellAutoCompleteInstallCommand = &Command{
2728
{Name: "completion"},
2829
},
2930
Usage: "Dumps the completion script for the current shell",
30-
ShellComplete: func(*Context, string) []string {
31+
ShellComplete: func(context *Context, c complete.Args) []string {
3132
return []string{"bash", "zsh", "fish"}
3233
},
3334
Description: `The <info>{{.HelpName}}</> command dumps the shell completion script required

flag.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import (
3333
"time"
3434

3535
"github.com/pkg/errors"
36+
"github.com/posener/complete"
3637
"github.com/symfony-cli/terminal"
3738
)
3839

@@ -93,7 +94,7 @@ func (f FlagsByName) Swap(i, j int) {
9394
type Flag interface {
9495
fmt.Stringer
9596

96-
PredictArgs(*Context, string) []string
97+
PredictArgs(*Context, complete.Args) []string
9798
Validate(*Context) error
9899
// Apply Flag settings to the given flag set
99100
Apply(*flag.FlagSet)

0 commit comments

Comments
 (0)