Skip to content

Commit 2b51c55

Browse files
Improve shell completions for buf flags (#4416)
This takes a pass at improving the shell completions provided by `buf` by wiring into cobra's completion system. This: * Adds completions for "enum" based flags (provides the potential values; disables flags & generally orders from most-used to least-used) * Adds directory-only completions for `--output` in generate and export * Adds file-extension completions for `--template` (yaml extensions) and `--binary` (wasm) * Disables file completions for free-form string flags like `--label`, `--source-control-url`, `--header`, etc. where it's unlikely that a user would want to supply a local file name Implementation-wise, this is all wired up under ModifyCobra, so we need the latest version of app-go which fixes that working on subcommands. It generally uses `errors.Join` just to simplify the error cases, which should not occur. Shared completions are in `private/buf/bufcli/completions.go`. Vaguely related to #4402, which is aiming to add more dynamic completions. In that PR, I mentioned further improvements to completions for more dynamic situations, but this should shore up the "base case" of the relatively straightforward static-ish completions. Ref: https://cobra.dev/docs/how-to-guides/shell-completion/
1 parent 0a99d8c commit 2b51c55

58 files changed

Lines changed: 434 additions & 10 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## [Unreleased]
44

5+
- Improve shell completions for `buf` flags with fixed value sets and file/directory arguments.
56
- Add support for Edition 2024 syntax to `buf format`.
67

78
## [v1.67.0] - 2026-04-01

cmd/buf/buf.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,14 @@ func newRootCommand(name string) *appcmd.Command {
462462
},
463463
ModifyCobra: func(cobraCommand *cobra.Command) error {
464464
cobraCommand.AddCommand(bufcobra.NewWebpagesCommand("webpages", cobraCommand))
465-
return nil
465+
return cobraCommand.RegisterFlagCompletionFunc(
466+
"log-format",
467+
cobra.FixedCompletions([]string{
468+
appext.LogFormatText.String(),
469+
appext.LogFormatColor.String(),
470+
appext.LogFormatJSON.String(),
471+
}, cobra.ShellCompDirectiveNoFileComp|cobra.ShellCompDirectiveKeepOrder),
472+
)
466473
},
467474
}
468475
}

cmd/buf/internal/command/alpha/registry/token/tokenget/tokenget.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"github.com/bufbuild/buf/private/pkg/connectclient"
2929
"github.com/bufbuild/buf/private/pkg/netext"
3030
"github.com/bufbuild/buf/private/pkg/syserror"
31+
"github.com/spf13/cobra"
3132
"github.com/spf13/pflag"
3233
)
3334

@@ -52,6 +53,9 @@ func NewCommand(
5253
},
5354
),
5455
BindFlags: flags.Bind,
56+
ModifyCobra: func(cmd *cobra.Command) error {
57+
return bufcli.RegisterFlagCompletionOutputFormat(cmd, formatFlagName)
58+
},
5559
}
5660
}
5761

cmd/buf/internal/command/alpha/registry/token/tokenlist/tokenlist.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"github.com/bufbuild/buf/private/pkg/connectclient"
2929
"github.com/bufbuild/buf/private/pkg/netext"
3030
"github.com/bufbuild/buf/private/pkg/syserror"
31+
"github.com/spf13/cobra"
3132
"github.com/spf13/pflag"
3233
)
3334

@@ -54,6 +55,9 @@ func NewCommand(
5455
},
5556
),
5657
BindFlags: flags.Bind,
58+
ModifyCobra: func(cmd *cobra.Command) error {
59+
return bufcli.RegisterFlagCompletionOutputFormat(cmd, formatFlagName)
60+
},
5761
}
5862
}
5963

cmd/buf/internal/command/beta/registry/plugin/pluginpush/pluginpush.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ import (
4747
pkgv1 "github.com/google/go-containerregistry/pkg/v1"
4848
"github.com/google/go-containerregistry/pkg/v1/remote"
4949
"github.com/google/go-containerregistry/pkg/v1/remote/transport"
50+
"github.com/spf13/cobra"
5051
"github.com/spf13/pflag"
5152
)
5253

@@ -83,6 +84,12 @@ func NewCommand(
8384
},
8485
),
8586
BindFlags: flags.Bind,
87+
ModifyCobra: func(cmd *cobra.Command) error {
88+
return errors.Join(
89+
bufcli.RegisterFlagCompletionOutputFormat(cmd, formatFlagName),
90+
bufcli.RegisterFlagCompletionVisibility(cmd, visibilityFlagName),
91+
)
92+
},
8693
}
8794
}
8895

cmd/buf/internal/command/breaking/breaking.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import (
3838
"github.com/bufbuild/buf/private/bufpkg/bufregistryapi/bufregistryapimodule"
3939
"github.com/bufbuild/buf/private/pkg/syserror"
4040
"github.com/bufbuild/buf/private/pkg/wasm"
41+
"github.com/spf13/cobra"
4142
"github.com/spf13/pflag"
4243
)
4344

@@ -74,6 +75,9 @@ func NewCommand(
7475
},
7576
),
7677
BindFlags: flags.Bind,
78+
ModifyCobra: func(cmd *cobra.Command) error {
79+
return bufcli.RegisterFlagCompletionErrorFormat(cmd, errorFormatFlagName)
80+
},
7781
}
7882
}
7983

cmd/buf/internal/command/build/build.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"github.com/bufbuild/buf/private/buf/buffetch"
2828
"github.com/bufbuild/buf/private/bufpkg/bufanalysis"
2929
"github.com/bufbuild/buf/private/bufpkg/bufimage/bufimageutil"
30+
"github.com/spf13/cobra"
3031
"github.com/spf13/pflag"
3132
)
3233

@@ -62,6 +63,9 @@ func NewCommand(
6263
},
6364
),
6465
BindFlags: flags.Bind,
66+
ModifyCobra: func(cmd *cobra.Command) error {
67+
return bufcli.RegisterFlagCompletionErrorFormat(cmd, errorFormatFlagName)
68+
},
6569
}
6670
}
6771

cmd/buf/internal/command/config/configlsmodules/configlsmodules.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131
"github.com/bufbuild/buf/private/bufpkg/bufconfig"
3232
"github.com/bufbuild/buf/private/pkg/normalpath"
3333
"github.com/bufbuild/buf/private/pkg/syserror"
34+
"github.com/spf13/cobra"
3435
"github.com/spf13/pflag"
3536
)
3637

@@ -69,6 +70,16 @@ func NewCommand(
6970
},
7071
),
7172
BindFlags: flags.Bind,
73+
ModifyCobra: func(cmd *cobra.Command) error {
74+
return cmd.RegisterFlagCompletionFunc(
75+
formatFlagName,
76+
cobra.FixedCompletions([]string{
77+
formatPath,
78+
formatName,
79+
formatJSON,
80+
}, cobra.ShellCompDirectiveNoFileComp|cobra.ShellCompDirectiveKeepOrder),
81+
)
82+
},
7283
}
7384
}
7485

cmd/buf/internal/command/config/internal/internal.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131
"github.com/bufbuild/buf/private/bufpkg/bufconfig"
3232
"github.com/bufbuild/buf/private/pkg/normalpath"
3333
"github.com/bufbuild/buf/private/pkg/syserror"
34+
"github.com/spf13/cobra"
3435
"github.com/spf13/pflag"
3536
)
3637

@@ -66,6 +67,12 @@ func NewLSCommand(
6667
},
6768
),
6869
BindFlags: flags.Bind,
70+
ModifyCobra: func(cmd *cobra.Command) error {
71+
return errors.Join(
72+
bufcli.RegisterFlagCompletionRuleFormat(cmd, formatFlagName),
73+
bufcli.RegisterFlagCompletionFileVersion(cmd, versionFlagName),
74+
)
75+
},
6976
}
7077
}
7178

@@ -123,11 +130,13 @@ func (f *flags) Bind(flagSet *pflag.FlagSet) {
123130
fmt.Sprintf(
124131
"List all the rules for the given configuration version. By default, the version in the buf.yaml in the current directory is used, or the latest version otherwise (currently v2). Cannot be set if --%s is set. Must be one of %s",
125132
configuredOnlyFlagName,
126-
xslices.Map(
127-
bufconfig.AllFileVersions,
128-
func(fileVersion bufconfig.FileVersion) string {
129-
return fileVersion.String()
130-
},
133+
xstrings.SliceToString(
134+
xslices.Map(
135+
bufconfig.AllFileVersions,
136+
func(fileVersion bufconfig.FileVersion) string {
137+
return fileVersion.String()
138+
},
139+
),
131140
),
132141
),
133142
)

cmd/buf/internal/command/convert/convert.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import (
3232
"github.com/bufbuild/buf/private/bufpkg/bufimage/bufimageutil"
3333
"github.com/bufbuild/buf/private/bufpkg/bufmodule"
3434
"github.com/bufbuild/buf/private/gen/data/datawkt"
35+
"github.com/spf13/cobra"
3536
"github.com/spf13/pflag"
3637
)
3738

@@ -91,6 +92,9 @@ Use a module on the bsr:
9192
},
9293
),
9394
BindFlags: flags.Bind,
95+
ModifyCobra: func(cmd *cobra.Command) error {
96+
return bufcli.RegisterFlagCompletionErrorFormat(cmd, errorFormatFlagName)
97+
},
9498
}
9599
}
96100

0 commit comments

Comments
 (0)