-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[6.2.0] Allows --query_file to be used for cquery and aquery too. (#1…
…7823) Fixes #12924. RELNOTES[NEW]: The aquery and cquery commands now respect the --query_file flag just like the query command. PiperOrigin-RevId: 487689456 Change-Id: Ia2c9d85e88fdf769a823eaf7b6585a77d654ae70
- Loading branch information
Showing
9 changed files
with
137 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
src/main/java/com/google/devtools/build/lib/runtime/commands/QueryOptionHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// Copyright 2022 The Bazel Authors. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License.package com.google.devtools.build.lib.runtime.commands; | ||
package com.google.devtools.build.lib.runtime.commands; | ||
|
||
import static java.nio.charset.StandardCharsets.UTF_8; | ||
|
||
import com.google.common.base.Joiner; | ||
import com.google.devtools.build.lib.query2.common.CommonQueryOptions; | ||
import com.google.devtools.build.lib.query2.engine.QueryException; | ||
import com.google.devtools.build.lib.runtime.CommandEnvironment; | ||
import com.google.devtools.build.lib.server.FailureDetails.Query; | ||
import com.google.devtools.build.lib.vfs.FileSystemUtils; | ||
import com.google.devtools.build.lib.vfs.Path; | ||
import com.google.devtools.common.options.OptionsParsingResult; | ||
import java.io.IOException; | ||
|
||
/** | ||
* Reads the query for query, cquery and aquery using the --query_file option or from the residue of | ||
* the command line. | ||
*/ | ||
public final class QueryOptionHelper { | ||
|
||
public static String readQuery( | ||
CommonQueryOptions queryOptions, | ||
OptionsParsingResult options, | ||
CommandEnvironment env, | ||
boolean allowEmptyQuery) | ||
throws QueryException { | ||
String query = ""; | ||
if (!options.getResidue().isEmpty()) { | ||
if (!queryOptions.queryFile.isEmpty()) { | ||
throw new QueryException( | ||
"Command-line query and --query_file cannot both be specified", | ||
Query.Code.QUERY_FILE_WITH_COMMAND_LINE_EXPRESSION); | ||
} | ||
query = Joiner.on(' ').join(options.getResidue()); | ||
} else if (!queryOptions.queryFile.isEmpty()) { | ||
// Works for absolute or relative query file. | ||
Path residuePath = env.getWorkingDirectory().getRelative(queryOptions.queryFile); | ||
try { | ||
query = new String(FileSystemUtils.readContent(residuePath), UTF_8); | ||
} catch (IOException unused) { | ||
throw new QueryException( | ||
"I/O error reading from " + residuePath.getPathString(), | ||
Query.Code.QUERY_FILE_READ_FAILURE); | ||
} | ||
} else { | ||
// When querying for the state of Skyframe, it's possible to omit the query expression. | ||
if (!allowEmptyQuery) { | ||
throw new QueryException( | ||
String.format( | ||
"missing query expression. Type '%s help query' for syntax and help", | ||
env.getRuntime().getProductName()), | ||
Query.Code.COMMAND_LINE_EXPRESSION_MISSING); | ||
} | ||
} | ||
return query; | ||
} | ||
|
||
private QueryOptionHelper() {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters