Skip to content

Commit 1cca4d7

Browse files
committed
Adding back {bash,zsh,fish,powershell} completions
1 parent 44c8877 commit 1cca4d7

File tree

2 files changed

+48
-11
lines changed

2 files changed

+48
-11
lines changed

main.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@ func main() {
3737
return
3838
}
3939
// run
40-
c := NewCommand(os.Args)
41-
if err := c.ExecuteContext(context.Background()); err != nil && err != io.EOF && err != rline.ErrInterrupt {
40+
if err := New(os.Args).ExecuteContext(context.Background()); err != nil && err != io.EOF && err != rline.ErrInterrupt {
4241
var he *handler.Error
4342
if !errors.As(err, &he) {
4443
fmt.Fprintf(os.Stderr, "error: %v\n", err)

run.go

+47-9
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,25 @@ type ContextExecutor interface {
2525
ExecuteContext(context.Context) error
2626
}
2727

28-
// NewCommand builds the command context.
29-
func NewCommand(cliargs []string) ContextExecutor {
28+
// New builds the command context.
29+
func New(cliargs []string) ContextExecutor {
3030
args := &Args{}
31+
var (
32+
bashCompletion bool
33+
zshCompletion bool
34+
fishCompletion bool
35+
powershellCompletion bool
36+
noDescriptions bool
37+
)
3138
v := viper.New()
3239
c := &cobra.Command{
33-
Use: text.CommandName + " [flags]... [DSN]",
34-
Short: text.Short(),
35-
Version: text.CommandVersion,
40+
Use: text.CommandName + " [flags]... [DSN]",
41+
Short: text.Short(),
42+
Version: text.CommandVersion,
43+
SilenceErrors: true,
44+
SilenceUsage: true,
45+
DisableAutoGenTag: true,
46+
DisableSuggestions: true,
3647
Args: func(_ *cobra.Command, cliargs []string) error {
3748
if len(cliargs) > 1 {
3849
return text.ErrWrongNumberOfArguments
@@ -74,6 +85,22 @@ func NewCommand(cliargs []string) ContextExecutor {
7485
return nil
7586
},
7687
RunE: func(cmd *cobra.Command, cliargs []string) error {
88+
switch {
89+
case bashCompletion:
90+
return cmd.GenBashCompletionV2(os.Stdout, !noDescriptions)
91+
case zshCompletion:
92+
if noDescriptions {
93+
return cmd.GenZshCompletionNoDesc(os.Stdout)
94+
}
95+
return cmd.GenZshCompletion(os.Stdout)
96+
case fishCompletion:
97+
return cmd.GenFishCompletion(os.Stdout, !noDescriptions)
98+
case powershellCompletion:
99+
if noDescriptions {
100+
return cmd.GenPowerShellCompletion(os.Stdout)
101+
}
102+
return cmd.GenPowerShellCompletionWithDesc(os.Stdout)
103+
}
77104
if len(cliargs) > 0 {
78105
args.DSN = cliargs[0]
79106
}
@@ -83,10 +110,17 @@ func NewCommand(cliargs []string) ContextExecutor {
83110

84111
c.SetVersionTemplate("{{ .Name }} {{ .Version }}\n")
85112
c.SetArgs(cliargs[1:])
86-
c.SilenceUsage, c.SilenceErrors = true, true
87113
c.SetUsageTemplate(text.UsageTemplate)
88114

89115
flags := c.Flags()
116+
117+
// completions
118+
flags.BoolVar(&bashCompletion, "completion-script-bash", false, "output bash completion script and exit")
119+
flags.BoolVar(&zshCompletion, "completion-script-zsh", false, "output zsh completion script and exit")
120+
flags.BoolVar(&fishCompletion, "completion-script-fish", false, "output fish completion script and exit")
121+
flags.BoolVar(&powershellCompletion, "completion-script-powershell", false, "output powershell completion script and exit")
122+
flags.BoolVar(&noDescriptions, "no-descriptions", false, "disable descriptions in completion scripts")
123+
90124
flags.SortFlags = false
91125
// command / file flags
92126
flags.VarP(commandOrFile{args, true}, "command", "c", "run only single command (SQL or internal) and exit")
@@ -145,8 +179,12 @@ func NewCommand(cliargs []string) ContextExecutor {
145179
_, _ = w.Write([]byte(s))
146180
}
147181
// mark hidden
148-
for _, s := range []string{"no-psqlrc", "no-usqlrc", "var", "variable"} {
149-
if err := flags.MarkHidden(s); err != nil {
182+
for _, name := range []string{
183+
"no-psqlrc", "no-usqlrc", "var", "variable",
184+
"completion-script-bash", "completion-script-zsh", "completion-script-fish",
185+
"completion-script-powershell", "no-descriptions",
186+
} {
187+
if err := flags.MarkHidden(name); err != nil {
150188
panic(err)
151189
}
152190
}
@@ -313,7 +351,7 @@ func (c commandOrFile) Type() string {
313351
return "FILE"
314352
}
315353

316-
// vs handles setting vars with predefined var names.
354+
// vs handles setting vars with predefined values.
317355
type vs struct {
318356
vars *[]string
319357
vals []string

0 commit comments

Comments
 (0)