|
| 1 | +// Copyright 2022 The Bazel Authors. All rights reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License.package com.google.devtools.build.lib.runtime.commands; |
| 14 | +package com.google.devtools.build.lib.runtime.commands; |
| 15 | + |
| 16 | +import static java.nio.charset.StandardCharsets.UTF_8; |
| 17 | + |
| 18 | +import com.google.common.base.Joiner; |
| 19 | +import com.google.devtools.build.lib.query2.common.CommonQueryOptions; |
| 20 | +import com.google.devtools.build.lib.query2.engine.QueryException; |
| 21 | +import com.google.devtools.build.lib.runtime.CommandEnvironment; |
| 22 | +import com.google.devtools.build.lib.server.FailureDetails.Query; |
| 23 | +import com.google.devtools.build.lib.vfs.FileSystemUtils; |
| 24 | +import com.google.devtools.build.lib.vfs.Path; |
| 25 | +import com.google.devtools.common.options.OptionsParsingResult; |
| 26 | +import java.io.IOException; |
| 27 | + |
| 28 | +/** |
| 29 | + * Reads the query for query, cquery and aquery using the --query_file option or from the residue of |
| 30 | + * the command line. |
| 31 | + */ |
| 32 | +public final class QueryOptionHelper { |
| 33 | + |
| 34 | + public static String readQuery( |
| 35 | + CommonQueryOptions queryOptions, |
| 36 | + OptionsParsingResult options, |
| 37 | + CommandEnvironment env, |
| 38 | + boolean allowEmptyQuery) |
| 39 | + throws QueryException { |
| 40 | + String query = ""; |
| 41 | + if (!options.getResidue().isEmpty()) { |
| 42 | + if (!queryOptions.queryFile.isEmpty()) { |
| 43 | + throw new QueryException( |
| 44 | + "Command-line query and --query_file cannot both be specified", |
| 45 | + Query.Code.QUERY_FILE_WITH_COMMAND_LINE_EXPRESSION); |
| 46 | + } |
| 47 | + query = Joiner.on(' ').join(options.getResidue()); |
| 48 | + } else if (!queryOptions.queryFile.isEmpty()) { |
| 49 | + // Works for absolute or relative query file. |
| 50 | + Path residuePath = env.getWorkingDirectory().getRelative(queryOptions.queryFile); |
| 51 | + try { |
| 52 | + query = new String(FileSystemUtils.readContent(residuePath), UTF_8); |
| 53 | + } catch (IOException unused) { |
| 54 | + throw new QueryException( |
| 55 | + "I/O error reading from " + residuePath.getPathString(), |
| 56 | + Query.Code.QUERY_FILE_READ_FAILURE); |
| 57 | + } |
| 58 | + } else { |
| 59 | + // When querying for the state of Skyframe, it's possible to omit the query expression. |
| 60 | + if (!allowEmptyQuery) { |
| 61 | + throw new QueryException( |
| 62 | + String.format( |
| 63 | + "missing query expression. Type '%s help query' for syntax and help", |
| 64 | + env.getRuntime().getProductName()), |
| 65 | + Query.Code.COMMAND_LINE_EXPRESSION_MISSING); |
| 66 | + } |
| 67 | + } |
| 68 | + return query; |
| 69 | + } |
| 70 | + |
| 71 | + private QueryOptionHelper() {} |
| 72 | +} |
0 commit comments