@Override public String convert(String value) throws Exception { if (!possibilities.contains(value)) { throw new ErrorMessage("Expected one of %s, but got %s", possibilities, value); } return value; } }
@Override public boolean hasNext() { if (pending) { return true; } try { line = reader.readLine(); } catch (IOException e) { throw new ErrorMessage(e, "Error while running %s", name); } pending = true; return line != null; }
@Override public int read() { try { return reader.read(); } catch (IOException e) { throw new ErrorMessage(e, "Error reading input"); } }
@Override public String readLine() { try { return reader.readLine(); } catch (IOException e) { throw new ErrorMessage(e, "Error reading input"); } }
@Override public void send(String content) { OutputStream outputStream = process.getOutputStream(); try { outputStream.write(content.getBytes(StandardCharsets.UTF_8)); outputStream.flush(); } catch (IOException e) { throw new ErrorMessage(e, "Error while sending content to %s", name); } }
public <T> List<T> getTrailing(CliConverter<T> converter) { List<T> result = new ArrayList<>(); for (String value : trailing) { try { result.add(converter.convert(value)); } catch (Exception e) { throw new ErrorMessage(e, "Failed to parse %s", value); } } return result; }
@Override public int waitFor(File outFile) { try (OutputStream out = new FileOutputStream(outFile)) { return waitFor(out); } catch (IOException e) { throw new ErrorMessage(e, "Error saving to %s", outFile); } }
public <T> T get(String option, CliConverter<T> converter) { if (!switches.contains(option)) { throw new IllegalStateException( String.format("Expected option name to be one of %s, but got %s", switches, option) ); } String value = values.get(option); try { return converter.convert(value); } catch (Exception e) { throw new ErrorMessage(e, "Failed to parse %s %s", option, value); } }
@Override public int waitFor() { stdout.background(); try { int result = process.waitFor(); stdoutFuture.get(); stderrFuture.get(); return result; } catch (InterruptedException e) { throw new ErrorMessage(e, "Interrupted while waiting for: %s", name); } catch (ExecutionException e) { throw new ErrorMessage(e, "Error while waiting for: %s", name); } finally { close(); } }
@Override public int waitFor(OutputStream out) { try { InputStream in = getStdOut(); byte[] buffer = new byte[4096]; int read; while ((read = in.read(buffer)) != -1) { out.write(buffer, 0, read); } } catch (IOException e) { throw new ErrorMessage(e, "Error while waiting for %s", name); } try { return process.waitFor(); } catch (InterruptedException e) { throw new ErrorMessage(e, "Interrupted while waiting for %s", name); } finally { close(); } }
public <T> List<T> getMulti(String option, CliConverter<T> converter) { List<String> values = multiValues.get(option); if (values == null || values.isEmpty()) { // value must have been specified less than twice T value = get(option, converter); return value == null ? Collections.<T>emptyList() : Collections.singletonList(value); } List<T> result = new ArrayList<>(); for (String value : values) { try { result.add(converter.convert(value)); } catch (Exception e) { throw new ErrorMessage(e, "Failed to parse %s %s", option, value); } } return result; }
return processBuilder.start(); } catch (IOException e) { throw new ErrorMessage(e, "Error running: %s", command);
out.flush(); throw new ErrorMessage(join("\n", errors));
public T openService(HostAndPort host) { ThriftClient<T> client = createClient(); try { return openService(host, client, transport); } catch (TTransportException e) { throw new ErrorMessage(e, "Failed to connect to %s", host); } }
public void help(String commandName, List<String> arguments) { List<CliCommand> cliCommands = new ArrayList<>(commands.size() + 1); CommandBuilder selectedCommand = null; for (CommandBuilder command : commands) { CliCommand cliCommand = command.defineCommand(); cliCommands.add(cliCommand); if (cliCommand.getName().equals(commandName)) { selectedCommand = command; } } if (commandName == null) { printSummary(io.out, cliCommands); } else if (selectedCommand instanceof CommandGroup) { ((CommandGroup) selectedCommand).help(arguments); } else if (selectedCommand != null) { io.out.println(selectedCommand.defineCommand().getDocumentation()); } else { printSummary(io.out, cliCommands); io.out.println(); io.out.flush(); throw new ErrorMessage("Unknown commandName: %s", commandName); } }
throw new ErrorMessage("Ptail failed: %s", ptail.getError()); io.out.statusf("%,d of %,d (%.01f%%)", count, lines, 100.0 * count / lines); } catch (IOException e) { throw new ErrorMessage(e, "Error ptailing %s", category);
private T openService(HostAndPort host, ThriftClient<T> client, Transport transport) throws TTransportException { try { NiftyClientConnector<? extends NiftyClientChannel> connector; if (Transport.FRAMED.equals(transport)) { connector = new FramedClientConnector(host); } else if (Transport.BUFFERED.equals(transport)) { connector = new UnframedClientConnector(host); } else { throw new ErrorMessage("Unexpected thrift transport type: %s", transport); } return client.open(connector).get(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw new TTransportException(TIMED_OUT, "Interrupted opening connection to " + host, e); } catch (ExecutionException e) { Throwable cause = e.getCause(); Throwables.propagateIfInstanceOf(cause, TTransportException.class); throw new TTransportException(UNKNOWN, "Exception opening connection to " + host, cause); } } }
public T openService(HostAndPort host) { ThriftClient<T> client = createClient(); try { return openService(host, client, transport); } catch (TTransportException e) { throw new ErrorMessage(e, "Failed to connect to %s", host); } }
throw new ErrorMessage("Ptail failed: %s", ptail.getError()); io.out.statusf("%,d of %,d (%.01f%%)", count, lines, 100.0 * count / lines); } catch (IOException e) { throw new ErrorMessage(e, "Error ptailing %s", category);
private T openService(HostAndPort host, ThriftClient<T> client, Transport transport) throws TTransportException { try { NiftyClientConnector<? extends NiftyClientChannel> connector; if (Transport.FRAMED.equals(transport)) { connector = new FramedClientConnector(host); } else if (Transport.BUFFERED.equals(transport)) { connector = new UnframedClientConnector(host); } else { throw new ErrorMessage("Unexpected thrift transport type: %s", transport); } return client.open(connector).get(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw new TTransportException(TIMED_OUT, "Interrupted opening connection to " + host, e); } catch (ExecutionException e) { Throwable cause = e.getCause(); Throwables.propagateIfInstanceOf(cause, TTransportException.class); throw new TTransportException(UNKNOWN, "Exception opening connection to " + host, cause); } } }