/** * For each source path, execute the command * * @return 0 if it runs successfully; -1 if it fails */ public int runAll() { int exitCode = 0; for (String src : args) { try { PathData[] srcs = PathData.expandAsGlob(src, getConf()); for (PathData s : srcs) { run(s); } } catch (IOException e) { exitCode = -1; displayError(e); } } return exitCode; }
private void printInstanceHelp(PrintStream out, Command instance) { out.println(instance.getUsage() + " :"); TableListing listing = null; final String prefix = " "; for (String line : instance.getDescription().split("\n")) { if (line.matches("^[ \t]*[-<].*$")) { String[] segments = line.split(":");
/** * The short usage suitable for the synopsis * @return "name options" */ public String getUsage() { String cmd = "-" + getName(); String usage = isDeprecated() ? "" : getCommandField(COMMAND_USAGE_FIELD); return usage.isEmpty() ? cmd : cmd + " " + usage; }
/** * Allows commands that don't use paths to handle the raw arguments. * Default behavior is to expand the arguments via * {@link #expandArguments(LinkedList)} and pass the resulting list to * {@link #processArguments(LinkedList)} * @param args the list of argument strings * @throws IOException */ protected void processRawArguments(LinkedList<String> args) throws IOException { processArguments(expandArguments(args)); }
/** * Processes the command's list of expanded arguments. * {@link #processArgument(PathData)} will be invoked with each item * in the list. The loop catches IOExceptions, increments the error * count, and displays the exception. * @param args a list of {@link PathData} to process * @throws IOException if anything goes wrong... */ protected void processArguments(LinkedList<PathData> args) throws IOException { for (PathData arg : args) { try { processArgument(arg); } catch (IOException e) { displayError(e); } } }
LinkedList<String> args = new LinkedList<String>(Arrays.asList(argv)); try { if (isDeprecated()) { displayWarning( "DEPRECATED: Please use '"+ getReplacementCommand() + "' instead."); processOptions(args); processRawArguments(args); } catch (CommandInterruptException e) { displayError("Interrupted"); return 130; } catch (IOException e) { displayError(e); return (numErrors == 0) ? exitCode : exitCodeForError();
/** * Iterates over the given expanded paths and invokes * {@link #processPath(PathData)} on each element. If "recursive" is true, * will do a post-visit DFS on directories. * @param parent if called via a recurse, will be the parent dir, else null * @param items a list of {@link PathData} objects to process * @throws IOException if anything goes wrong... */ protected void processPaths(PathData parent, PathData ... items) throws IOException { // TODO: this really should be iterative for (PathData item : items) { try { processPath(item); if (recursive && isPathRecursable(item)) { recursePath(item); } postProcessPath(item); } catch (IOException e) { displayError(e); } } }
try { Path srcPath = new Path(src); FileSystem fs = srcPath.getFileSystem(getConf()); FileStatus[] statuses = fs.globStatus(srcPath); if (statuses == null) { } else { for(FileStatus s : statuses) { run(s.getPath()); content = content.substring(0, eol); System.err.println(getCommandName() + ": " + content); } catch (IOException e) { exitCode = -1; System.err.println(getCommandName() + ": " + e.getLocalizedMessage());
throw new UnknownCommandException(); TraceScope scope = tracer.newScope(instance.getCommandName()); if (scope.getSpan() != null) { String args = StringUtils.join(" ", argv); exitCode = instance.run(Arrays.copyOfRange(argv, 1, argv.length)); } finally { scope.close();
/** * Expands a list of arguments into {@link PathData} objects. The default * behavior is to call {@link #expandArgument(String)} on each element * which by default globs the argument. The loop catches IOExceptions, * increments the error count, and displays the exception. * @param args strings to expand into {@link PathData} objects * @return list of all {@link PathData} objects the arguments * @throws IOException if anything goes wrong... */ protected LinkedList<PathData> expandArguments(LinkedList<String> args) throws IOException { LinkedList<PathData> expandedArgs = new LinkedList<PathData>(); for (String arg : args) { try { expandedArgs.addAll(expandArgument(arg)); } catch (IOException e) { // other exceptions are probably nasty displayError(e); } } return expandedArgs; }
/** * Execute the command on the input path data. Commands can override to make * use of the resolved filesystem. * @param pathData The input path with resolved filesystem * @throws IOException */ protected void run(PathData pathData) throws IOException { run(pathData.path); }
int exitCode = 0; if (args == null) { // no argument return run(); try { Path srcPath = new Path(src); FileSystem fs = srcPath.getFileSystem(getConf()); FileStatus[] statuses = fs.globStatus(srcPath); if (statuses == null) { for(FileStatus s : statuses) { try { run(s.getPath()); } catch (FileNotFoundException ex) { handleIOException(e);
return command.getDescription(); final int result = command.run(arguments); if (contextCommands.isShowResultCode()) { if (result == 0) {
/** * Expand the given argument into a list of {@link PathData} objects. * The default behavior is to expand globs. Commands may override to * perform other expansions on an argument. * @param arg string pattern to expand * @return list of {@link PathData} objects * @throws IOException if anything goes wrong... */ protected List<PathData> expandArgument(String arg) throws IOException { PathData[] items = PathData.expandAsGlob(arg, getConf()); if (items.length == 0) { // it's a glob that failed to match throw new PathNotFoundException(arg); } return Arrays.asList(items); }
@Test @Ignore public void generateMethods() { final CommandFactory commandFactory = new CommandFactory(new Configuration()); FsCommand.registerCommands(commandFactory); final String[] names = commandFactory.getNames(); final String collect = Arrays.stream(names).map(item -> "\"" + item.replace("-", "") + "\"").collect(Collectors.joining(",")); System.out.println(collect); Arrays.stream(names).map(commandFactory::getInstance).forEach(item -> { String description = ""; final String[] sentences = item.getDescription().split("\\."); if (sentences.length == 0) { description = item.getDescription(); } else { description = sentences[0] + "."; } String cliCommand = String.format("@CliCommand(value = {\"%s\", \"hdfs dfs -%s\"}, help = \"%s\")", item.getCommandName(), item.getCommandName(), description); String content = String.format(" public String %s(\n" + " @CliOption(key = {\"\"}, help = \"%s\") String path\n" + " ) {\n" + " return runCommand(\"%s\", path);\n" + " }\n", item.getCommandName(), description, item.getCommandName()); System.out.println(cliCommand); System.out.println(content); System.out.println(); }); }
/** * Display an warning string prefaced with the command name. * @param message warning message to display */ public void displayWarning(String message) { err.println(getName() + ": " + message); }
/** handle IOException */ private void handleIOException(IOException e) { String content = e.getLocalizedMessage(); if (e instanceof RemoteException) { int eol = content.indexOf('\n'); if (eol>=0) { content = content.substring(0, eol); } } System.err.println(getCommandName() + ": " + content); } }
/** * Display an error string prefaced with the command name. Also increments * the error count for the command which will result in a non-zero exit * code. * @param message error message to display */ public void displayError(String message) { numErrors++; displayWarning(message); }
/** * Display an exception prefaced with the command name. Also increments * the error count for the command which will result in a non-zero exit * code. * @param e exception to display */ public void displayError(Exception e) { // build up a list of exceptions that occurred exceptions.add(e); // use runtime so it rips up through the stack and exits out if (e instanceof InterruptedIOException) { throw new CommandInterruptException(); } String errorMessage = e.getLocalizedMessage(); if (errorMessage == null) { // this is an unexpected condition, so dump the whole exception since // it's probably a nasty internal error where the backtrace would be // useful errorMessage = StringUtils.stringifyException(e); LOG.debug(errorMessage); } else { errorMessage = errorMessage.split("\n", 2)[0]; } displayError(errorMessage); }