Skip to content

Commit

Permalink
feat: add flag and label completion for bazel commands (#4584)
Browse files Browse the repository at this point in the history
Inspired by #373 with a
re-write + simplication + improvement of the completion logic from the
original.

To enable bash completion:

```
$ brew install bash-completion
$ aspect completion bash > $(brew --prefix)/etc/bash_completion.d/aspect

# add to your .bash_profile:

if [[ -r "$HOMEBREW_PREFIX/etc/profile.d/bash_completion.sh" ]]; then
  . "$HOMEBREW_PREFIX/etc/profile.d/bash_completion.sh"
  echo "bash completion loaded"
fi
```

See https://tecadmin.net/enable-bash-completion-on-macos/ for more info.

Demos:

```
$ aspect <tab><tab>
analyze-profile     (Analyze build profile data)                                         info                (Display runtime info about the bazel server)
aquery              (Query the action graph)                                             init                (Create a new Bazel workspace)
build               (Build the specified targets)                                        license             (Prints the license of this software.)
canonicalize-flags  (Present a list of bazel options in a canonical form)                lint                (Run configured linters over the dependency graph.)
clean               (Remove the output tree)                                             outputs             (Print paths to declared output files)
completion          (Generate the autocompletion script for the specified shell)         print               (Print syntax elements from BUILD files)
config              (Displays details of configurations.)                                query               (Query the dependency graph, ignoring configuration flags)
configure           (Auto-configure Bazel by updating BUILD files)                       run                 (Build a single target and run it with the given arguments)
coverage            (Same as 'test', but also generates a code coverage report.)         shutdown            (Stop the bazel server)
cquery              (Query the dependency graph, honoring configuration flags)           support             (Interactive, human-escalated support for Bazel problems)
docs                (Open documentation in the browser)                                  test                (Build the specified targets and run all test targets among them)
fetch               (Fetch external repositories that are prerequisites to the targets)  version             (Print the versions of Aspect CLI and Bazel)
help
```

```
$ aspect test <tab><tab>
:test        :test2       foo          foo/bar      foo/fum/bar
```

```
$ aspect test foo<tab><tab>
foo           foo/bar       foo/fum/bar   foo:foo       foo:fum/test  foo:test      foo:test2
```

```
$ aspect test foo/<tab><tab>
foo/bar      foo/fum/bar
```

```
$ aspect test //foo<tab><tab>
//foo           //foo/bar       //foo/fum/bar   //foo:foo       //foo:fum/test  //foo:test      //foo:test2
```

```
$ aspect test //foo/<tab><tab>
//foo/bar      //foo/fum/bar
```

```
$ aspect test @//foo<tab><tab>
@//foo           @//foo/bar       @//foo/fum/bar   @//foo:foo       @//foo:fum/test  @//foo:test      @//foo:test2
```

```
$ aspect test @//foo/<tab><tab>
@//foo/bar      @//foo/fum/bar
```

```
$ aspect test @@//foo<tab><tab>
@@//foo           @@//foo/bar       @@//foo/fum/bar   @@//foo:foo       @@//foo:fum/test  @@//foo:test      @@//foo:test2
```

```
$ aspect test @@//foo/<tab><tab>
@@//foo/bar      @@//foo/fum/bar
```

```
$ cd foo
$ aspect test <tab><tab>
:foo       :fum/test  :test      :test2     bar        fum/bar
```

GitOrigin-RevId: 4044b8d8bb34dfad160cb1d4728bf13d10cb3b9c
  • Loading branch information
gregmagolan committed Feb 16, 2024
1 parent ce7cf4f commit debbd0f
Show file tree
Hide file tree
Showing 7 changed files with 249 additions and 49 deletions.
2 changes: 1 addition & 1 deletion pkg/aspect/aquery/aquery.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func New(streams ioutils.Streams, bzl bazel.Bazel, isInteractive bool) *AQuery {
}

func (runner *AQuery) Run(ctx context.Context, cmd *cobra.Command, args []string) (exitErr error) {
nonFlags, flags, err := bazel.ParseOutBazelFlags(cmd.CalledAs(), args)
nonFlags, flags, err := bazel.SeparateBazelFlags(cmd.CalledAs(), args)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/aspect/cquery/cquery.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func New(streams ioutils.Streams, bzl bazel.Bazel, isInteractive bool) *CQuery {
}

func (runner *CQuery) Run(ctx context.Context, cmd *cobra.Command, args []string) (exitErr error) {
nonFlags, flags, err := bazel.ParseOutBazelFlags(cmd.CalledAs(), args)
nonFlags, flags, err := bazel.SeparateBazelFlags(cmd.CalledAs(), args)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/aspect/outputs/outputs.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func New(streams ioutils.Streams, bzl bazel.Bazel) *Outputs {
}

func (runner *Outputs) Run(_ context.Context, _ *cobra.Command, args []string) error {
nonFlags, bazelFlags, err := bazel.ParseOutBazelFlags("aquery", args)
nonFlags, bazelFlags, err := bazel.SeparateBazelFlags("aquery", args)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/aspect/query/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func New(streams ioutils.Streams, bzl bazel.Bazel, isInteractive bool) *Query {
}

func (runner *Query) Run(ctx context.Context, cmd *cobra.Command, args []string) (errExit error) {
nonFlags, flags, err := bazel.ParseOutBazelFlags(cmd.CalledAs(), args)
nonFlags, flags, err := bazel.SeparateBazelFlags(cmd.CalledAs(), args)
if err != nil {
return err
}
Expand Down
1 change: 1 addition & 0 deletions pkg/bazel/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ go_library(
"@com_github_bazelbuild_bazelisk//platforms",
"@com_github_bazelbuild_bazelisk//repositories",
"@com_github_bazelbuild_bazelisk//versions",
"@com_github_bazelbuild_buildtools//edit:go_default_library",
"@com_github_mitchellh_go_homedir//:go-homedir",
"@com_github_spf13_cobra//:cobra",
"@com_github_spf13_pflag//:pflag",
Expand Down
2 changes: 1 addition & 1 deletion pkg/bazel/bazel.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ func (b *bazel) RunCommand(streams ioutils.Streams, wd *string, command ...strin

// Initializes start-up flags from args and returns args without start-up flags
func InitializeStartupFlags(args []string) ([]string, []string, error) {
nonFlags, flags, err := ParseOutBazelFlags("startup", args)
nonFlags, flags, err := SeparateBazelFlags("startup", args)
if err != nil {
return nil, nil, err
}
Expand Down
Loading

0 comments on commit debbd0f

Please sign in to comment.