blob: 4569872bfb2fc5ca93d4ab306a496be9feb64e7c [file] [log] [blame]
/*
* Copyright 2012-present Facebook, Inc.
*
* 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.facebook.buck.cli;
import com.facebook.buck.util.Console;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableMap;
import java.io.IOException;
import java.util.List;
import java.util.Map;
public class AuditCommandRunner implements CommandRunner {
private static ImmutableMap<String, ? extends AbstractCommandRunner<?>> auditCommands;
private final Console console;
public AuditCommandRunner(CommandRunnerParams params) {
console = params.getConsole();
setAuditCommands(params);
}
@Override
public int runCommand(BuckConfig buckConfig, List<String> args)
throws IOException, InterruptedException {
if (args.isEmpty()) {
console.printBuildFailure("No audit command is given.");
printUsage();
return 1;
}
String auditCmd = args.get(0);
if (getAuditCommands().containsKey(auditCmd)) {
CommandRunner cmd = Preconditions.checkNotNull(getAuditCommands().get(auditCmd));
return cmd.runCommand(buckConfig, args.subList(1, args.size()));
} else {
console.printBuildFailure("Unknown audit command: " + auditCmd);
printUsage();
return 1;
}
}
private ImmutableMap<String, ? extends AbstractCommandRunner<?>> getAuditCommands() {
return auditCommands;
}
private void setAuditCommands(CommandRunnerParams params) {
auditCommands = ImmutableMap.of(
"input", new AuditInputCommand(params),
"classpath", new AuditClasspathCommand(params),
"dependencies", new AuditDependenciesCommand(params),
"owner", new AuditOwnerCommand(params),
"rules", new AuditRulesCommand(params));
}
private void printUsage() {
// TODO(user): implement better way of showing help
console.getStdOut().println("buck audit <cmd>");
for (Map.Entry<String, ? extends AbstractCommandRunner<?>> entry :
getAuditCommands().entrySet()) {
console.getStdOut().println(" " + entry.getKey() + " - " + entry.getValue().getUsageIntro());
}
}
}