Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(copybara): sync commits from Aspect-internal silo #659

Merged
merged 1 commit into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 34 additions & 8 deletions pkg/aspect/outputs/outputs.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package outputs
import (
"context"
"fmt"
"strings"

"github.com/spf13/cobra"

Expand All @@ -44,17 +45,42 @@ func (runner *Outputs) Run(_ context.Context, _ *cobra.Command, args []string) e
return err
}

if len(nonFlags) < 1 {
return fmt.Errorf("a query expression is required as the first argument to outputs command")
// Test to see if the command has been passed the `--query_file` Bazel flag.
// There is no short hand version of this flag, so the single check is fine.
hasQueryFileBazelFlag := false
for _, bazelFlag := range bazelFlags {
if strings.HasPrefix(bazelFlag, "--query_file") {
hasQueryFileBazelFlag = true
break
}
}

query := nonFlags[0]

// Query will be empty string when supplying a query via a query file.
// The code in `bzl.Aquery` will filter that.
var query string
var mnemonicFilter string
if len(nonFlags) == 2 {
mnemonicFilter = nonFlags[1]
} else if len(nonFlags) > 2 {
return fmt.Errorf("expecting a maximum of 2 arguments to outputs command but got %v", len(nonFlags))
numNonFlags := len(nonFlags)

if hasQueryFileBazelFlag {
// We may have a single arg that is the mnemonic filter, or none.
if numNonFlags == 1 {
// The first should be the mnemonic
mnemonicFilter = nonFlags[0]
} else if numNonFlags > 1 {
return fmt.Errorf("expecting a maximum of 1 argument to outputs when using --query_file, got %v", numNonFlags)
}
} else {
// No use of `--query_file`, see what args we do have.
if numNonFlags < 1 {
return fmt.Errorf("a query expression is required as the first argument to outputs command")
}
query = nonFlags[0]

if numNonFlags == 2 {
mnemonicFilter = nonFlags[1]
} else if numNonFlags > 2 {
return fmt.Errorf("expecting a maximum of 2 arguments to outputs command but got %v", numNonFlags)
}
}

agc, err := runner.bzl.AQuery(query, bazelFlags)
Expand Down
7 changes: 5 additions & 2 deletions pkg/bazel/bazel.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,8 +280,11 @@ func (b *bazel) AQuery(query string, bazelFlags []string) (*analysis.ActionGraph
cmd := []string{"aquery"}
cmd = append(cmd, bazelFlags...)
cmd = append(cmd, "--output=proto")
cmd = append(cmd, "--")
cmd = append(cmd, query)

if query != "" {
cmd = append(cmd, "--")
cmd = append(cmd, query)
}
err := b.RunCommand(streams, nil, cmd...)
bazelErrs <- err
}()
Expand Down
Loading